From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 6377 invoked by alias); 3 May 2012 20:37:39 -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: 17056 Received: (qmail 3729 invoked from network); 3 May 2012 20:37:36 -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=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 Received-SPF: neutral (ns1.primenet.com.au: 209.85.212.177 is neither permitted nor denied by SPF record at ntlworld.com) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-proxyuser-ip:date:from:to:subject:message-id:in-reply-to :references:x-mailer:mime-version:content-type :content-transfer-encoding:x-gm-message-state; bh=bPvC2p+Ux1VYXdNdku0cxi94tIEa1s9w4nLUg9Mlzys=; b=Hwa637vYrENZDPadb99B9AI0aha5ogYeHBDJAEPMftSgUcDuf78b7Rq817YqZweG3H P311Ql1/+jIBooLpxxV5/KwxW0aF6O6yKHHY++TjUEGLOclt6O4f4morDlBVxvsknO1X Hmur97huN92VuI4ygmuhJR5YWbwSJ5fL7+1ONd0G64JYLBYaWW5+YGYF/wzpG8LYYikf hmsrJQ/lk+PlmbCqXjnpKYG+8ENhcePe5jn3UzHEz40nMMEg/GtJWxlka2O+7rz2oXvF QEBFGuLgohndwKV15p9+NslxoORq7w+3CiEdtNt6nhwTRXQozOlkaHebOvODp8ON8ILN kOew== X-ProxyUser-IP: 86.6.29.42 Date: Thu, 3 May 2012 21:37:25 +0100 From: Peter Stephenson To: zsh-users@zsh.org Subject: Re: a 'require' function for zsh scripts and interactive functions Message-ID: <20120503213725.751d7e7b@pws-pc.ntlworld.com> In-Reply-To: <120503082329.ZM13981@torch.brasslantern.com> References: <120503082329.ZM13981@torch.brasslantern.com> X-Mailer: Claws Mail 3.8.0 (GTK+ 2.24.7; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Gm-Message-State: ALoCoQkHzPUpDM1W3EEBe5/n/eBEnGlsPOIheuTfBuWOb5vKL3PRH/PrO3Kwkfr51LP8FyjhxiB+ On Thu, 03 May 2012 08:23:29 -0700 Bart Schaefer wrote: > require () { > setopt localtraps > for UTIL > do > trap "msg 'No $UTIL found'" EXIT > : ${commands[$UTIL]:?require failed} > done > trap - EXIT > } > > There's one more gotcha with that which may be a bug: The EXIT trap is > skipped when the function returns with 'require failed' (but still run > by non-interactive shells when an actual exit occurs). It's not entirely clear whether it's correct, since we don't explicitly define what it should do and EXIT traps in functions are specific to zsh, but it's plausible. The ":?" syntax causes immediate exit from the shell, or back to the prompt: in other words, it's treated as an immediate error, i.e. the same as e.g.: % fn() { trap 'echo EXITING' EXIT; echo ${**}; } % fn fn: bad substitution % fn() { trap 'echo EXITING' EXIT; echo ${foo:?nope, sorry}; } % fn fn: foo: nope, sorry The cases are, however, different when you're exiting the shell: % zsh -fc 'trap "echo EXITING" EXIT; echo ${foo:?nope, sorry}' zsh:1: foo: nope, sorry EXITING % zsh -fc 'trap "echo EXITING" EXIT; echo ${**}' zsh:1: bad substitution Bash calls the trap in both cases. You can argue the function and shell cases are different since the ":?" might be considered to cause the function to abort, with nothing more in the main body of the script executing, but then the shell to exit normally (apart from the status), so the EXIT trap would be run. So there are actually 16 possibilities, though the realistic ones are where the trap occurs additionally in one or more of the three remaining cases. You might well consider the most useful behaviour is for the trap to happen in all cases, since it's there to clear up. -- Peter Stephenson Web page now at http://homepage.ntlworld.com/p.w.stephenson/