bash – pipe stdout和stderr到shell脚本中的两个不同的进程?

我有一个pipline做的
command1 | command2

所以,command1的stdout转到command2,而command1的stderr转到终端(或者shell的stdout处)。

如何管道stderr的command1到第三个进程(command3),而stdout仍然是command2?

使用另一个文件描述符
{ command1 2>&3 | command2; } 3>&1 1>&2 | command3

您最多可以使用7个其他文件描述符:从3到9。
如果你想要更多的解释,请问,我可以解释;-)

测试

{ { echo a; echo >&2 b; } 2>&3 | sed >&2 's/$/1/'; } 3>&1 1>&2 | sed 's/$/2/'

输出

b2
a1

生成两个日志文件
1.只有stderr
stderr和stdout

{ { { command 2>&1 1>&3; } | tee err-only.log; } 3>&1; } > err-and-stdout.log

如果命令是echo“stdout”; echo“stderr”>& 2然后我们可以这样测试:

$ { { { echo out>&3;echo err>&1;}| tee err-only.log;} 3>&1;} > err-and-stdout.log
$ head err-only.log err-and-stdout.log
==> err-only.log <==
err

==> err-and-stdout.log <==
out
err

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...