简体   繁体   English

嵌入式外壳不支持重定向:exec 2>>(logger -t myscript)

[英]embedded shell does not support redirection: exec 2> >(logger -t myscript)

I'm trying to run the command from this question : 我正在尝试从这个问题运行命令:

exec 2> >(logger -t myscript)

It works great on my desktop linux system, however, on my embedded linux device the same command presents the following error: 它在我的桌面linux系统上很好用,但是,在我的嵌入式linux设备上,相同的命令显示以下错误:

-sh: syntax error near unexpected token `>'

So I'm guessing my shell doesn't like part of the command syntax - most likely this portion: 所以我猜我的外壳不喜欢命令语法的一部分-很可能是这部分:

exec 2> > (logger -t myscript) exec 2> (logger -t myscript)

In fact, while I understand that the 2> is redirecting stderr I don't actually understand the syntax of the second > character in this case, is it another way of representing a pipe? 实际上,尽管我知道2>正在重定向stderr ,但在这种情况下我实际上并不理解second >字符的语法,这是表示管道的另一种方式吗?

If I can understand what it is doing then perhaps I can modify my command to work with my limited shell on the embedded linux device. 如果我能理解它在做什么,那么也许我可以修改命令以使其与嵌入式Linux设备上的有限外壳一起使用。

The syntax in question only works with bash (or other shells with ksh extensions). 有问题的语法仅适用于bash(或其他具有ksh扩展名的shell)。 In the error 在错误

-sh: syntax error near unexpected token `>'

...you're trying to use that syntax with /bin/sh . ...您正尝试在/bin/sh使用该语法。

Be sure your script starts with #!/bin/bash , and that you invoke it with bash yourscript rather than sh yourscript . 确保您的脚本以#!/bin/bash开头,并且您使用bash yourscript而不是sh yourscript


A bit more explanation: 多一点解释:

  • >(foo) gets replaced with a filename (of the form /dev/fd/## if supported, or a named pipe otherwise) which receives output from a process named foo . >(foo)被替换为文件名(如果支持,则为/dev/fd/##格式,否则为命名管道),该文件名将从名为foo的进程接收输出。 This is the part that requires bash or ksh extensions. 这是需要bash或ksh扩展的部分。
  • exec <redirection> applies a redirection to the current shell process (thus, exec 2>stderr.log redirects all stderr from the current command and its children to the file stderr.log ). exec <redirection>exec <redirection>应用于当前的shell进程(因此exec 2>stderr.log将所有stderr从当前命令及其子exec 2>stderr.log重定向到文件stderr.log )。

Thus, exec 2> >(foo) modifies the stderr file descriptor (of your current shell session) to go to the stdin of command foo ; 因此, exec 2> >(foo)修改(当前shell会话的)stderr文件描述符以转到命令foo的stdin; in this case, foo is logger -t myscript , thus sending the process's stderr to syslog. 在这种情况下, foologger -t myscript ,因此将进程的stderr发送到syslog。


To perform the same operation on a more limited (but still POSIX-compliant) shell: 要在功能更有限(但仍兼容POSIX)上执行相同的操作:

# note: if any security concerns exist, put the FIFO in a directory
#       created by mktemp -d rather than hardcoding its name

mkfifo /tmp/log.fifo                 # create the FIFO
logger -t myscript </tmp/log.fifo &  # start the reader in the background first!
exec 2>/tmp/log.fifo                 # then start writing
rm -f /tmp/log.fifo                  # safe to delete at this point

>( command ) is a bash construct called "process substitution". >( command )是一个bash构造,称为“进程替换”。 man bash says: man bash说:

Process substitution is supported on systems that support named pipes (FIFOs) or the /dev/fd method of naming open files. 在支持命名管道(FIFO)或命名打开文件的/dev/fd方法的系统上支持进程替换。

Your shell doesn't seem to be bash, anyway. 无论如何,您的外壳似乎都不是bash。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 此shell命令的意思是“ exec 3&gt;&1&gt;&gt;(logger -t“ OKOK”)” - What does this shell command mean “exec 3>&1 > >(logger -t ”OKOK“)” 为什么在Ubuntu上“ let”不能与“ sh myscript”一起使用,而对“ ./myscript”却可以呢? - Why doesn't “let” work with “sh myscript” on Ubuntu, when it does with “./myscript”? execvp shell 命令与 shell output 重定向不起作用 - execvp shell command with shell output redirection does not work shell_exec不适用于find - shell_exec doesn't work with find 如果反引号不能执行,STDERR重定向到STDOUT会丢失 - STDERR redirection to STDOUT lost if backticks can't exec Linux shell中的I / O流重定向。 shell如何使用重定向处理命令? - I/O stream redirection in a Linux shell. How does the shell process a command with redirection? shell_exec不运行某些shell命令 - shell_exec does not run some shell commands 来自 Linux 中的 shell 脚本的输出重定向一旦从 PHP 的 exec() 调用就被重定向到奇怪的位置 - Output redirection from shell script in Linux is redirected to weird location once called from PHP's exec() 即使添加新的 shell_exec 来停止另一个,shell_exec 也不会停止 - shell_exec won't stop even though add new shell_exec to stop the other one Linux | C语言中的Shell实现| 输入重定向不显示 - Linux | Shell implementation in C | Input redirection does not display
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM