Bug 81165

Summary: /bin/sh -e bug
Product: Base System Reporter: Simon Marlow <simonmar>
Component: binAssignee: freebsd-bugs (Nobody) <bugs>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 4.11-STABLE   
Hardware: Any   
OS: Any   

Description Simon Marlow 2005-05-17 17:30:00 UTC
There is a bug in ash's handling of the -e flag.  See the example below.

Fix: 

unknown.
How-To-Repeat: 
$ cat >test.sh
if true; then
  false && true
fi
echo "test succeeded"
$ /bin/sh -e test.sh
zsh: 34546 exit 1     /bin/sh -e test.sh
$ bash -e test.sh
test succeeded

Bash works correctly.  If the 'if' statement is removed, ash also
works correctly.
Comment 1 Giorgos Keramidas 2005-05-17 19:34:45 UTC
On 2005-05-17 17:20, Simon Marlow <simonmar@gmail.com> wrote:
> There is a bug in ash's handling of the -e flag.  See the example
> below.
>
> $ cat >test.sh
> if true; then
>   false && true
> fi
> echo "test succeeded"
> $ /bin/sh -e test.sh
> zsh: 34546 exit 1     /bin/sh -e test.sh
> $ bash -e test.sh
> test succeeded
>
> Bash works correctly.  If the 'if' statement is removed, ash also
> works correctly.

Are you sure what bash does is correct?  What do the standards say about
indermediate commands that fail and the correct behavior of the "shell"
(i.e. the "false" command in there)?
Comment 2 freebsd 2005-05-17 20:53:07 UTC
in message <200505171620.j4HGKVLK034638@sm.dnsalias.com>,
wrote Simon Marlow thusly...
>
> There is a bug in ash's handling of the -e flag.  See the example below.
> 
> >How-To-Repeat:
> 
> $ cat >test.sh
> if true; then
>   false && true
> fi
> echo "test succeeded"
> $ /bin/sh -e test.sh
> zsh: 34546 exit 1     /bin/sh -e test.sh
> $ bash -e test.sh
> test succeeded
> 
> Bash works correctly.  If the 'if' statement is removed, ash also
> works correctly.

Just a data point ... "test succeeded" is printed by /bin/sh of
FreeBSD 5.3-p13, ksh93 20050202, & bash 3.0.16_1.


  - Parv

--
Comment 3 Simon Marlow 2005-05-17 21:26:34 UTC
On 5/17/05, Giorgos Keramidas <keramida@ceid.upatras.gr> wrote:
> On 2005-05-17 17:20, Simon Marlow <simonmar@gmail.com> wrote:
> > There is a bug in ash's handling of the -e flag.  See the example
> > below.
> >
> > $ cat >test.sh
> > if true; then
> >   false && true
> > fi
> > echo "test succeeded"
> > $ /bin/sh -e test.sh
> > zsh: 34546 exit 1     /bin/sh -e test.sh
> > $ bash -e test.sh
> > test succeeded
> >
> > Bash works correctly.  If the 'if' statement is removed, ash also
> > works correctly.
>=20
> Are you sure what bash does is correct?  What do the standards say about
> indermediate commands that fail and the correct behavior of the "shell"
> (i.e. the "false" command in there)?

Yes, I'm sure.  The /bin/sh manpage says:

     -e errexit
             Exit immediately if any untested command fails in non-interact=
ive
             mode.  The exit status of a command is considered to be explic=
-
             itly tested if the command is used to control an if, elif, whi=
le,
             or until; or if the command is the left hand operand of an ``&=
&''
             or ``||'' operator.

and (clearer, IMO), the Single Unix Spec:

-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 will immediately exit.

so the upshot is that the command

    false && X

should not cause the script to exit when the -e switch is on (it
doesn't matter what X is).

You can easily see that the behaviour of /bin/sh is strange, because
in this case

   if true; then E; fi

does not behave in the same way as just E.

However, it appears that /bin/sh in FreeBSD 5.3 is working correctly.

Cheers,
   Simon
Comment 4 Stefan Farfeleder freebsd_committer freebsd_triage 2005-09-10 11:14:02 UTC
State Changed
From-To: open->patched

Mark as patched because this bug exists only in RELENG_4.  Personally I 
don't want to touch sh in RELENG_4, so maybe the PR should just be closed. 
Simon, is updating to RELENG_{5,6} an option for you?
Comment 5 Stefan Farfeleder freebsd_committer freebsd_triage 2005-09-10 11:57:08 UTC
State Changed
From-To: patched->closed

Originator agrees that the report can be closed.