PIPESTATUS(bash) + pipefail(ksh)
I have two processes foo
and bar
, connected with a pipe:
$ foo | bar
bar
always exits 0; I'm interested in the exit code of foo
. Is there any way to get at it?
--
There are 3 common ways of doing this:
Pipefail
The first way is to set the pipefail
option (ksh
, zsh
or bash
). This is the simplest and what it does is basically set the exit status $?
to the exit code of the last program to exit non-zero (or zero if all exited successfully).
$ false | true; echo $?
0
$ set -o pipefail
$ false | true; echo $?
1
$PIPESTATUS
Bash also has an array variable called $PIPESTATUS
($pipestatus
in zsh
) which contains the exit status of all the programs in the last pipeline.
$ true | true; echo "${PIPESTATUS[@]}"
0 0
$ false | true; echo "${PIPESTATUS[@]}"
1 0
$ false | true; echo "${PIPESTATUS[0]}"
1
$ true | false; echo "${PIPESTATUS[@]}"
0 1
You can use the 3rd command example to get the specific value in the pipeline that you need.
Separate executions
This is the most unwieldy of the solutions. Run each command separately and capture the status
$ OUTPUT="$(echo foo)"
$ STATUS_ECHO="$?"
$ printf '%s' "$OUTPUT" | grep -iq "bar"
$ STATUS_GREP="$?"
$ echo "$STATUS_ECHO $STATUS_GREP"
0 1
PIPESTATUS(bash) + pipefail(ksh)的更多相关文章
- Unix Shells: Bash, Fish, Ksh, Tcsh, Zsh
Hyperpolyglot Unix Shells: Bash, Fish, Ksh, Tcsh, Zsh grammar | quoting and escaping | charactersvar ...
- bash与ksh数组使用
区别: bash与ksh在数组的使用中,最大的不同在于数组的定义. bash: declare -a arrayname ksh:set -A arrayname 其实,数组不用非要定义,在赋值的时候 ...
- ash, bash, ksh, csh, zsh
/bin/bash (就是 Linux 预设的 shell, 是现在很多Linux的发行版中默认的shell,综合了其他shell的很多优点.)/bin/ksh (Kornshell 由 AT& ...
- Bash 会清空从父进程继承来的 OLDPWD
即便 Bash 没有从父进程继承任何的环境变量,Bash 自己也会创建三个环境变量,分别是: $ env -i bash -c export declare -x OLDPWD declare -x ...
- 快速学习Bash
作者:Vamei 出处:http://www.cnblogs.com/vamei 严禁转载. Shell是Linux下经典的文本互动方式,而Bash是现在最常用的一种Shell.我在这里总结了Bash ...
- bash中的管道和重定向
管道 管道命令操作符是:”|”,它仅能处理经由前面一个指令传出的正确输出信息,也就是 standard output 的信息,对于 stdandarderror 信息没有直接处理能力.然后,传递给下一 ...
- Zsh vs. Bash不完全对比解析,zsh是一种更强大的被成为“终极”的Shell
https://www.zhihu.com/question/21418449 Mort | Zsh vs. Bash:不完全对比解析(1) 2014-10-07 bdpqlxz Zsh和B ...
- bash5.0参考手册
Bash Reference Manual a.summary-letter { text-decoration: none } blockquote.indentedblock { margin-r ...
- Linux学习记录
---恢复内容开始--- linux与unix的关系 linux是借鉴了unix设计思想,也称linux位类unix系统. Linux常用命令 1.命令基本格式 命令[选项][参数] 注意:个别命令不 ...
随机推荐
- uva 557 Burger
https://vjudge.net/problem/UVA-557 题意: n个人,n/2个牛肉煲,n/2个鸡肉堡 每次抛硬币,根据正反决定每个人吃什么汉堡 如果某一个汉堡被选完了,就不抛了 问最后 ...
- Python学习笔记(四十八)POP3收取邮件
收取邮件就是编写一个MUA作为客户端,从MDA把邮件获取到用户的电脑或者手机上.收取邮件最常用的协议是POP协议,目前版本号是3,俗称POP3. Python内置一个poplib模块,实现了POP3协 ...
- javascript操作对象的方法
with 确定某个对象的作用区域,在with代码段内的次对象的属性或方法可以直接使用. 例: //比如stu中有name,age属性和walk方法 with(stu) { alert(name+&qu ...
- JavaScript事件和方法
单击一个超链接触发事件 1.用a标签的onclick <a href="#" onclick="js代码"> 这种写法呢,存在一种弊端,就是点击后会 ...
- laravel后台返回ajax数据
后台模式: $array = array('msg'=>'添加失败!','status'=>'false'); return json_encode($array); 前台显示: $.aj ...
- 从INT_MAX和INT_MIN看补码
刷一道题的时候遇到INT_MAX和INT_MIN的问题,有些东西忘了,梳理一下. INT_MAX为2147483647,INT_MIN为-2147483648,为什么MIN的绝对值比MAX多1呢,因为 ...
- 【leetcode 简单】第二题 反转整数
给定一个 32 位有符号整数,将整数中的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: 21 注意: 假 ...
- U盘出现大量乱码文件,并且不能彻底删除
问题如图所示: 问题出现原因:不正常的插拔等情况造成的,导致U盘的文件分配表错乱了 解决方法:参考http://bbs.cfanclub.net/thread-405004-1-1.html 运行ch ...
- 35、def func(a,b=[]) 这种写法有什么坑?
那我们先通过程序看看这个函数有什么坑吧! def func(a,b=[]): b.append(a) print(b) func(1) func(1) func(1) func(1) 看下结果 [1] ...
- python3爬虫.1.简单的网页爬虫
此为记录下我自己的爬虫学习过程. 利用url包抓取网页 import urllib.request #url包 def main(): url = "http://www.douban.co ...