Chet Ramey wrote:
Roman Rakus wrote:
Hi
there is simple script:
#!/bin/sh
echo foo|cat /nosuchfile
echo "here: $?"
executed by sh -e
In bash-3.2 we got:
cat: /nosuchfile: No such file or directory
but in bash-4.0:
cat: /nosuchfile: No such file or directory
here: 1
The bash-4.0 behavior is correct; the previous behavior was a bug.
From execute_cmd.c:
/* 10/6/2008 -- added test for pipe_in and pipe_out because they indicate
the presence of a pipeline, and (until Posix changes things), a
pipeline failure should not cause the parent shell to exit on an
unsuccessful return status, even in the presence of errexit.. */
I'm not sure what Posix says
Posix says that only simple command failure should cause the shell to exit,
with several exceptions that are listed in the standard. A pipeline is
not a simple command.
That means that a pipeline failure doesn't cause the shell to exit, and
the failure of any individual simple command within a pipeline should not
cause the parent shell to exit. Even if the child process running the
pipeline element inherits the errexit flag, the fact that it exits should
not affect the parent shell. (The bug was that only a non-zero exit status
in the *final* pipeline element caused the parent shell to exit.)
but it breaks the:
-e Exit immediately if a command exits with a non-zero status.
I'm not immediately sure where you got that, but the documentation makes
it clear:
-e Exit immediately if a simple command (see SHELL GRAMMAR
above) exits with a non-zero status. The shell does not
exit if the command that fails is part of the command
list immediately following a while or until keyword,
part of the test in an if statement, part of a && or ||
list, or if the command's return value is being inverted
via !. A trap on ERR, if set, is executed before the
shell exits.
Chet
This looks like Posix is not clear as it should be.
In Posix:
-e
When this option is on, if a simple command fails for any of the reasons
listed in Consequences of Shell Errors or returns an exit status value >0, and
is not part of the compound list following a while, until, or if keyword, and
is not a part of an AND or OR list, and is not a pipeline preceded by the !
reserved word, then the shell shall immediately exit.
They are
talking about simple commands, but later the words `is not a pipeline preceded
by the ! reserved word' makes no sense, if they are talking only about simple
commands...
So I understand that this is meant in bash this way.
RR