首页 > 其他分享 >“set -e” 的作用

“set -e” 的作用

时间:2023-06-07 10:00:50浏览次数:55  
标签:use set exit 作用 command true bash

-----------------------------------------------------------
 #!/bin/bash set -e
 command 1
 command 2
 ... exit 0
 ---------------------------------------------------------- Every script you write should include set -e at the top. This tells bash that it should exit the script if any statement returns a non-true return value. The benefit of using -e is that it prevents errors snowballing into serious issues when they could have been caught earlier. Again, for readability you may want to use set -o errexit.

你写的每个脚本都应该在文件开头加上set -e,这句语句告诉bash如果任何语句的执行结果不是true则应该退出。这样的好处是防止错误像滚雪球般变大导致一个致命的错误,而这些错误本应该在之前就被处理掉。如果要增加可读性,可以使用set -o errexit,它的作用与set -e相同。



Using -e gives you error checking for free. If you forget to check something, bash will do it for you. Unfortunately it means you can't check $? as bash will never get to the checking code if it isn't zero. There are other constructs you could use:

使用-e帮助你检查错误。如果你忘记检查(执行语句的结果),bash会帮你执行。不幸的是,你将无法检查$?,因为如果执行的语句不是返回0,bash将无法执行到检查的代码。你可以使用其他的结构:



1.  
2. [ "$?"-ne 0]; then   
3.    echo "command failed";   
4.    exit 1;   
5.




could be replaced with

能够被代替为



1. || { echo "command failed"; exit 1; }   
 
  or


或者


1. ! command; then  
2.     echo "command failed";   
3.    exit 1;   
4.



What if you have a command that returns non-zero or you are not interested in its return value? You can use command || true, or if you have a longer section of code, you can turn off the error checking, but I recommend you use this sparingly.

command || true,或者你有一段很长的代码,你可以关闭错误检查(不使用set -e),但是我还是建议你保守地使用这个语句。



标签:use,set,exit,作用,command,true,bash
From: https://blog.51cto.com/u_13990003/6430018

相关文章

  • Docker for Windows 中文文档(3)——Docker Settings
    Docker设置Docker运行时,显示Docker鲸鱼。默认情况下,Docker鲸鱼图标被放置在“通知”区域中。如果隐藏,单击任务栏上的向上箭头显示。提示:您可以将鲸鱼固定在通知框外面,使其始终在任务栏上可见。为此,拖放鲸鱼图标。或者,右键单击任务栏的空白部分,选择设置,然后通过任务栏设置为通知......
  • Docker for Windows 中文文档(2)——Set up tab completion in PowerShell
    在PowerShell中设置tab完成如果您希望为Docker命令提供方便的选项卡完成,可以按如下方式安装posh-dockerPowerShell模块。1.启动“elevated”PowerShell(即以管理员身份运行)。为此,请搜索PowerShell,右键单击,然后选择以管理员身份运行。当系统询问您是否允许此应用更改您的设备时,......
  • slave 故障,用set_global 跳
    //slave故障,用set_global跳1.停止slave slavestop;2.跳 setglobalsql_slave_skip_counter=N3.开始slave slavestart 小结:  1、setglobalsql_slave_skip_counter=N中的N是指跳过N个event  2、最好记的是N被设置为1时,效果跳过下一个事务。  3、跳过第N个e......
  • mysql 多值检索 find_in_set()函数
    问题描述:有一个字段type类型,存储的值为:1,2,3,4,等这样的,要检索出里面全部含有某一个类型的值,列如3想要的结果如下:如何实现。。下面是具体的示例:+-----+-----------+|fid|type|+-----+-----------+|1|1,2,3,4,5||4|2,3,4,5|+-----+-----------+2rowsins......
  • 1_Setting Up an Ubuntu 20.04 server for deployment
     原文:https://www.codewithharry.com/blogpost/setup-ubuntu-20-04-server/  SettingUpanUbuntu20.04serverfordeploymentWhenyoufirstcreateaserverfromanyproviderlikeLinode,DigitalOcean,etc.,youwillhavetosecureitbyexecutingseveral......
  • 在开发过程中,C#中@的用法,对C#程序设计来说有不错的借鉴价值。一下介绍了四种用法。 @
    在开发过程中,C#中@的用法,对C#程序设计来说有不错的借鉴价值。一下介绍了四种用法。原文链接:https://www.cnblogs.com/likui-bookHouse/p/9109872.html在开发过程中,C#中@的用法,对C#程序设计来说有不错的借鉴价值。一下介绍了四种用法。1、@是取消字符串中的转意符。比如不加@......
  • Set up SSH for Git and Mercurial on Mac OSX/Linux
    https://confluence.atlassian.com/pages/viewpage.action?pageId=270827678http://www.worldhello.net/gotgithub/index.html......
  • 记一次 Connection reset by peer 报错修复
    写项目的时候,我自己开发了一个打包下载的接口,可以提供给用户将特定的文件打包下载的功能。但是在执行的过程中出现了报错java.io.IOException:Connectionresetbypeer因为是第一次写下载接口不知道后续我就查百度百度上给的答案是Nginx缓存不够,得取消缓存或者是增大缓存,在......
  • k8s中设置hostNetwork: true,怎么修改冲突的端口,yaml使用的是DaemonSet
    apiVersion:apps/v1kind:DaemonSetmetadata:name:cadvisornamespace:monitoringspec:selector:matchLabels:app:cAdvisortemplate:metadata:labels:app:cAdvisorspec:tolerations:#污点容忍,忽略master的......
  • k8s之hostport 在daemonset 上面暴露端口
    NodePort虽然可以给pod提供节点级别的porxy,但是如果对于一个daemonset,采用NodePort方式来申明节点端口就不这么直观了,这个时候就可以使用pod的hostport来直接在pod的节点上暴露端口。apiVersion:apps/v1kind:DaemonSetmetadata:labels:app:tools-testnam......