请注意:
mark@L-R910LPKW:~$ f(){ set -euo pipefail ; git statu; echo $?; } && f && echo OK ; set +euo pipefail
git: 'statu' is not a git command. See 'git --help'.
The most similar commands are
status
stage
stash
1
OK
mark@L-R910LPKW:~$
命令是
f(){ set -euo pipefail ; git statu; echo $?; } && f && echo OK ; set +euo pipefail
我的问题是 - 为什么函数
f
NOT 在运行失败后立即中止 git statu
?运行该命令清楚地显示 git statu
失败,退出代码为 1,但仍进一步继续到 echo $?
,而不是按照 set -e
的要求中止。
这在手册中有详细记录:
[当设置
时,如果失败的命令是紧随-e
或while
关键字的命令列表的一部分(until
语句中的测试的一部分),则 shell 不会退出,if
或&&
列表中执行的任何命令的一部分,最后一个||
或&&
之后的命令除外,管道中除最后一个命令之外的任何命令,或者如果命令的返回状态与||
.!
反转
如果复合命令或 shell 函数在忽略
的上下文中执行时设置-e
,则在复合命令或包含函数调用的命令完成之前,该设置不会产生任何效果。-e
您的函数确实在不忽略
-e
的上下文中中止。例如:
$ f(){ set -euo pipefail ; git statu; echo $?; }
$ ( f; echo OK )
git: 'statu' is not a git command. See 'git --help'.
The most similar commands are
status
stage
stash
$