错误时自动退出bash shell脚本[重复]
本文翻译自:Automatic exit from bash shell script on error [duplicate]
This question already has an answer here: 这个问题已经在这里有了答案:
- Aborting a shell script if any command returns a non-zero value? 是否终止任何命令返回非零值的shell脚本? 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楼
参考:https://stackoom.com/question/C2sK/错误时自动退出bash-shell脚本-重复
#2楼
Here is how to do it: 这是操作方法:
#!/bin/shabort()
{echo >&2 '
***************
*** ABORTED ***
***************
'echo "An error occurred. Exiting..." >&2exit 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 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
)
错误时自动退出bash shell脚本[重复]相关推荐
- Linux VPS服务器根据CPU负载及内存占用自动重启的bash shell脚本
Linux VPS服务器根据CPU负载及内存占用自动重启的bash shell脚本 288月2011 0 主要用于监控 linux 服务器负载及内存占用,如 MySQl.php-fpm,当负载或内存占 ...
- Linux——Bash Shell脚本 for循环
1.创建和执行Bash Shell脚本 (1)借助Bash Shell环境和脚本编写功能,将Linux命令与shell脚本组合在一起,从而轻松的解决重复而困难的实际问题,Bash shell脚本最简单 ...
- Lvs别样的自动部署监控shell脚本
Lvs别样的自动部署监控shell脚本 l 脚本功能: l 实验环境图: l 具体脚本: l 结果验证: l 参考资料: 先申明,本文现在已经在我公司的测试环境和生产测试环境使用.正式环境请用ke ...
- linux shell 执行目录,bash shell脚本执行的几种方法
bash shell 脚本执行的方法有多种,本文作一个总结,供大家学习参考. 假设我们编写好的shell脚本的文件名为hello.sh,文件位置在/data/shell目录中并已有执行权限. 方法一: ...
- BASH SHELL 脚本基础
什么是shell Linux系统的shell作为操作系统的外壳,为用户提供使用操作系统的接口.它是命令语言.命令解释程序及程序设计语言的统称. shell是用户和Linux内核之间的接口程序, ...
- linux如何调试脚本文件目录,如何在Linux下调试Bash Shell脚本的方法
新手写了一个 hello world 小脚本,如何能调试运行在 Linux 或者类 UNIX 的系统上的 bash shell 脚本呢? 这是 Linux / Unix 系统管理员或新用户最常问的问题 ...
- linux shell 一行 for,BASH shell脚本回显到同一行输出
我有一个简单的BASH shell脚本,它检查curl命令的HTTP响应代码. 逻辑很好,但我坚持"简单地"打印出"输出". 我正在使用GNU bash,版本3 ...
- Linux shell 对话框,如何在 Bash Shell 脚本中显示对话框
原标题:如何在 Bash Shell 脚本中显示对话框 这个教程给出几个如何使用类似zenity和whiptail的工具在Bash Shell 脚本中提供消息/对话框的例子.使用这些工具,你的脚本能够 ...
- shell bash脚本_如何在Windows 10上创建和运行Bash Shell脚本
shell bash脚本 With the arrival of Windows 10's Bash shell, you can now create and run Bash shell scri ...
最新文章
- php js获取file,PHP file_get_contents 读取js脚本的问题
- 电气实现:蒙特卡洛法 模拟多台电动汽车有序充放电负荷和(python实现)
- 防止P2P终结者的方法
- php连接mysql并读取数据
- html如何查看文档,查看文档
- php 姓氏表,PHP拆分姓名中的姓氏和名字函数
- 正则-Strip函数
- C语言中连续两个printf,在C中两个连续的printf()调用的奇怪行为
- PostGreSql工作笔记004---PostGreSql修改密码_windows和linux下修改
- Python学习-07-面向对象编程初级
- [Jobdu] 题目1037:Powerful Calculator
- 大规模定制生产模式及其关键技术
- 加载mysql的jdbc驱动_JDBC驱动加载
- Linux安装DBLE
- 调整SumatraPDF暗黑模式
- 抖音数据 - 网民评论数据采集,分析
- 懂技术/ 更应该了解社会 /让社会变好 中国社会学家 周孝正经典语录
- 职业生涯规划需要考虑的三大要点
- STM32 GPIO 配置之ODR, BSRR, BRR 详解
- Eclipse 安装 Jrebel插件
热门文章
- 我的配置(vc可以禁止控制台输出)
- LinkedList 使用巩固及图解
- 如果你现在学Android---学习使用Kotlin进行Android开发
- css 中文字旋转,css
- oauth2 单点登录_Spring Security Oauth2和Spring Boot实现单点登录
- 名片DIY官方认证代码_【教程】DIYQQ动态名片
- BlendMode颜色混合模式枚举值
- java方法重载编程_学java教程之普通方法重载
- master-worker常驻型程序代码修改哪些需要重启master或者worker
- VS2010/MFC编程入门之十七(对话框:文件对话框)