[转帖]linux中的set -e 与set -o pipefail
https://www.cnblogs.com/xingmuxin/p/8431970.html
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时
|
1
2
3
4
5
6
7
8
9
|
#!/bin.bash# there is no a.test,but have b.testcat a.testecho $?cat b.testecho $?cat b.test | echo "hi"echo $?<br><br>cat a.test | echo "hi"<br>echo $? |
执行结果如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
linux-UMLhEm:/home/test/shell # sh -x tst.sh+ cat a.testcat: a.test: No such file or directory+ echo 11+ cat b.test----this is a test-----+ echo 00+ cat b.test+ echo hihi+ echo 00+ cat a.test+ echo hihicat: a.test: No such file or directory+ echo 00 |
可以看到在执行 cat a.test | echo "hi" 时,返回的是最右边命令执行的结果。
下面设置set -o pipeline,示例如下:
|
1
2
3
4
5
|
set -o pipefailcat b.test | echo "hi"echo $?cat a.test | echo "hi"echo $? |
输出结果如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
|
+ set -o pipefail+ cat b.test+ echo hihi+ echo 141141+ cat a.test+ echo hihicat: a.test: No such file or directory+ echo 11 |
[转帖]linux中的set -e 与set -o pipefail的更多相关文章
- [转帖]Linux中的15个基本‘ls’命令示例
Linux中的15个基本‘ls’命令示例 https://linux.cn/article-5109-1.html ls -lt 和 ls -ltr 来查看文件新旧顺序. list time rese ...
- [转帖]Linux中buff/cache内存占用过高解决办法
Linux中buff/cache内存占用过高解决办法 https://www.cnblogs.com/rocky-AGE-24/p/7629500.html /proc/sys/vm/drop_cac ...
- [转帖]Linux中的find(-atime、-ctime、-mtime)指令分析
Linux中的find(-atime.-ctime.-mtime)指令分析 https://www.cnblogs.com/zhangjinjin01/p/5505970.html https://w ...
- [转帖]Linux 中的零拷贝技术,第 2 部分
Linux 中的零拷贝技术,第 2 部分 https://www.ibm.com/developerworks/cn/linux/l-cn-zerocopy2/index.html Linux 中 ...
- [转帖]Linux 中的零拷贝技术,第 1 部分
Linux 中的零拷贝技术,第 1 部分 https://www.ibm.com/developerworks/cn/linux/l-cn-zerocopy1/index.html 引言 传统的 ...
- [转帖]Linux中awk工具的使用
Linux中awk工具的使用 2018年10月09日 17:26:20 谢公子 阅读数 2170更多 分类专栏: linux系统安全 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权 ...
- [转帖]linux中systemctl详细理解及常用命令
linux中systemctl详细理解及常用命令 2019年06月28日 16:16:52 思维的深度 阅读数 30 https://blog.csdn.net/skh2015java/article ...
- linux中的set -e 与set -o pipefail
1.set -e "Exit immediately if a simple command exits with a non-zero status." 在“set -e”之后出 ...
- [转帖]NotePad++编辑Linux中的文件
NotePad++编辑Linux中的文件 https://blog.csdn.net/chengqiuming/article/details/78882692 原作者 未经允许不允许转帖 加密自己参 ...
- 转帖:对linux中半增加半连接数量和防止服务器被dos攻击
.增大队列SYN最大半连接数 在Linux中执行命令"sysctl -a|grep net.ipv4.tcp_max_syn_backlog",在返回的"net.ipv4 ...
随机推荐
- Shell脚本实践总结
对比大小 符号用法:(必须使用双括号) < 小于 (( "$a" < "$b" )) <= 小于等于 (( "$a&q ...
- DVWA Brute Force(暴力破解)全等级
Brute Force(暴力破解) 目录: Brute Force(暴力破解) 1.Low 2.Medium 3.High 方法1--Burp爆破 方法2--Python脚本爆破 4.Impossib ...
- Blazor入门100天 : 身份验证和授权之 OpenID 与 OAuth2
目录: OpenID 与 OAuth2 基础知识 Blazor wasm Gitee 码云登录 Blazor wasm GitHub 登录 Blazor wasm Google 登录 Blazor w ...
- Spring系列:基于Spring-Jdbc实现事务
目录 一.事务基本概念 二.编程式事务 三.声明式事务 前期准备 四.基于注解的声明式事务 @Transactional注解标识的位置 事务属性:只读 事务属性:超时 事务属性:回滚策略 事务属性:隔 ...
- Karmada 结合 coreDNS 插件实现跨集群统一域名访问
本文分享自华为云社区<Karmada 结合 coreDNS 插件实现跨集群统一域名访问>,作者:云容器大未来 . 在多云与混合云越来越成为企业标配的今天,服务的部署和访问往往不在一个 K8 ...
- 知识+AI融合创新探索,华为云论文被AI顶级学术期刊IEEE TPAMI接受
摘要:通过利用物体类别之间存在的层级关系约束,自动学习从数据中抽取识别不同类别的规则,一方面对模型的预测过程进行解释,另一方面也提供了一条引入人工先验知识的可行途径. 前言 受益于深度学习技术的突破, ...
- 云小课|3种常用Git工作流推荐
本文分享自华为云社区<[云小课]应用平台第44课 常用Git工作流推荐>,作者: 应用万花筒. . 1. Git工作流-动静有法 简单来说,工作流就是开发团队预置的开发流程和解决问题时使用 ...
- 实例讲解FusionInsight MRS RTD 实时决策引擎在医保行业应用
摘要: 通过引入FusionInsight RTD实时决策系统,实现医保费用事前预防.事中控制.事后审核的全流程管理 本文分享自华为云社区<FusionInsight MRS RTD 实时决策引 ...
- DBA:介里有你没有用过的“CHUAN”新社区版本Redis6.0
摘要:华为云DCS Redis 6.0社区版带来了极致性能.功能全面.可靠性强.性价比高的云服务,并且完全兼容开源Redis,客户端无需修改代码,开通后即可使用,使企业完全无需后顾之忧就能享受到业务响 ...
- 浅谈QUIC协议原理与性能分析及部署方案
之前写过<http1.0 与 http1.1的区别> 与 <再谈HTTP2性能提升之背后原理-HTTP2历史解剖>,QUIC协议,现在nginx官方也即将支持.所以还是得跟上时 ...