shell - ksh 中管道命令行的退出状态

标签 shell pipe ksh

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 中是否有比上述形式更简单的形式来检索管道命令行中的退出状态?以及如何解释上面的行。我对 subshel​​l 和 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/

相关文章:

linux - 更智能、更简洁的方法来匹配 shell 脚本中的输入变量?

linux - 整合sed命令

linux - 使用 Linux 从文件名中包含前一周日期的 FTP 位置获取所有文件

image - 如何使程序在 PostScript 文件的顶部覆盖文本?

linux - 更改日期和时间系统后 Tomcat 卡住

shell - 从Shell转义sqlite3的单引号

Python:为子进程提供输入

c - 是否可以使用 libcurl 同时下载和发布 multipart/form-data 同一文件?

c - 如何访问不相关和以前运行的进程的标准输入/标准输出?

shell - 在与获取另一个脚本的同一行上定义 shell 脚本变量