From owner-freebsd-questions@FreeBSD.ORG Tue Dec 27 23:00:32 2011 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 9FA8D106566B for ; Tue, 27 Dec 2011 23:00:32 +0000 (UTC) (envelope-from max@mxcrypt.com) Received: from mail-vw0-f54.google.com (mail-vw0-f54.google.com [209.85.212.54]) by mx1.freebsd.org (Postfix) with ESMTP id 56F9C8FC14 for ; Tue, 27 Dec 2011 23:00:31 +0000 (UTC) Received: by vbbfr13 with SMTP id fr13so16482946vbb.13 for ; Tue, 27 Dec 2011 15:00:31 -0800 (PST) Received: by 10.52.94.242 with SMTP id df18mr3515941vdb.122.1325026831213; Tue, 27 Dec 2011 15:00:31 -0800 (PST) MIME-Version: 1.0 Received: by 10.220.150.132 with HTTP; Tue, 27 Dec 2011 14:59:59 -0800 (PST) In-Reply-To: <029f01ccc4df$9cca8190$d65f84b0$@fisglobal.com> References: <029f01ccc4df$9cca8190$d65f84b0$@fisglobal.com> From: Maxim Khitrov Date: Tue, 27 Dec 2011 17:59:59 -0500 Message-ID: To: Devin Teske Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: FreeBSD Subject: Re: Unexpected sh behavior with EXIT trap and errexit X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 27 Dec 2011 23:00:32 -0000 On Tue, Dec 27, 2011 at 4:36 PM, Devin Teske wr= ote: >> -----Original Message----- >> From: owner-freebsd-questions@freebsd.org [mailto:owner-freebsd- >> questions@freebsd.org] On Behalf Of Maxim Khitrov >> Sent: Tuesday, December 27, 2011 12:25 PM >> To: FreeBSD >> Subject: Unexpected sh behavior with EXIT trap and errexit >> >> Can anyone explain this behavior (FreeBSD 9.0-RC3 amd64): >> >> Script: >> ---- >> #!/bin/sh >> >> cleanup() >> { >> =C2=A0 =C2=A0 echo 'first' >> =C2=A0 =C2=A0 echo 'second' >> } >> >> fail() { return 42; } >> >> trap cleanup EXIT >> set -o errexit >> fail >> ---- >> >> Output: >> ---- >> first >> ---- >> > > If you change to: > > fail() { false; } > > Then the outcome is what you expect (both lines come out). It seems that any command other than return, built-in or external, works as expected. > ASIDE: It appears that it's nothing to do with echo, stdout, or anything = other than the fact that only the first command of the cleanup routine is c= alled. Correct, I used echo just for the example. I also tried 'set +o errexit' as the first line of cleanup() to see if that would allow execution to continue. Nope. > From sh(1) regarding errexit: > > "If a shell function is executed and its exit status is explicitly tested= , all commands of the function are considered to be tested as well." > > The exact meaning of which escapes me at the moment, but I'm lead to beli= eve that this explanation somehow plays a role in what we're witnessing wit= h your sample. I don't know if this is related, but it means that errexit should not cause the following code to terminate (and it doesn't): dosomething() { echo 'here' false echo 'still here' } set -o errexit dosomething || true echo 'all is well' > It may be a bug, it may not. What's interesting in the sample is that the= "return 42" is a valid command that succeeds while the function itself doe= s not. > > HTH, > Devin > > >> Now comment out 'set -o errexit', replace 'fail' with 'fail || exit' >> (which should be equivalent to using errexit), and run again. >> >> Output: >> ---- >> first >> second >> ---- >> >> Am I doing something stupid or is this a bug? Bash prints out the same >> (second) output for both versions of the code. >> >> - Max