摘要:本文主要向大家介绍了Linux运维知识之linux中的set -e 与set -o pipefail,通过具体的内容向大家展现,希望对大家学习Linux运维知识有所帮助。
本文主要向大家介绍了Linux运维知识之linux中的set -e 与set -o pipefail,通过具体的内容向大家展现,希望对大家学习Linux运维知识有所帮助。
1、set -e
"Exit immediately if a simple command exits with a non-zero status."
在“set -e”之后出现的代码,一旦出现返回值非零,整个脚本就会立即退出。
2、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."
在这个设置执行后,其后面的代码,包括管道命令的返回值,为最后一个非零的命令的返回值,或者当管道内的所有命令都执行成功后返回零。
如下例子所示:
在没有设置set -o pipifail时
#!/bin.bash
# there is no a.test,but have b.test
cat a.test
echo $?
cat b.test
echo $?
cat b.test | echo "hi"
echo $?cat a.test | echo "hi"echo $?
执行结果如下:
linux-UMLhEm:/home/test/shell # sh -x tst.sh
+ cat a.test
cat: a.test: No such file or directory
+ echo 1
1
+ cat b.test
----this is a test-----
+ echo 0
0
+ cat b.test
+ echo hi
hi
+ echo 0
0
+ cat a.test
+ echo hi
hi
cat: a.test: No such file or directory
+ echo 0
0
可以看到在执行 cat a.test | echo "hi" 时,返回的是最右边命令执行的结果。
下面设置set -o pipeline,示例如下:
set -o pipefail
cat b.test | echo "hi"
echo $?
cat a.test | echo "hi"
echo $?
输出结果如下:
+ set -o pipefail
+ cat b.test
+ echo hi
hi
+ echo 141
141
+ cat a.test
+ echo hi
hi
cat: a.test: No such file or directory
+ echo 1
1
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注系统运维Linux频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号