Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

a 'require' function for zsh scripts and interactive functions



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