庆典:重定向(并附加)输出和错误到文件终端和得到适当的退出状态 [英] bash: redirect (and append) stdout and stderr to file and terminal and get proper exit status
问题描述
要重定向(和append)输出和错误到一个文件,同时在终端上显示出来,我这样做:
To redirect (and append) stdout and stderr to a file, while also displaying it on the terminal, I do this:
command 2>&1 | tee -a file.txt
然而,有另一种方式来做到这一点,这样我得到的退出状态的精确值?
However, is there another way to do this such that I get an accurate value for the exit status?
也就是说,如果我的测试 $?
,我想看看命令的退出状态
,而不是 T恤的退出状态
。
That is, if I test $?
, I want to see the exit status of command
, not the exit status of tee
.
我知道,我可以使用 $ {PIPESTATUS [0]}
而不是 $?
在这里,但我要寻找,不会有涉及检查另一种解决方案 PIPESTATUS
。
I know that I can use ${PIPESTATUS[0]}
here instead of $?
, but I am looking for another solution that would not involve having to check PIPESTATUS
.
推荐答案
也许你可以把从PIPESTATUS退出值到 $?
Perhaps you could put the exit value from PIPESTATUS into $?
command 2>&1 | tee -a file.txt ; ( exit ${PIPESTATUS} )
这篇关于庆典:重定向(并附加)输出和错误到文件终端和得到适当的退出状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!