Zsh Mailing List Archive
Messages sorted by:
Re: equivalent of "if (( $+commands[FOO] ))" for functions?
- X-seq: zsh-users 17199
- From: TjL <luomat@xxxxxxxxx>
- To: "Benjamin R. Haskell" <zsh@xxxxxxxxxx>
- Subject: Re: equivalent of "if (( $+commands[FOO] ))" for functions?
- Date: Mon, 6 Aug 2012 19:12:04 -0400
- Cc: Zsh Users <zsh-users@xxxxxxx>
- In-reply-to: <alpine.LNX.email@example.com>
- List-help: <mailto:firstname.lastname@example.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:email@example.com>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <CAPHo7Jcp4CoYOjACfOuKQ6dBuS8gCNGy-yCC9e6zCRseahhZug@mail.gmail.com> <alpine.LNX.firstname.lastname@example.org>
- Sender: tj@xxxxxx
On Mon, Aug 6, 2012 at 6:38 PM, Benjamin R. Haskell <zsh@xxxxxxxxxx> wrote:
> In case you've only seen the idiom you're using, and didn't have an
> $+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] ))
> echo yay
Ah, that's helpful, thanks. Indeed I have just been copy/pasting this
without really knowing how it worked.
Hrm… so… I often do something like this to do different things based
on the exit status of a given command 'foo'
if [ "$EXIT" = "0" ]
# do whatever
echo "$0: failed (\$EXIT = $EXIT)"
Is there a way to do something like that with $+param?
I tried this:
EXIT+=( test -d ~/etc )
if (( $+EXIT[test] ))
thinking that it would say 'yes' if 'test -d' exited with status = 0
or 'no' with any other status, but that didn't seem to work (I always
seem to get no even if 'test -d' should return 0.
So I assume that I'm misunderstanding something, possibly trying to
make apple pie uses oranges and wondering why it doesn't taste right.
Messages sorted by: