On 08/11/2010 05:20 AM, Pierre Gaston wrote:
> On Wed, Aug 11, 2010 at 2:18 PM, Peng Yu <pengyu...@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   ebl...@redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to