博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
错误时自动退出bash shell脚本[重复]
阅读量:2288 次
发布时间:2019-05-09

本文共 3468 字,大约阅读时间需要 11 分钟。

本文翻译自:

This question already has an answer here: 这个问题已经在这里有了答案:

  • 9 answers 9个答案

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楼

参考:


#2楼

Here is how to do it: 这是操作方法:

#!/bin/shabort(){    echo >&2 '****************** ABORTED ******************'    echo "An error occurred. Exiting..." >&2    exit 1}trap 'abort' 0set -e# Add your script below....# If an error occurs, the abort() function will be called.#----------------------------------------------------------# ===> Your script goes here# Done!trap : 0echo >&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 . 有关更多信息,请参阅 。

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 builtin: 使用内置的 :

#!/bin/bashset -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 . 很好地记录了一些陷阱和解决方法。

(*) 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

转载地址:http://bicnb.baihongyu.com/

你可能感兴趣的文章
IO流 合并流 SequenceInputStream
查看>>
IO流切割文件
查看>>
IO流-对象序列化操作流
查看>>
io流-多线程连接管道流
查看>>
RandomAccessFile可实现数据的分段写入也就是多线程下载
查看>>
DataInputStream与DataOutputStream用于操作基本数据类型的数据的流对象
查看>>
ByteArrayStream用于操作字节数组的流对象
查看>>
IO流-字符编码表转换示例
查看>>
IO流-转换流的字符编码转换-ISO-8859-1和utf-8和GBK互转
查看>>
基于AWS的自动化部署实践
查看>>
同时使用ColorKey以及顶点Alpha效果
查看>>
Cisco SIP支持的标准
查看>>
MySQL:MySQL日期数据类型、MySQL时间类型使用总结
查看>>
理解MySQL——并行数据库与分区(Partition)
查看>>
Linux查看系统开机时间
查看>>
ssh批量登录并执行命令(python实现)
查看>>
Python模块之---Pexpect
查看>>
Python的Pexpect详解
查看>>
Python模块:optparse 处理命令行参数
查看>>
supervisor安装配置与使用
查看>>