help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] questions about errexit, pipes to while loops


From: Jonathan Baccash
Subject: Re: [Help-bash] questions about errexit, pipes to while loops
Date: Wed, 28 Mar 2012 17:16:03 +0000

So I guess the philosophy of "set -e" is "die for more stuff, but not all 
stuff, and if in doubt check for yourself"? Without set -e, the behavior is 
basically "if you want to die when something fails, check for yourself", which 
is pretty much in conflict with the approach of most popular programming 
languages (exception handling) today, which is "if you want to continue 
processing despite a failure, check for yourself". It seems set -e is a partial 
solution only, and I guess if I really want to use exceptions the answer is I 
better use a language with exceptions.

Btw, I don't think your solution is fool-proof.

[ -f /file/you/want/there ] || exit 1
# Other process removes /file/you/want/there
command using < /file/you/want/there
# Command appears to succeed by exit status, but really failed.

Jon.

-----Original Message-----
From: Chet Ramey [mailto:address@hidden
Sent: Wednesday, March 28, 2012 8:41 AM
To: Jonathan Baccash
Cc: address@hidden; address@hidden
Subject: Re: [Help-bash] questions about errexit, pipes to while loops

On 3/27/12 1:41 PM, Jonathan Baccash wrote:
> Thanks  a bunch Chet. It sounds like the docs mixed with the bug on the first 
> example had me confused about what is considered a simple command. And it 
> sounds like newer versions of bash and posix are moving toward more sane 
> semantics (IMO), and I look forward to using bash 4.2. Perhaps it would be 
> good if the last example caused the compound command to return an error 
> status (and thereby fail the script when set -e is set)? Or perhaps there 
> needs to be an analogue of set -u for redirection to a file that doesn't 
> exist?

Posix actually speaks directly to what it calls `redirection errors':

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_08_01

They don't rise to the level that would cause the shell to exit, whether or not 
-e is set, unless applied to one of the special builtins (and never in an 
interactive shell).  The usual workaround

[ -f /file/you/want/there ] || exit 1
command using < /file/you/want/there

is the best.

Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    address@hidden    http://cnswww.cns.cwru.edu/~chet/

________________________________

The contents of this e-mail and any attachments are confidential and only for 
use by the intended recipient. Any unauthorized use, distribution or copying of 
this message is strictly prohibited. If you are not the intended recipient 
please inform the sender immediately by reply e-mail and delete this message 
from your system. Thank you for your co-operation.



reply via email to

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