我在Bourne shell上运行脚本,例如not bash (实际上是来自BusyBox 1.23.2的sh )。
正如this question on SU中的问答,我希望“捕捉”由我的脚本中的命令发出的错误消息,并在前面附加一个字符串,以便在日志文件中突出它们。然而,与链接问题中提供的答案相反,我不能使用进程替换,因为shell不是bash。
我正在将所有输出重定向到日志文件:
#exec 3>&1 4>&2 >>$LOGFILE 2> >(sed 's/^/ *** ERROR: /' >&1)
# --> -sh: syntax error: unexpected redirection
exec 3>&1 4>&2 >>$LOGFILE 2>&1发布于 2015-09-19 03:54:28
以下代码在busybox下工作:
$ { find /var/cache/ldconfig 3>&1 1>&2 2>&3 | sed 's/^/error: /' ; } >> /tmp/log 2>&1现在来解释一下:
find /var/cache/ldconfig在标准输出上生成消息,而stderr3>&1 1>&2 2>&3置换标准输出和标准错误(使用临时文件描述符,3)| sed 's/^/error: /'在标准输出流上执行插入)(以前的stderr){ cmd ; } >> /tmp/log 2>&1使正确地分组然后重定向标准输出和标准错误成为可能(以一种匿名函数将所有内容分组),将错误和输出流附加到/tmp/loghttps://stackoverflow.com/questions/32659580
复制相似问题