本文翻译自:Stop on first error [duplicate]
Possible Duplicate: 可能重复:
Automatic exit from bash shell script on error 错误时自动退出bash shell脚本
How can I have bash stop on the first command failure, without putting stuff like this all through my code? 如何让bash在第一个命令失败时停止,而又不会在我的代码中放这样的东西呢?
some_prog || exit 1
some_other_prog || exit 1
#1楼
参考:https://stackoom.com/question/ezSK/停在第一个错误上-重复
#2楼
Maybe you want set -e
: 也许你想set -e
:
www.davidpashley.com/articles/writing-robust-shell-scripts.html#id2382181 : www.davidpashley.com/articles/writing-robust-shell-scripts.html#id2382181 :
This tells bash that it should exit the script if any statement returns a non-true return value. 这告诉bash如果有任何语句返回非真实的返回值,则应退出脚本。 The benefit of using -e is that it prevents errors snowballing into serious issues when they could have been caught earlier. 使用-e的好处是,它可以防止错误在可能早些时候被捕获时就滚入严重的问题。 Again, for readability you may want to use set -o errexit. 同样,出于可读性考虑,您可能需要使用set -o errexit。