Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
a 'require' function for zsh scripts and interactive functions
- X-seq: zsh-users 17054
- From: TJ Luoma <luomat@xxxxxxxxx>
- To: zsh-users@xxxxxxx
- Subject: a 'require' function for zsh scripts and interactive functions
- Date: Thu, 3 May 2012 09:56:06 -0400
- 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==
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
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
Messages sorted by:
Reverse Date,
Date,
Thread,
Author