bash文件中set -e和set -o potion-name的作用

本文详细解析了 Bash shell 中 set 命令的 -e 和 -o pipefail 选项的作用。通过实例演示了如何使用这两个选项来控制脚本在遇到错误时的行为,并解释了它们对于提高脚本健壮性的重要性。

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.


小实验:

[plain] view plain copy


  1. #!/bin/bash  
  2. # testset.sh  
  3. echo 'disable exit on non-zero return status and pipefail track'  
  4. set +e  
  5. set +o pipefail  
  6. a=$[1/0]|b=2  
  7. echo 'return status = '$?  
  8.   
  9. echo 'disable exit on non-zero return status but enable pipefail track'  
  10. set +e  
  11. set -o pipefail  
  12. a=$[1/0]|b=2  
  13. echo 'return status = '$?  
  14.   
  15. echo 'enable exit on non-zero return status and pipefail track'  
  16. set -e  
  17. set -o pipefail  
  18. a=$[1/0]|b=2  
  19. echo '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

Baidu Button BEGIN







Baidu Button END 172.16.140.13 Baidu Button BEGIN Baidu Button END

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值