博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
解释bash脚本中set -e与set -o pipefail的作用
阅读量:4176 次
发布时间:2019-05-26

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

man set中的解释:

       set [--abefhkmnptuvxBCEHPT] [-o option] [arg ...]

       set [+abefhkmnptuvxBCEHPT] [+o option] [arg ...]
... ...
              -e      Exit  immediately  if a pipeline (which may consist of a
                      single simple command),  a subshell command enclosed  in
                      parentheses,  or one of the commands executed as part of
                      a command list enclosed by  braces  (see  SHELL  GRAMMAR
                      above) exits with a non-zero status.  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 ││ 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 !.  A trap  on  ERR,
                      if set, is executed before the shell exits.  This option
                      applies to the shell environment and each subshell envi-
                      ronment  separately  (see  COMMAND EXECUTION ENVIRONMENT
                      above), and may cause subshells to exit before executing
                      all the commands in the subshell.
... ...

              -o option-name

                      The option-name can be one of the following:
... ...

                      pipefail

                              If  set,  the  return value of a pipeline is the
                              value of the last (rightmost)  command  to  exit
                              with  a non-zero status, or zero if all commands
                              in the pipeline exit successfully.  This  option
                              is disabled by default.
.. ...

个人理解:

set -e表示一旦脚本中有命令的返回值为非0,则脚本立即退出,后续命令不再执行;

set -o pipefail表示在管道连接的命令序列中,只要有任何一个命令返回非0值,则整个管道返回非0值,即使最后一个命令返回0.

小实验:

#!/bin/bash# testset.shecho 'disable exit on non-zero return status and pipefail track'set +eset +o pipefaila=$[1/0]|b=2echo 'return status = '$?echo 'disable exit on non-zero return status but enable pipefail track'set +eset -o pipefaila=$[1/0]|b=2echo 'return status = '$?echo 'enable exit on non-zero return status and pipefail track'set -eset -o pipefaila=$[1/0]|b=2echo 'return status = '$?
输出结果:
[root@desktop2 ~]# ./testset.sh
disable exit on non-zero return status and pipefail track
./testset.sh: line 6: 1/0: division by 0 (error token is "0")
return status = 0
disable exit on non-zero return status but enable pipefail track
./testset.sh: line 12: 1/0: division by 0 (error token is "0")
return status = 1
enable exit on non-zero return status and pipefail track
./testset.sh: line 18: 1/0: division by 0 (error token is "0")
[root@desktop2 ~]#

REF:

1. pipefail

http://petereisentraut.blogspot.com/2010/11/pipefail.html

2. linux中的set命令: "set -e" 与 "set -o pipefail"

http://blog.chinaunix.net/uid-15007890-id-3493077.html

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

你可能感兴趣的文章
【算法概论】分治算法:查找中位数
查看>>
【算法概论】分治算法:k路归并
查看>>
Python debug 一
查看>>
向量vector的初始化
查看>>
android数据存储与访问之使用pull解析器
查看>>
Android 短信模块分析(七) MMS数据库定义及结构整理
查看>>
Android 短信模块分析(八) MMS数据库表关系
查看>>
Android 图标上面添加提醒(二)使用开源UI类库 Viewbadger
查看>>
Android 图标上面添加提醒(一)使用Canvas绘制
查看>>
Android WebView加载Html右边空白问题的解决方案
查看>>
Android 仿网易新闻v3.5:上下滑动的引导页
查看>>
Android 天气预报图文字幕垂直滚动效果
查看>>
Android硬件加速
查看>>
智慧平安社区系统开发解决方案,智慧小区大数据分析平台建设
查看>>
NQI国家质量技术基础系统开发,国家质量基础设施平台建设
查看>>
nc命令用法举例
查看>>
Linux vmstat命令详解
查看>>
linux watch命令
查看>>
Linux lsof命令详解
查看>>
C/C++中字符串操作函数strcpy,strcat,strlen等
查看>>