zoukankan      html  css  js  c++  java
  • 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 (kshzsh 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
  • 相关阅读:
    Python之标示符和关键字
    Python之变量以及类型
    python之注释的分类
    Python的环境的搭建
    Python之第一个helloworld的代码
    Linux_ubuntu-命令系统管理
    Linux_ubuntu命令-用户、权限管理
    Linux-ubuntu命令-文件、磁盘管理
    Linux_Ubuntu命令概述
    Linux-Ubuntu文件权限
  • 原文地址:https://www.cnblogs.com/kakaisgood/p/7681905.html
Copyright © 2011-2022 走看看