加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-15 16:22:01 所属栏目:安全 来源:网络整理
导读:我有一个pipline做的 command1 | command2 所以,command1的stdout转到command2,而command1的stderr转到终端(或者shell的stdout处)。 如何管道stderr的command1到第三个进程(command3),而stdout仍然是command2? 使用另一个文件描述符 { command1 23 | com
我有一个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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读