且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在bash脚本输出中捕获退出代码以显示并登录

更新时间:2023-12-05 19:56:28

使用数组PIPESTATUS:

Use array PIPESTATUS:

Python script.py 2>&1 | tee /logfile.log
ret="${PIPESTATUS[0]}"
if [[ "$ret" -ne "0" ]]; then
  exit "$ret"
fi

来自man bash:

PIPESTATUS:一个数组变量(请参见下面的数组),其中包含最 最近执行的前台管道(可能仅包含一个命令).

PIPESTATUS: An array variable (see Arrays below) containing a list of exit status values from the processes in the most- recently-executed foreground pipeline (which may contain only a single command).