简体   繁体   English

在bash中使用-e时,传递命令之前的感叹号仍然不会导致脚本失败

[英]when using -e in bash, exclamation mark before a passing command still doesn't cause the script to fail

I thought the following script would just print 'hello' and then exit with a '1' 我认为以下脚本只会打印'hello'然后以'1'退出

#!/bin/bash -e

! echo hello
echo world

However, it prints 然而,它打印

hello
world

and exits with a 0 并以0退出

the following script exits with a 1: 以下脚本以1退出:

#!/bin/bash -e

! echo hello

and so does the following 以下情况也是如此

#!/bin/bash -e

echo hello | grep world
! echo hello
echo world

but for some reason the -e option doesn't manage to fail the script when a command returns a failing exit code due to a ! 但由于某种原因,当命令返回由于a而导致失败的退出代码时,-e选项无法使脚本失败。 half way through. 中途。 Can anybody offer an explanation that will make me feel better about this? 任何人都可以提供一个解释,让我感觉更好吗?

http://www.gnu.org/software/bash/manual/bashref.html#The-Set-Builtin http://www.gnu.org/software/bash/manual/bashref.html#The-Set-Builtin

The -e command will fail the script for any command returning a non-zero code, except for some cases, like while loops, or commands returning 0 but inverted with the ! 对于任何返回非零代码的命令,-e命令将使脚本失败,除了某些情况,例如while循环,或返回0但是反转的命令! option. 选项。 Thus your ! echo hello 这样你! echo hello ! echo hello will return 1 (0 inverted by the ! ) but the -e option won't fail. ! echo hello将返回1(0反转为! )但-e选项不会失败。

But if you make a exit status 42 , you will have your script failed. 但是如果你进入exit status 42 ,你的脚本就会失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM