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.命令基本格式 命令[选项][参数] 注意:个别命令不 ...
随机推荐
- Codeforces 807 B T-Shirt Hunt
B. T-Shirt Hunt http://codeforces.com/problemset/problem/807/B time limit per test 2 seconds memory ...
- [数据库中间件]centos6.6下配置libzdb所产生的错误
1.关于gmtime_r.timegm的隐藏声明错误,从系统的time.h中复制两个函数引用到libzdb自己定义的time.h,代码如下: extern struct tm *gmtime_r (c ...
- Gmail登录(2017.10)
本文地址:http://www.cnblogs.com/veinyin/p/7646382.html 由于众所周知的原因,我们使用 Gmail 查看邮件时会有些麻烦,有时梯子不够用甚至不能查看. 比 ...
- 2017ACM暑期多校联合训练 - Team 2 1009 HDU 60563 TrickGCD (容斥公式)
题目链接 Problem Description You are given an array A , and Zhu wants to know there are how many differe ...
- JS的全局函数eval解析JSON字符串
JavaScript eval() 函数 定义和用法 eval() 函数可计算某个字符串,并执行其中的的 JavaScript 代码. 语法 eval(string) 参数 描述 string 必需. ...
- flask基础之安装和使用入门(一)
前言 Flask框架作为一个python极简化的web框架,它不像Django那样的重型,非常适合快速开发一些小型的应用.本人用flask开发了几个项目之后,慢慢研究flask底层的一些原理,开始一步 ...
- adb_usb.ini在adb找不到设备时
不能连接不上adb,如下方法解决,步骤描述不愿意看的话,直接使用本文最下面的批处理命令,方法copy粘贴到新建的bat文件里运行,或者直接粘贴到dos窗口运行. 1. 使用androidsdk目录中的 ...
- 在64位ubuntu中安装代码比较工具beyond compare
1,//从http://www.scootersoftware.com/download.php 官方地址下载 bcompare-3.3.2.14050.tar.gz 或 bcompare-4.0.7 ...
- pip安装使用详解【转】
转自:pip安装使用详解 – 运维生存时间http://www.ttlsa.com/python/how-to-install-and-use-pip-ttlsa/ pip类似RedHat里面的yum ...
- Machine Learning系列--隐马尔可夫模型的三大问题及求解方法
本文主要介绍隐马尔可夫模型以及该模型中的三大问题的解决方法. 隐马尔可夫模型的是处理序列问题的统计学模型,描述的过程为:由隐马尔科夫链随机生成不可观测的状态随机序列,然后各个状态分别生成一个观测,从而 ...