From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 17360 invoked from network); 4 Sep 2004 15:45:13 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 4 Sep 2004 15:45:13 -0000 Received: (qmail 89947 invoked from network); 4 Sep 2004 15:45:07 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 4 Sep 2004 15:45:07 -0000 Received: (qmail 11535 invoked by alias); 4 Sep 2004 15:45:00 -0000 Mailing-List: contact zsh-users-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 7975 Received: (qmail 11518 invoked from network); 4 Sep 2004 15:45:00 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by sunsite.dk with SMTP; 4 Sep 2004 15:45:00 -0000 Received: (qmail 88809 invoked from network); 4 Sep 2004 15:45:00 -0000 Received: from moonbase.zanshin.com (64.84.47.139) by a.mx.sunsite.dk with SMTP; 4 Sep 2004 15:44:57 -0000 Received: from toltec.zanshin.com (toltec.zanshin.com [64.84.47.166]) by moonbase.zanshin.com (8.13.1/8.13.1) with ESMTP id i84FiusW022900 for ; Sat, 4 Sep 2004 08:44:56 -0700 Date: Sat, 4 Sep 2004 08:44:56 -0700 (PDT) From: Bart Schaefer Reply-To: zsh-users@sunsite.dk To: Zsh Users Subject: Re: Making a script 'sourceable' In-Reply-To: <20040904110724.GA12874@DervishD> Message-ID: References: <20040904110724.GA12874@DervishD> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Spam-Checker-Version: SpamAssassin 2.63 on a.mx.sunsite.dk X-Spam-Level: X-Spam-Status: No, hits=-0.0 required=6.0 tests=BAYES_44 autolearn=no version=2.63 X-Spam-Hits: -0.0 On Sat, 4 Sep 2004, DervishD wrote: > > > The second thing is derived from the above question: since > > > checking for 'sourcery' ;) is very difficult even non portably, I've > > > thought about making my zsh scripts sourceables. > > Lloyd Z. has the way of it. > Well, an extra fork... I don't really like that method, but... There's also this, wherein a function name unlikely to exist in the calling shell is invented and then that function destroys itself as soon as it is invoked: --- 8< --- #! bin/zsh function __the_real_script_$$ { unfunction __the_real_script_$$ emulate -LR zsh # body of script goes here, using "local" to control variables } __the_real_script_$$ "$@" --- >8 --- However, that pretty thoroughly demolishes the usefulness of $0, and any error messages that are printed will fail to show the name of the script and the line numbers will be "wrong". On the other hand this (and the subshell wrapper variant, too) has the advantage that the entire script is parsed for syntax before any of it is executed, so if you make a mistake somewhere you don't have half-finished script processing to clean up. Back on the first hand again, though, you pay the memory cost of that parse on every call to the script.