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: Tue, 27 Mar 2012 17:41:53 +0000

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?

Jon.

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

On 3/25/12 4:40 PM, Jonathan Baccash wrote:
> As far as I can tell, the documentation doesn't match the behavior for
> bash (using bash 3.1.17) in the following scripts. First, this script
> works as
> expected:
>
>
>
> set -e
> cat /dev/zero | false
> echo blah $?
>
>
>
> The script exits with status 1 and does not print blah 1.

While that might be what you expected, it was a bug and did not match the 
documentation or Posix (at the time), which specified that set -e affected only 
simple commands.  A pipeline, even one that ends with a simple command, is not 
a simple command.

That was fixed in 2008, between bash-3.2 and bash-4.0.


> This one does not
> appear to work in accordance with docs:
>
>
>
> set -e
>
> cat /dev/zero | while true; do false; done
>
> echo blah $?

This one works correctly (for the time; Posix and bash have changed since).
Not only is a pipeline not a simple command, a while loop is a compound command.

>
>
> Another weird thing I noticed is that this:
>
> set -e
> cat </file/that/doesnt/exist
>
> exits with a non-zero exit status,

Again, a failing simple command.  This time, it happens to be a redirection 
failure.

>
>
>
> set -e
> while true; do false; done </file/that/doesnt/exist
>
> does not. Shouldn't it?

No, not according to what bash and Posix specified at the time (and still do, 
actually).

As I alluded to above, bash and Posix have changed since 2008.  Bash-4.2 
implements the current Posix specification, which exits the shell if any 
command fails -- not just simple commands -- with the usual exceptions listed 
in the man page.  Interestingly, no shell exits on your last example above.

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]