两种方法:
1 set -e
在脚本开始的位置写:
set -e
通过帮助信息,可以知道,set -e
意味着当某一条命令返回非零时,脚本被中断。
$ help set
-e Exit immediately if a command exits with a non-zero status.
2 exit n
使用exit n
,例如:
if [ $? -ne 0 ]; then
exit 1
fi
参考
https://unix.stackexchange.com/questions/309339/how-to-exit-a-shell-script-if-one-part-of-it-fails
网友评论