本文翻译自:Automatic exit from bash shell script on error [duplicate]
This question already has an answer here: 这个问题已经在这里有了答案:
I've been writing some shell script and I would find it useful if there was the ability to halt the execution of said shell script if any of the commands failed. 我一直在写一些shell脚本,如果有任何命令失败的话,如果能够停止执行该shell脚本,我会发现它很有用。 See below for an example: 参见以下示例:
#!/bin/bash
cd some_dir
./configure --some-flags
make
make install
So in this case if the script can't change to the indicated directory then it would certainly not want to do a ./configure afterwards if it fails. 因此,在这种情况下,如果脚本无法更改到指示的目录,那么如果失败,它肯定不会在之后执行./configure。
Now I'm well aware that I could have an if check for each command (which I think is a hopeless solution), but is there a global setting to make the script exit if one of the commands fails? 现在我很清楚,我可以对每个命令进行if检查(我认为这是一个绝望的解决方案),但是是否存在全局设置,如果其中一个命令失败,则退出脚本?
#1楼
参考:https://stackoom.com/question/C2sK/错误时自动退出bash-shell脚本-重复
#2楼
Here is how to do it: 这是操作方法:
#!/bin/sh
abort()
{
echo >&2 '
***************
*** ABORTED ***
***************
'
echo "An error occurred. Exiting..." >&2
exit 1
}
trap 'abort' 0
set -e
# Add your script below....
# If an error occurs, the abort() function will be called.
#----------------------------------------------------------
# ===> Your script goes here
# Done!
trap : 0
echo >&2 '
************
*** DONE ***
************
'
#3楼
One idiom is: 一种成语是:
cd some_dir && ./configure --some-flags && make && make install
I realize that can get long, but for larger scripts you could break it into logical functions. 我意识到这可能会很长,但是对于较大的脚本,您可以将其分解为逻辑功能。
#4楼
I think that what you are looking for is the trap
command: 我认为您正在寻找的是trap
命令:
trap command signal [signal ...]
For more information, see this page . 有关更多信息,请参阅此页面 。
Another option is to use the set -e
command at the top of your script - it will make the script exit if any program / command returns a non true value. 另一个选择是在脚本顶部使用set -e
命令-如果任何程序/命令返回非真值,它将使脚本退出。
#5楼
To exit the script as soon as one of the commands failed, add this at the beginning: 要在其中一个命令失败后立即退出脚本,请在开头添加以下内容:
set -e
This causes the script to exit immediately when some command that is not part of some test (like in a if [ ... ]
condition or a &&
construct) exits with a non-zero exit code. 当不属于某些测试的某些命令(例如,在if [ ... ]
条件或&&
构造中)不使用非零退出代码退出时,这将导致脚本立即退出。
#6楼
Use the set -e
builtin: 使用内置的set -e
:
#!/bin/bash
set -e
# Any subsequent(*) commands which fail will cause the shell script to exit immediately
Alternatively, you can pass -e
on the command line: 或者,您可以在命令行上传递-e
:
bash -e my_script.sh
You can also disable this behavior with set +e
. 您也可以使用set +e
禁用此行为。
You may also want to employ all or some of the the -e
-u
-x
and -o pipefail
options like so: 您可能还希望使用-e
-u
-x
和-o pipefail
选项的全部或部分,如下所示:
set -euxo pipefail
-e
exits on error, -u
errors on undefined variables, and -o (for option) pipefail
exits on command pipe failures. -e
在错误时退出, -u
错误在未定义变量上退出, -o (for option) pipefail
在命令管道失败时退出。 Some gotchas and workarounds are documented well here . 在这里很好地记录了一些陷阱和解决方法。
(*) Note: (*) 注意:
The shell does not exit if the command that fails is part of the command list immediately following a while or until keyword, part of the test following the if or elif reserved words, part of any command executed in a && or || 壳不退出如果失败的命令是紧跟在同时或直到关键字,测试的部分继如果或elif的保留字的命令列表的一部分,在一个执行的任何命令或&&的一部分|| list except the command following the final && or || 列表,最后一个&&或||之后的命令除外 , any command in a pipeline but the last, or if the command's return value is being inverted with ! ,管道中除最后一条命令外的任何命令,或者如果命令的返回值正使用!
(from man bash
) (来自man bash
)