[英]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. 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. 在这种情况下,
foo
是logger -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.