Linux 当set-e被设置并且两个管道由&;连接时,为什么要使用bash脚本&;失败
假设我们有这样一个脚本Linux 当set-e被设置并且两个管道由&;连接时,为什么要使用bash脚本&;失败,linux,bash,pipeline,Linux,Bash,Pipeline,假设我们有这样一个脚本 #!/bin/bash set -e ls notexist && ls notexist echo still here 由于set-e而无法退出 但是 威尔。 为什么?因为正如Bash手册所说的关于set-e: The shell does not exit if the command that fails is [...] part of any command executed in a && or || list except
#!/bin/bash
set -e
ls notexist && ls notexist
echo still here
由于set-e而无法退出
但是
威尔。
为什么?因为正如Bash手册所说的关于
set-e
:
The shell does not exit if the command that fails is [...]
part of any command executed in a && or || list except the
command following the final && or ||
如果失败的命令是命令的一部分,则shell不会退出
紧跟在while或until关键字之后的列表,这是测试的一部分
在if或elif保留字之后,在
&&或| | |列出除最后&&或| | |任意命令后的命令以外的其他命令
在管道中,但在最后一个管道中,或者如果命令的返回值为
倒转
ls notexist | | ls notexist
命令终止shell,因为第二个(最后一个)ls notexist
退出失败。ls-notexist&&ls-notexist
不会终止shell,因为在第一个ls-notexist
失败且从未到达第二个(最后一个)后,“&&list”的执行将停止
顺便说一句,使用
true
和false
而不是像ls notexist
这样的特定命令进行此类测试更容易、更可靠,因为正如Bash手册所说的关于set-e
:
The shell does not exit if the command that fails is [...]
part of any command executed in a && or || list except the
command following the final && or ||
如果失败的命令是命令的一部分,则shell不会退出
紧跟在while或until关键字之后的列表,这是测试的一部分
在if或elif保留字之后,在
&&或| | |列出除最后&&或| | |任意命令后的命令以外的其他命令
在管道中,但在最后一个管道中,或者如果命令的返回值为
倒转
ls notexist | | ls notexist
命令终止shell,因为第二个(最后一个)ls notexist
退出失败。ls-notexist&&ls-notexist
不会终止shell,因为在第一个ls-notexist
失败且从未到达第二个(最后一个)后,“&&list”的执行将停止
顺便说一句,使用
true
和false
而不是bash手册中针对set-e
的特定命令(如ls notexist
)进行此类测试更容易、更可靠:
The shell does not exit if the command that fails is [...]
part of any command executed in a && or || list except the
command following the final && or ||
仪表板手册上说:
If not interactive, exit immediately if any untested command fails.
The exit status of a command is considered to be explicitly tested
if the command is used to control an if, elif, while, or until;
or if the command is the left hand operand of an “&&” or “||” operator.
对于AND测试,shell将在“左手操作数”测试期间提前停止。
因为仍然有测试,它会考虑整个命令被“测试”,因此不会中止。< /P>
对于OR测试,shell必须运行所有(两个)测试,一旦最后一个测试失败,它将得出结论认为存在未检查的错误,因此将中止
我同意这有点违反直觉。bash手册中针对
set-e
:
The shell does not exit if the command that fails is [...]
part of any command executed in a && or || list except the
command following the final && or ||
仪表板手册上说:
If not interactive, exit immediately if any untested command fails.
The exit status of a command is considered to be explicitly tested
if the command is used to control an if, elif, while, or until;
or if the command is the left hand operand of an “&&” or “||” operator.
对于AND测试,shell将在“左手操作数”测试期间提前停止。
因为仍然有测试,它会考虑整个命令被“测试”,因此不会中止。< /P>
对于OR测试,shell必须运行所有(两个)测试,一旦最后一个测试失败,它将得出结论认为存在未检查的错误,因此将中止
我同意这有点违反直觉。我喜欢破折号的解释,谢谢你发布它!很好的解释,谢谢我喜欢破折号的解释,谢谢你发布它!很好的解释,谢谢