我正在尝试从this question运行这个命令
exec 2> >(logger -t myscript)它在我的桌面linux系统上工作得很好,但是,在我的嵌入式linux设备上,相同的命令会出现以下错误:
-sh: syntax error near unexpected token `>'因此,我猜我的shell不喜欢命令语法的一部分--很可能是这个部分:
exec 2>>(logger -t myscript)
实际上,虽然我理解2>重定向stderr,但在本例中我并不真正理解第二个>字符的语法,这是表示管道的另一种方式吗?
如果我能理解它在做什么,那么也许我可以修改我的命令,以便在嵌入式linux设备上使用我有限的shell。
发布于 2015-03-03 22:40:52
所讨论的语法仅适用于bash (或其他带有ksh扩展的shell)。在错误中
-sh: syntax error near unexpected token `>'.你试图在/bin/sh中使用这种语法.
确保脚本以#!/bin/bash开头,并且使用bash yourscript而不是sh yourscript调用它。
更详细的解释:
>(foo)被替换为一个文件名(如果支持的话是表单/dev/fd/##,或者一个指定的管道),该文件名接收来自名为foo的进程的输出。这是需要bash或ksh扩展的部分。exec <redirection>将重定向应用到当前shell进程(因此,exec 2>stderr.log将当前命令及其子命令中的所有stderr重定向到文件stderr.log)。因此,exec 2> >(foo)修改(当前shell会话的) stderr文件描述符以转到命令foo的stdin;在本例中,foo是logger -t myscript,从而将进程的stderr发送到syslog。
要在更有限的(但仍符合POSIX标准的)shell上执行相同的操作:
# 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发布于 2015-03-03 22:40:54
>( command )是一个名为“进程替换”的bash结构。man bash说:
在支持命名管道(FIFO)或命名打开文件的
/dev/fd方法的系统上,支持进程替换。
不管怎么说,你的蛋壳似乎不太好吃。
https://stackoverflow.com/questions/28843312
复制相似问题