DASH Shell discussions
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: Peng Yu <pengyu.ut@gmail.com>
Cc: dash@vger.kernel.org
Subject: Re: How to inherent non-zero exit status using pipe? (dash)
Date: Wed, 11 Aug 2010 07:51:45 -0600	[thread overview]
Message-ID: <4C62AAF1.4060103@redhat.com> (raw)
In-Reply-To: <AANLkTimi+MdwWVLceKdNgjCaK5B=dm_+raH++Owdh_QS@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1024 bytes --]

On 08/11/2010 06:34 AM, Peng Yu wrote:
> Hi,
> 
> The following example returns the exit status of the last command in a
> pipe. I'm wondering if there is a way to inherent non-zero exit status
> using pipe. That is, if there is any command in a pipe that return a
> non-zero status, I'd like the whole pipe return a non-zero status.

Generally not possible using only POSIX constructs (bash's 'set -o
pipefail' and '${PIPESTATUS[@]}' are extensions).  But what you _can_ do
is play with exec to be able to propagate non-zero status from a
particular portion of a pipeline through a temporary variable or file:

$ exec 3>&1 # duplicate original stdout
$ result=$(
  exec 4>&1 >&3 3>&- # move cmd subst stdout, and restore original
  { ./main.sh; echo $? >&4 # run command, and record its status
  } | head -n 3)
$ echo $? # status from head
$ echo $result # status from ./main.sh
$ exec 3>&-

-- 
Eric Blake   eblake@redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 620 bytes --]

  reply	other threads:[~2010-08-11 13:52 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-08-11 12:34 How to inherent non-zero exit status using pipe? (dash) Peng Yu
2010-08-11 13:51 ` Eric Blake [this message]
2010-08-13  1:15   ` Herbert Xu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4C62AAF1.4060103@redhat.com \
    --to=eblake@redhat.com \
    --cc=dash@vger.kernel.org \
    --cc=pengyu.ut@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox