zsh-users
 help / color / mirror / code / Atom feed
* a 'require' function for zsh scripts and interactive functions
@ 2012-05-03 13:56 TJ Luoma
  2012-05-03 15:23 ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: TJ Luoma @ 2012-05-03 13:56 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 1117 bytes --]


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



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-05-03 20:37 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-05-03 13:56 a 'require' function for zsh scripts and interactive functions TJ Luoma
2012-05-03 15:23 ` Bart Schaefer
2012-05-03 20:37   ` Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).