From: TJ Luoma <luomat@gmail.com>
To: zsh-users@zsh.org
Subject: a 'require' function for zsh scripts and interactive functions
Date: Thu, 3 May 2012 09:56:06 -0400 [thread overview]
Message-ID: <D1F7FCAD7C22435C9B6D93594DF416FF@gmail.com> (raw)
[-- 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
next reply other threads:[~2012-05-03 13:56 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-05-03 13:56 TJ Luoma [this message]
2012-05-03 15:23 ` Bart Schaefer
2012-05-03 20:37 ` Peter Stephenson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=D1F7FCAD7C22435C9B6D93594DF416FF@gmail.com \
--to=luomat@gmail.com \
--cc=zsh-users@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).