我有一个Bash shell脚本,它调用许多命令。
如果任何命令返回非零值,我想让shell脚本自动退出并返回值为1。
如果不显式地检查每个命令的结果,这可能吗?
例如,
dosomething1
if [[ $? -ne 0 ]]; then
exit 1
fi
dosomething2
if [[ $? -ne 0 ]]; then
exit 1
fi
我有一个Bash shell脚本,它调用许多命令。
如果任何命令返回非零值,我想让shell脚本自动退出并返回值为1。
如果不显式地检查每个命令的结果,这可能吗?
例如,
dosomething1
if [[ $? -ne 0 ]]; then
exit 1
fi
dosomething2
if [[ $? -ne 0 ]]; then
exit 1
fi
当前回答
像这样的表达式
dosomething1 && dosomething2 && dosomething3
当其中一个命令返回非零值时将停止处理。例如,下面的命令永远不会打印"done":
cat nosuchfile && echo "done"
echo $?
1
其他回答
示例中的if语句是不必要的。就像这样做:
dosomething1 || exit 1
如果你采纳了Ville Laurikari的建议并使用set -e,那么对于某些命令你可能需要使用这个:
dosomething || true
|| true将使命令管道即使在命令失败时也具有真实的返回值,因此-e选项不会终止脚本。
像这样的表达式
dosomething1 && dosomething2 && dosomething3
当其中一个命令返回非零值时将停止处理。例如,下面的命令永远不会打印"done":
cat nosuchfile && echo "done"
echo $?
1
如果你需要在退出时进行清理,你也可以使用'trap'和伪信号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
或者,特别是当你使用"set -e"时,你可以捕获EXIT;然后,当脚本因为任何原因退出时,包括正常结束、中断、由-e选项导致的退出等等,都会执行陷阱。
#!/bin/bash -e
应该足够了。
运行时带-e或者在顶部设置-e。
再看看set -u。