[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to inherent non-zero exit status using pipe?
From: |
Eric Blake |
Subject: |
Re: How to inherent non-zero exit status using pipe? |
Date: |
Wed, 11 Aug 2010 07:46:02 -0600 |
User-agent: |
Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.8) Gecko/20100805 Red Hat/3.1.2-2.el6 Mnenhy/0.8.3 Thunderbird/3.1.2 |
On 08/11/2010 05:20 AM, Pierre Gaston wrote:
> On Wed, Aug 11, 2010 at 2:18 PM, Peng Yu <pengyu.ut@gmail.com> 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.
>
> set -o pipefail
But be aware that you seldom want to use this globally - if you use it,
it should only be around the pipeline that you care about and
immediately followed by set +o pipefail. Why? Because some commands
are _designed_ to have non-zero exit status when sent to a pipe, but
which should not cause the script to fail. For example:
yes | rm
In this case yes will never exit with 0 status, because yes is
terminated by the SIGPIPE caused when rm finishes its job and quits
accepting input from yes. But you don't care whether yes was
successful, just whether rm was able to do its job.
--
Eric Blake eblake@redhat.com +1-801-349-2682
Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature