epl*_*epl 5 unix bash shell exit-code
考虑以下 Bash 脚本:
#!/usr/bin/env bash
set -o errexit pipefail
shopt -s inherit_errexit
( echo hello ; exit 1 ) | cat
echo world
Run Code Online (Sandbox Code Playgroud)
使用5.0.17版本运行,输出如下:
hello
world
Run Code Online (Sandbox Code Playgroud)
但是,负责打印的子 shellhello会失败,并具有非零退出状态。作为管道的一部分,启用选项后pipefail,整个管道应该同样失败,并具有相同的状态(管道中的后续项当然会自行返回零状态)。因此,管道应该评估为非零状态,因为errexit(如果不是inherit_errexit的话)会提示立即终止脚本,而不会到达最终的语句 print world。
可以看出,对于未达到最终打印语句的预测并不准确。
为什么?
您只是设置了errexit设置,而不是pipefail。后面不能放多个选项-o,需要重复-o选项。其他一切都被放入位置参数中。
所以改变
set -o errexit pipefail
Run Code Online (Sandbox Code Playgroud)
到
set -o errexit -o pipefail
Run Code Online (Sandbox Code Playgroud)