From nobody@FreeBSD.org  Sat Apr  7 03:16:12 2007
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52])
	by hub.freebsd.org (Postfix) with ESMTP id 5C7A716A403
	for <freebsd-gnats-submit@FreeBSD.org>; Sat,  7 Apr 2007 03:16:12 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [69.147.83.33])
	by mx1.freebsd.org (Postfix) with ESMTP id 4EC1D13C48C
	for <freebsd-gnats-submit@FreeBSD.org>; Sat,  7 Apr 2007 03:16:12 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.13.1/8.13.1) with ESMTP id l373GBuR010184
	for <freebsd-gnats-submit@FreeBSD.org>; Sat, 7 Apr 2007 03:16:11 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.13.1/8.13.1/Submit) id l373BAaL000574;
	Sat, 7 Apr 2007 03:11:10 GMT
	(envelope-from nobody)
Message-Id: <200704070311.l373BAaL000574@www.freebsd.org>
Date: Sat, 7 Apr 2007 03:11:10 GMT
From: Alexander Melkov<melkov@comptek.ru>
To: freebsd-gnats-submit@FreeBSD.org
Subject: sh and bash with -e sometimes fail to test errors in functions
X-Send-Pr-Version: www-3.0

>Number:         111343
>Category:       bin
>Synopsis:       sh(1) and bash(1) with -e sometimes fail to test errors in functions
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    jilles
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sat Apr 07 03:20:02 GMT 2007
>Closed-Date:    Sun Jun 12 10:39:44 UTC 2011
>Last-Modified:  Sun Jun 12 10:39:44 UTC 2011
>Originator:     Alexander Melkov
>Release:        6.2
>Organization:
>Environment:
FreeBSD melkov.ru 6.2-PRERELEASE FreeBSD 6.2-PRERELEASE #9: Thu Jan 11 23:13:53 MSK 2007     ----@melkov.ru:/usr/obj/usr/src/sys/MELKOV  i386
>Description:
When shell function is called as a part of complex command with || operator,
effect of -e (i.e. exit after first untested command returns error status)
within that function is disabled.

>How-To-Repeat:
1. Create script
==> test.sh <==
set -e

qq ( ) {
  false
  echo ok
}

qq || echo failed
<end script>

2a) FreeBSD 4.11
Command:
 sh -e test.sh
<no output>

Command:
 bash -e test.sh
Output:
 ok

2b) FreeBSD 6.2
Command:
 sh -e test.sh
Output:
 ok

Command:
 bash -e test.sh
Output:
 ok

In all cases, expected output is:
 failed

>Fix:

>Release-Note:
>Audit-Trail:

From: Jilles Tjoelker <jilles@stack.nl>
To: bug-followup@FreeBSD.org, melkov@comptek.ru
Cc:  
Subject: Re: bin/111343: sh(1) and bash(1) with -e sometimes fail to test
	errors in functions
Date: Mon, 14 Sep 2009 00:43:27 +0200

 Replacing the function call with the function definition, the behaviour
 seems reasonable. The command is tested, so -e should not trigger.
 
 In general, set -e is not a good idea for more complicated scripts
 because its rules can be confusing and inconsistent across shells. You
 can achieve a similar effect by putting '|| exit' after commands.
 
 -- 
 Jilles Tjoelker

From: "Alexander Melkov" <melkov@comptek.ru>
To: "Jilles Tjoelker" <jilles@stack.nl>,
	<bug-followup@FreeBSD.org>
Cc:  
Subject: Re: bin/111343: sh(1) and bash(1) with -e sometimes fail to testerrors in functions
Date: Mon, 14 Sep 2009 14:18:06 +0400

 > Replacing the function call with the function definition, the behaviour
 > seems reasonable.
 Ok, thanks. The problem is much worse then.
 ==> test1.sh <==
 set -e
 
 { false; echo ok; } || echo failed
 if ! { false; echo ok; } ; then
   echo failed
 fi
 if ! ( false; echo ok; ) ; then
   echo failed
 fi
 - outputs
 ok
 ok
 ok
 
 > The command is tested, so -e should not trigger.
 Why testing the command should change the operation *within* that command? 
 That's well beyond traditional programming, thus obviously a bug.
 
 > In general, set -e is not a good idea for more complicated scripts
 > because its rules can be confusing and inconsistent across shells. You
 > can achieve a similar effect by putting '|| exit' after commands.
 Ok, very nice. I'll just take my 10000 lines of scripts and add 10000 times 
 '|| exit'.
 Actually -e is well defined and operates reliably except the case mentioned 
 in this PR.
 
State-Changed-From-To: open->closed 
State-Changed-By: jilles 
State-Changed-When: Sun Jun 12 10:36:20 UTC 2011 
State-Changed-Why:  
Fixing this would require a new '-e'-like option so as to avoid 
breaking existing scripts relying on '-e'. The exact behaviour 
of the new option is unclear and it seems an ugly solution to me. 
It also looks like you have not asked bash maintainers about this. 

Another alternative other than '|| exit' is 'terminating' 
commands with '&&' rather than ';'. 


Responsible-Changed-From-To: freebsd-bugs->jilles 
Responsible-Changed-By: jilles 
Responsible-Changed-When: Sun Jun 12 10:36:20 UTC 2011 
Responsible-Changed-Why:  
Track. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=111343 
>Unformatted:
