From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 3899 invoked by alias); 14 May 2014 16:31:49 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 18807 Received: (qmail 18189 invoked from network); 14 May 2014 16:31:44 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham version=3.3.2 Date: Wed, 14 May 2014 18:22:48 +0200 From: Roman Neuhauser To: Peter Stephenson Cc: zsh-users@zsh.org Subject: Re: Stop script if one command returns != 0 Message-ID: <20140514162248.GE1629@isis.sigpipe.cz> References: <20140514152011.7a811dcb@pwslap01u.europe.root.pri> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20140514152011.7a811dcb@pwslap01u.europe.root.pri> User-Agent: Mutt/1.5.21 (2010-09-15) # p.stephenson@samsung.com / 2014-05-14 15:20:11 +0100: > On Wed, 14 May 2014 16:01:19 +0200 > Florian Lindner wrote: > > I have a script that I source with a number of commands and exports. Is > > there a way to tell zsh (or even in a portable way to sh) to stop > > execution of this script if any of the commands returns a return code > > other than zero? > > (Count the responses. I'm guessing 3 to 5...) > > set -e > > is standard across Bourne-like shells including zsh --- this is > equivalent to the zsh option "ERREXIT". errexit as specified by SUS/POSIX is mostly useless and very dangerous. http://pubs.opengroup.org/onlinepubs/9699919799/: % The -e setting shall be ignored when executing the compound list % following the while, until, if, or elif reserved word, a pipeline % beginning with the ! reserved word, or any command of an AND-OR list % other than the last. you might think this means that if { f; g; h}; then ... fi will execute all of f, g, h no matter if any of them fails. that's true, but not the whole truth: errexit won't be in effect *globally* while the condition is getting executed. this goes down to functions: ---------- 8< ---------- #!/bin/sh set -e f() { # h must not run when g fails; # we have errexit, hooray! do-this do-that } if f; then # f is executed with set +e in effect !!! ... fi f && echo ok # again, f runs with set +e ---------- >8 ---------- http://austingroupbugs.net/view.php?id=537 -- roman