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.命令基本格式 命令[选项][参数] 注意:个别命令不 ...
随机推荐
- ACM-ICPC2018 青岛赛区网络预赛-B- Red Black Tree
题目描述 BaoBao has just found a rooted tree with n vertices and (n-1) weighted edges in his backyard. A ...
- HDU 1452 欧拉定理
让你求$2004^x$所有因子之和,因子之和函数是积性函数$\sigma(n)=\sum_{d|n}d=\prod_{i=0}^{m}(\sum_{j=0}^{k_i}{P_i^{j}})$可用二项式 ...
- CF839 D 容斥
求$gcd>1$的所有$gcd(a_i,a_{i+1}…a_{n})*(n-i+1)$的和 首先先标记所有出现的数.从高到低枚举一个数k,记录它的倍数出现次数cnt,那么当前所有组合的答案就是$ ...
- DevExpress 常用命令包括导出-打印-打印预览等
3.表格打印也是最常见的,打印代码如下: PrintingSystem ps = null; DevExpress.XtraPrinting.PrintableComponentLink link = ...
- 《JavaScript 实战》:实现图片幻滑动展示效果
滑动展示效果主要用在图片或信息的滑动展示,也可以设置一下做成简单的口风琴(Accordion)效果.这个其实就是以前写的图片滑动展示效果的改进版,那是我第一篇比较受关注的文章,是时候整理一下了. 有如 ...
- 【CodeForces】947 C. Perfect Security 异或Trie
[题目]C. Perfect Security [题意]给定长度为n的非负整数数组A和数组B,要求将数组B重排列使得A[i]^B[i]的字典序最小.n<=3*10^5,time=3.5s. [算 ...
- form表单设置input文本属性只读,不可更改
记住一条好用的,设置readonly属性为true <input readonly=''true"> 更多方法,转载: http://www.jb51.net/web/6 ...
- scrapy爬虫框架介绍
一 介绍 Scrapy一个开源和协作的框架,其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的,使用它可以以快速.简单.可扩展的方式从网站中提取所需的数据.但目前Scrapy的用途十分广泛,可 ...
- php简单文件管理器——php经典实例
<html> <head> <title>文件管理</title> <meta charset='utf-8' /> </head&g ...
- webconfig的配置解析
<?xml version="1.0"?> <!--注意: 除了手动编辑此文件以外,您还可以使用 Web 管理工具来配置应用程序的设置.可以使用 Visual S ...