Bug 77067

Summary: /bin/sh premature termination when 'set -e' is active
Product: Base System Reporter: Brian Candler <B.Candler>
Component: binAssignee: Stefan Farfeleder <stefanf>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 5.3-STABLE   
Hardware: Any   
OS: Any   

Description Brian Candler 2005-02-03 15:50:16 UTC
Under certain circumstances, 'set -e' causes /bin/sh to exit when it should
not. From the manpage:

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

However, there are circumstances where even when a command status is
explicitly tested the script still terminates. I can replicate this using a
function call.

How-To-Repeat: 
This script demonstrates the problem:

------------------------------------------------------------
#!/bin/sh

set -e

foo ( ) {
  if test "x$1" != "xyes"; then
    return 1
  fi
  echo "OK!"   # this line causes the problem!
}

if foo yes; then echo "yes"; fi
if foo no; then echo "no"; fi
echo "Completed!"
------------------------------------------------------------

It fails to complete. However, if you remove the 'echo "OK!"' line it *does*
complete. (This line can be anything - for example, replacing it with
"return 0" also causes the problem).

The script runs correctly under 'bash', so I don't think my understanding of
-e semantics is broken.
Comment 1 Stefan Farfeleder freebsd_committer freebsd_triage 2005-09-04 20:34:03 UTC
Responsible Changed
From-To: freebsd-bugs->stefanf

Over to me.
Comment 2 Stefan Farfeleder freebsd_committer freebsd_triage 2005-09-10 09:40:33 UTC
State Changed
From-To: open->patched

Fixed in -current, thanks.
Comment 3 Stefan Farfeleder freebsd_committer freebsd_triage 2005-12-26 18:17:21 UTC
State Changed
From-To: patched->closed

Also fixed in RELENG_{5,6}.