Unix/Linux 脚本中 “set -e” 的作用

[root@centos5 ~]# ./testcmd           return 0
1
2
3
4
5
over
[root@centos5 ~]# vi testcmd 
[root@centos5 ~]# ./testcmd             return 1
1
2
3
4
5
[root@centos5 ~]# 
#!/bin/bash
set -e
fun()
{
for i in {1..5}
do
        echo $i
        sleep 1
done
return 1
}
fun
echo "over"


-----------------------------------------------------------

#!/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将无法执行到检查的代码。你可以使用其他的结构:

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

could be replaced with

能够被代替为

[plain]  view plain  copy
  1. command || { echo "command failed"; exit 1; }   
or

或者

[plain]  view plain  copy
  1. if ! command; then  
  2.      echo "command failed";   
  3.     exit 1;   
  4. fi   

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.

如果你有一个命令返回非0或者你对语句执行的结果不关心,那你可以使用command || true,或者你有一段很长的代码,你可以关闭错误检查(不使用set -e),但是我还是建议你保守地使用这个语句。




补充:

$ set -e

这行代码之后的任何代码,如果返回一个非0的值,那么整个脚本立即退出,官方的说明是为了防止错误出现滚雪球的现象

$ set -o 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.

可理解为:

告诉 bash 返回从右到左第一个以非0状态退出的管道命令的返回值,如果所有命令都成功执行时才返回0



    编写shell脚本没多久,对于其许多命令的具体用法还不太熟悉。最近刚好有需求,就尝试用脚本去实现。其中就有用到set -e选项。

        在用这个命令之前,也查过其功能,描述比较简单:就是当命令以非零状态退出时,则退出shell。主要作用是,当脚本执行出现意料之外的情况时,立即退出,避免错误被忽略,导致最终结果不正确。这里主要讨论的是set -e 选项的作用范围,创建如下测试文件:
[plain]  view plain  copy
  1. zhenglei@SzExdroid14:~/study/scripts$ cat run.sh  
  2. #!/bin/bash  
  3. set -e  
  4.   
  5. test_dir/set.sh  
  6. zhenglei@SzExdroid14:~/study/scripts$ cat test_dir/set.sh  
  7. #!/bin/bash  
  8. set -e  
  9.   
  10. function return1()  
  11. {  
  12.         return 1  
  13. }  
  14. function return0()  
  15. {  
  16.         return 0  
  17. }  
  18. function returnf()  
  19. {  
  20.         return $1  
  21. }  
  22.   
  23. echo "run start"  
  24. return1  
  25. if [ $? -ne 0 ];then  
  26.         echo "cmd error"  
  27. fi  
  28. echo "run end !"  

单独执行test_dir/set.sh时打印如下:

[python]  view plain  copy
  1. zhenglei@SzExdroid14:~/study/scripts$ ./test_dir/set.sh  
  2. run start  

即return1语句返回非零,立即退出执行。说明set -e 选项有效。接着将test_dir/set.sh中的set -e 屏蔽掉再执行run.sh时:

[python]  view plain  copy
  1. zhenglei@SzExdroid14:~/study/scripts$ ./run.sh  
  2. run start  
  3. cmd error  
  4. run end !  

打印和程序流程相符。说明run.sh中set -e选项在set.sh中不起作用。看似此选项的只作用于当前脚本,不作用于其所调用的脚本。如此?
将run.sh脚本修改如下,并执行:

[python]  view plain  copy
  1. zhenglei@SzExdroid14:~/study/scripts$ cat run.sh  
  2. #!/bin/bash  
  3. set -e  
  4.   
  5. . test_dir/set.sh  
  6. zhenglei@SzExdroid14:~/study/scripts$ ./run.sh  
  7. run start  

说明set -e 选项对set.sh起作用。脚本作为一个进程去描述set -e选项的范围应该是:set -e选项只作用于当前进行,不作用于其创建的子进程。
set -e 命令用法总结如下:
1. 当命令的返回值为非零状态时,则立即退出脚本的执行。
2. 作用范围只限于脚本执行的当前进行,不作用于其创建的子进程。
3. 另外,当想根据命令执行的返回值,输出对应的log时,最好不要采用set -e选项,而是通过配合exit 命令来达到输出log并退出执行的目的。

注:编译此文档时,出现行间距不一致的情况 ,编辑器也没找到可调整的菜单。结果发现shift + enter / delete 键可以比较方便的控制。

出处:https://blog.csdn.net/todd911/article/details/9954961

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值