From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 16416 invoked by alias); 3 May 2012 13:56:23 -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: 17054 Received: (qmail 14560 invoked from network); 3 May 2012 13:56:20 -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.6 required=5.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,FREEMAIL_FROM,HTML_MESSAGE,NML_ADSP_CUSTOM_MED,RCVD_IN_DNSWL_LOW, T_DKIM_INVALID,T_TO_NO_BRKTS_FREEMAIL autolearn=no version=3.3.2 Received-SPF: pass (ns1.primenet.com.au: SPF record at _spf.google.com designates 209.85.213.171 as permitted sender) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:to:message-id:subject:x-mailer:mime-version:content-type; bh=JnQP/p0eN89oJ16hU21M9ebZO+uKRjPNMnmLqdhW+s8=; b=tAHbvte2XsykVKlPk2PbN5itS/HF0j9HZl+Fbh4qCWAN5hgpG5CwA8jj/QuXdDwAqQ heTMJt2a4Rlbs1mmSf+iH/KnRTKg13iN9O3uGP4REll6zh+gujdHANhX9PzfLh8K96SZ PKf3L4yBnQevn8yRE4VHa3ZkouKM5zTl1ZrCVrZUySJI1M1k3BiR3WkwsLpL4fdmMwyx XtgMJxygrAA0nn2bh75RlFcQjqnfy3Ki0gCsdfgmwdhbiOoEUQ2mjR8A8JgTIiyXl0Bb TzUbSXMctDyLoGTeIBnqpOQYKJVJ6xUsZL6n/8HohWWOXrqMMKUfCLgx4nbGGLQgYyvS KvMQ== Date: Thu, 3 May 2012 09:56:06 -0400 From: TJ Luoma To: zsh-users@zsh.org Message-ID: Subject: a 'require' function for zsh scripts and interactive functions X-Mailer: sparrow 1.5 (build 1043) MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="4fa28e76_15014acb_2833" --4fa28e76_15014acb_2833 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Content-Disposition: inline I'm trying to come up with a function which will allow me to 'require' that a given command is found in $PATH, so I can put a line at the top of a script like this: #!/bin/zsh require gmv lynx wget ... or in another function foo () { require bar bar } and know that the script will check to see if those are there before executing. I have defined 'require' like this require () { for UTIL in $@ do if (( $+commands[$UTIL] )) then : else msg "No $UTIL found" if [[ "$SHLVL" == "1" ]] then return 1 else exit 1 fi fi done } The SHLVL is intended to keep my login shell from exiting if a function doesn't find a required command. The : in the if/else/fi is because I wasn't sure how else to do a "not" for (( $+commands[$UTIL] )) (`msg` is just a fancy way of doing `echo` which uses `growlnotify` on Mac. http://luo.ma/msg) When I finished creating `require`, I found myself wondering if I had just reinvented a wheel that zsh already implemented some other way, so I thought I'd ask. I also thought there might be other ways to improve this if zsh didn't already have something built-in. TjL --4fa28e76_15014acb_2833--