From: "Benjamin R. Haskell" <zsh@benizi.com>
To: TjL <luomat@gmail.com>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: equivalent of "if (( $+commands[FOO] ))" for functions?
Date: Mon, 6 Aug 2012 18:38:58 -0400 (EDT) [thread overview]
Message-ID: <alpine.LNX.2.01.1208061823580.24430@hp.internal> (raw)
In-Reply-To: <CAPHo7Jcp4CoYOjACfOuKQ6dBuS8gCNGy-yCC9e6zCRseahhZug@mail.gmail.com>
On Mon, 6 Aug 2012, TjL wrote:
> I only recently learned about this method of taking some action only
> if the command 'FOO' is found:
>
> if (( $+commands[FOO] ))
> then
>
> # take actions
>
> fi
>
> but what I am wondering is: is there a way to have this same sort of
> check, except that it also includes zsh functions/aliases?
>
> If yes, what's the syntax for that?
if (( $+functions[FOO] )) ; then : actions here ; fi
if (( $+aliases[FOO] )) ; then : actions here ; fi
The $commands, $functions, and $aliases associative arrays are described
in `man zshmodules` under the heading:
THE ZSH/PARAMETER MODULE
In case you've only seen the idiom you're using, and didn't have an
explanation:
$+param expands to 0 if param is unset, and 1 if it's set. The double
parentheses: (( ... )) just make the conditional "mathy" (so that
non-zero is true). So, you can use this with your own associative
arrays, too:
typeset -A some_array
some_array+=( foo some-foo-thing )
if (( $+some_array[foo] ))
then
echo yay
fi
> Otherwise I'll keep using 'which' and sending the output to /dev/null
> but I figured it was worth asking.
Combining what you've asked about: to execute some action whether FOO is
a command, an alias, or a function (or a built-in):
if (( $+commands[FOO] || $+functions[FOO] || $+aliases[FOO] || $+builtins[FOO] ))
then
# actions
fi
But, since `which` is itself a shell built-in, it might be quicker and
easier to just keep using it:
if which FOO &> /dev/null
then
# actions
fi
I generally use (($+commands[FOO])) to test for whether a command is
installed (and usually to run `alias FOO=something` if it's not). Using
which FOO &> /dev/null lets the user override FOO in a different way.
--
Best,
Ben
next prev parent reply other threads:[~2012-08-06 22:49 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-08-06 22:13 TjL
2012-08-06 22:36 ` Frank Terbeck
2012-08-06 22:38 ` Benjamin R. Haskell [this message]
2012-08-06 23:12 ` TjL
2012-08-07 13:17 ` Benjamin R. Haskell
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=alpine.LNX.2.01.1208061823580.24430@hp.internal \
--to=zsh@benizi.com \
--cc=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).