help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Terminate calling bash script upon receiving non zero re


From: Eric Blake
Subject: Re: [Help-bash] Terminate calling bash script upon receiving non zero return status (and possibly terminate calling script recursively)
Date: Mon, 23 Apr 2012 09:38:23 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:11.0) Gecko/20120329 Thunderbird/11.0.1

On 04/23/2012 09:25 AM, Greg Wooledge wrote:
> On Mon, Apr 23, 2012 at 10:20:25AM -0500, Peng Yu wrote:
>> I wasn't aware of '-e' before. But it seems to be what I am looking
>> for. See the example below.
> 
> IT'S A TRAP!

Agreed - although I have seen scripts that successfully use 'set -e',
they are few and far between - most scripts that attempt to use 'set -e'
end up being buggier than if the author had avoided it in the first place.

> 
>> I don't get when it is going to fail and when it is confusing. Would
>> you please let me know what you mean here?
> 

Another reason NOT to use 'set -e' - it does not play nicely with functions.

$ f() { set -e; false; echo oops; }
$ f && echo hi
oops
hi

Did you want to abort the shell as soon as you hit the failure on false?
 Too bad.  Not only did f() keep on running (and echo oops), but because
f was not aborted early, the call to f succeeded and you also got to the
next part of your code (and echo hi).  And this behavior is required by
POSIX.

We're serious when we tell you that 'set -e' is NOT the magic pill you
are looking for, and that you really are better off coding up error
handling yourself.

-- 
Eric Blake   address@hidden    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]