From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-users@zsh.org
Subject: Re: a 'require' function for zsh scripts and interactive functions
Date: Thu, 03 May 2012 08:23:29 -0700 [thread overview]
Message-ID: <120503082329.ZM13981@torch.brasslantern.com> (raw)
In-Reply-To: <D1F7FCAD7C22435C9B6D93594DF416FF@gmail.com>
Att'n zsh-workers: Possible bug noted at the very end of this reply.
On May 3, 9:56am, TJ Luoma wrote:
>
> 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:
Wow, something did horrible things to your cut-and-paste. I won't try
to excerpt your function here, but all leading whitespace is gone, and
no line wrapping in your paragraphs (I've reformatted the excerpts).
It looks like a bad html-to-text conversion.
> The SHLVL is intended to keep my login shell from exiting if a
> function doesn't find a required command.
More on this in a moment ...
> The : in the if/else/fi is because I wasn't sure how else to do a
> "not" for (( $+commands[$UTIL] ))
That one is simple:
if ! (( $+commands[$UTIL] ))
or
if (( $+commands[$UTIL] == 0 ))
> When I finished creating `require`, I found myself wondering if I had
> just reinvented a wheel that zsh already implemented some other way,
Look at the ${param:?message} expansion.
${NAME?WORD}
${NAME:?WORD}
In the first form, if NAME is set, or in the second form if NAME
is both set and non-null, then substitute its value; otherwise,
print WORD and exit from the shell. Interactive shells instead
return to the prompt. If WORD is omitted, then a standard message
is printed.
The only tickler is that NAME and WORD aren't expanded in the output:
% print ${commands[$UTIL]?:No $UTIL found}
zsh: commands[$UTIL]: No $UTIL found
So your function is probably as good as anything, but if you want to
take advantage of the special behavior of :? for interactive shells you
can start with something like this:
require () {
setopt localtraps
for UTIL
do
trap "msg 'No $UTIL found'" EXIT
: ${commands[$UTIL]:?require failed}
done
trap - EXIT
}
There's one more gotcha with that which may be a bug: The EXIT trap is
skipped when the function returns with 'require failed' (but still run
by non-interactive shells when an actual exit occurs). So you may have
to play around with "eval" and quoting to get decent output in your
terminal, but for non-interactive shells growl will pop up.
--
Barton E. Schaefer
next prev parent reply other threads:[~2012-05-03 15:24 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-05-03 13:56 TJ Luoma
2012-05-03 15:23 ` Bart Schaefer [this message]
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=120503082329.ZM13981@torch.brasslantern.com \
--to=schaefer@brasslantern.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).