Linux 如果任何命令返回非零值,是否中止 shell 脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/821396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Aborting a shell script if any command returns a non-zero value?
提问by Jin Kim
I have a Bash shell script that invokes a number of commands. I would like to have the shell script automatically exit with a return value of 1 if any of the commands return a non-zero value.
我有一个调用许多命令的 Bash shell 脚本。如果任何命令返回非零值,我希望 shell 脚本自动退出,返回值为 1。
Is this possible without explicitly checking the result of each command?
如果不明确检查每个命令的结果,这可能吗?
e.g.
例如
dosomething1
if [[ $? -ne 0 ]]; then
exit 1
fi
dosomething2
if [[ $? -ne 0 ]]; then
exit 1
fi
采纳答案by Ville Laurikari
Add this to the beginning of the script:
将此添加到脚本的开头:
set -e
This will cause the shell to exit immediately if a simple command exits with a nonzero exit value. A simple command is any command not part of an if, while, or until test, or part of an && or || list.
如果一个简单的命令以非零退出值退出,这将导致 shell 立即退出。一个简单的命令是不属于 if、while 或 until 测试的一部分,或者不属于 && 或 || 的任何命令 列表。
See the bash(1) man pageon the "set" internal command for more details.
有关更多详细信息,请参阅“set”内部命令上的bash(1) 手册页。
I personally start almost all shell scripts with "set -e". It's really annoying to have a script stubbornly continue when something fails in the middle and breaks assumptions for the rest of the script.
我个人以“set -e”开始几乎所有的 shell 脚本。当中间出现故障并破坏脚本其余部分的假设时,让脚本顽固地继续下去真的很烦人。
回答by gabor
An expression like
像这样的表达
dosomething1 && dosomething2 && dosomething3
will stop processing when one of the commands returns with a non-zero value. For example, the following command will never print "done":
当其中一个命令返回非零值时,将停止处理。例如,以下命令永远不会打印“done”:
cat nosuchfile && echo "done"
echo $?
1
回答by lumpynose
Run it with -e
or set -e
at the top.
使用-e
或set -e
在顶部运行它。
Also look at set -u
.
还看看set -u
。
回答by Zan Lynx
The if statements in your example are unnecessary. Just do it like this:
您示例中的 if 语句是不必要的。只需这样做:
dosomething1 || exit 1
If you take Ville Laurikari's advice and use set -e
then for some commands you may need to use this:
如果您接受 Ville Laurikari 的建议并使用set -e
then 对于某些命令,您可能需要使用以下命令:
dosomething || true
The || true
will make the command pipeline have a true
return value even if the command fails so the the -e
option will not kill the script.
即使命令失败,这|| true
也会使命令管道具有true
返回值,因此该-e
选项不会终止脚本。
回答by fholo
If you have cleanup you need to do on exit, you can also use 'trap' with the pseudo-signal ERR. This works the same way as trapping INT or any other signal; bash throws ERR if any command exits with a nonzero value:
如果您需要在退出时进行清理,您还可以使用带有伪信号 ERR 的“陷阱”。这与捕获 INT 或任何其他信号的工作方式相同;如果任何命令以非零值退出,bash 将抛出 ERR:
# Create the trap with
# trap COMMAND SIGNAME [SIGNAME2 SIGNAME3...]
trap "rm -f /tmp/$MYTMPFILE; exit 1" ERR INT TERM
command1
command2
command3
# Partially turn off the trap.
trap - ERR
# Now a control-C will still cause cleanup, but
# a nonzero exit code won't:
ps aux | grep blahblahblah
Or, especially if you're using "set -e", you could trap EXIT; your trap will then be executed when the script exits for any reason, including a normal end, interrupts, an exit caused by the -e option, etc.
或者,特别是如果您使用“set -e”,您可能会陷入 EXIT;当脚本出于任何原因退出时,您的陷阱将被执行,包括正常结束、中断、由 -e 选项引起的退出等。
回答by Mark Edgar
The $?
variable is rarely needed. The pseudo-idiom command; if [ $? -eq 0 ]; then X; fi
should always be written as if command; then X; fi
.
$?
很少需要该变量。伪习语command; if [ $? -eq 0 ]; then X; fi
应始终写为if command; then X; fi
.
The cases where $?
is required is when it needs to be checked against multiple values:
需要的情况$?
是需要针对多个值进行检查时:
command
case $? in
(0) X;;
(1) Y;;
(2) Z;;
esac
or when $?
needs to be reused or otherwise manipulated:
或$?
需要重用或以其他方式操作时:
if command; then
echo "command successful" >&2
else
ret=$?
echo "command failed with exit code $ret" >&2
exit $ret
fi
回答by leonbloy
To add to the accepted answer:
添加到接受的答案:
Bear in mind that set -e
sometimes is not enough, specially if you have pipes.
请记住,set -e
有时这还不够,特别是如果您有管道。
For example, suppose you have this script
例如,假设你有这个脚本
#!/bin/bash
set -e
./configure > configure.log
make
... which works as expected: an error in configure
aborts the execution.
...按预期工作:configure
中止执行时出错。
Tomorrow you make a seemingly trivial change:
明天你做一个看似微不足道的改变:
#!/bin/bash
set -e
./configure | tee configure.log
make
... and now it does not work. This is explained here, and a workaround (Bash only) is provided:
......现在它不起作用。此处对此进行了解释,并提供了一种解决方法(仅限 Bash):
#!/bin/bash set -e set -o pipefail ./configure | tee configure.log make
回答by Malina
just throwing in another one for reference since there was an additional question to Mark Edgars input and here is an additional example and touches on the topic overall:
只是抛出另一个供参考,因为 Mark Edgars 的输入还有一个额外的问题,这里是一个额外的例子,涉及整个主题:
[[ `cmd` ]] && echo success_else_silence
which is the same as cmd || exit errcode
as someone showed.
这cmd || exit errcode
和有人展示的一样。
eg. I want to make sure a partition is unmounted if mounted:
例如。如果已安装,我想确保卸载分区:
[[ `mount | grep /dev/sda1` ]] && umount /dev/sda1
回答by Baligh Uddin
#!/bin/bash -e
should suffice.
应该足够了。