将stdout和stderr重定向到其他程序,而无需更改退出代码 [英] Redirect stdout and stderr to different program without changing exit code

查看:46
本文介绍了将stdout和stderr重定向到其他程序,而无需更改退出代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这篇文章基本上会问同样的问题,但我需要一个附加要求,即:退出代码应保持不变.

接受的答案使用管道,因此退出代码不再是 command1 的退出代码.

命令集 pipefail 不符合我的需要,因为我不想影响执行 command1 时的行为,这可能是一个复合的Bash命令本身./p>

我尝试了以下方法,但是输出不符合预期:

  [$ hidden] $(echo haha​​; echo hehe 1& 2)>>(在读取-r x的同时,回显"xx $ x";完成)2>>(在读取-r y的同时,回显"yy $ y";完成)xx哈哈xx yy hehe 

任何人都可以告诉原因吗?

解决方案

我自己找到了答案:

  command1 3>& 1 4>& 2 1>>(command2 1& 3 2>& 4)2>>(command3 1& 3 2>& 4) 

通过这种方式,无论将 command2 command3 输出到stdout或stderr,都不会有问题.

This post basically asks the same question but I want an additional requirement, which is: the exit code should be unchanged.

The accepted answer uses pipe, so the exit code is no longer the exit code of command1.

Command set pipefail doesn't fit my need as I don't want to affect the behavior when executing command1, which might be a compound Bash command itself.

I tried the following approach, but the output is not as expected:

[hidden]$ (echo haha; echo hehe 1>&2) > >(while read -r x; do echo "xx $x"; done)
2> >(while read -r y; do echo "yy $y"; done)
xx haha
xx yy hehe

Can anyone tell why?

解决方案

I found the answer myself:

command1 3>&1 4>&2 1> >(command2 1>&3 2>&4) 2> >(command3 1>&3 2>&4)

In this way, no matter command2 and command3 output to stdout or stderr, there will be no problem.

这篇关于将stdout和stderr重定向到其他程序,而无需更改退出代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆