在 bash
中,从环境变量 $PIPESTATUS
我们可以检索管道命令行的退出状态,如下所示。
$ echo "some string" | grep x | tee some.tmp ; echo ${PIPESTATUS[1]}
1
但是,如果我使用 ksh
,我需要像下面这样的操作来获取退出状态。我搜索它得到如下所示的内容。
$ getme=`((echo "some string" | grep x 3>&- 4>&- ; echo $? >&4) | tee some.tmp 1>&3 3>&- 4>&- ) 4>&1` ; echo $getme
1
$ getme=`((echo "some string " | grep me 3>&- 4>&- ; echo $? >&4) | tee some.tmp 1>&3 3>&- 4>&- ) 4>&1` ; echo $getme
0
ksh 中是否有比上述形式更简单的形式来检索管道命令行中的退出状态?以及如何解释上面的行。我对 subshell 和 4 作为描述符的用法知之甚少。 重定向部分有点难以解释
最佳答案
$?
不适合?
echo "some string" | grep x | tee some.tmp ; echo "Returncode: $?"
或者您需要每个管道的特定错误代码
您还可以尝试使用设置
选项
-e
Unless contained in a || or && command, or the commandfollowing an if while or until command or in the pipeline following !, if a command has a non-zero exit status, execute the ERR trap, if set, and exit. This mode is disabled while reading profiles.
-o pipefail
A pipeline will not complete until all components of the pipeline have completed, and the return value will be the value of the last non-zero command to fail or zero if no command has failed.
关于shell - ksh 中管道命令行的退出状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20991110/