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
  • 相关阅读:
    .NET开源项目
    关于微信号的校验
    java 中关于synchronized的通常用法
    关于java 定时器的使用总结
    新的博客已经启用,欢迎大家访问(402v.com)
    Hadoop综合大作业
    hive基本操作与应用
    理解MapReduce计算构架
    熟悉HBase基本操作
    第三章 熟悉常用的HDFS操作
  • 原文地址:https://www.cnblogs.com/kakaisgood/p/7681905.html
Copyright © 2011-2022 走看看