错误时自动退出bash shell脚本[重复]

本文翻译自: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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值