zsh-users
 help / color / mirror / code / Atom feed
* equivalent of "if (( $+commands[FOO] ))" for functions?
@ 2012-08-06 22:13 TjL
  2012-08-06 22:36 ` Frank Terbeck
  2012-08-06 22:38 ` Benjamin R. Haskell
  0 siblings, 2 replies; 5+ messages in thread
From: TjL @ 2012-08-06 22:13 UTC (permalink / raw)
  To: Zsh Users

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?

Otherwise I'll keep using 'which' and sending the output to /dev/null
but I figured it was worth asking.

Thanks!

TjL


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

* Re: equivalent of "if (( $+commands[FOO] ))" for functions?
  2012-08-06 22:13 equivalent of "if (( $+commands[FOO] ))" for functions? TjL
@ 2012-08-06 22:36 ` Frank Terbeck
  2012-08-06 22:38 ` Benjamin R. Haskell
  1 sibling, 0 replies; 5+ messages in thread
From: Frank Terbeck @ 2012-08-06 22:36 UTC (permalink / raw)
  To: Zsh Users

[...]
> 	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?

$+functions[foo], $+aliases[FOO]...

See "man zshmodules | less -p PARAMETER" for details.

Gruß Frank


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

* Re: equivalent of "if (( $+commands[FOO] ))" for functions?
  2012-08-06 22:13 equivalent of "if (( $+commands[FOO] ))" for functions? TjL
  2012-08-06 22:36 ` Frank Terbeck
@ 2012-08-06 22:38 ` Benjamin R. Haskell
  2012-08-06 23:12   ` TjL
  1 sibling, 1 reply; 5+ messages in thread
From: Benjamin R. Haskell @ 2012-08-06 22:38 UTC (permalink / raw)
  To: TjL; +Cc: Zsh Users

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


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

* Re: equivalent of "if (( $+commands[FOO] ))" for functions?
  2012-08-06 22:38 ` Benjamin R. Haskell
@ 2012-08-06 23:12   ` TjL
  2012-08-07 13:17     ` Benjamin R. Haskell
  0 siblings, 1 reply; 5+ messages in thread
From: TjL @ 2012-08-06 23:12 UTC (permalink / raw)
  To: Benjamin R. Haskell; +Cc: Zsh Users

On Mon, Aug 6, 2012 at 6:38 PM, Benjamin R. Haskell <zsh@benizi.com> wrote:
> 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

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'

For example:

	foo

	EXIT="$?"

	if [ "$EXIT" = "0" ]
	then
		# do whatever

	else
		echo "$0: failed (\$EXIT = $EXIT)"

		exit 1
	fi

Is there a way to do something like that with $+param?

I tried this:

	EXIT+=( test -d ~/etc )

	if (( $+EXIT[test] ))
	then
		echo yes
	else
		echo no
	fi

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.


TjL


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

* Re: equivalent of "if (( $+commands[FOO] ))" for functions?
  2012-08-06 23:12   ` TjL
@ 2012-08-07 13:17     ` Benjamin R. Haskell
  0 siblings, 0 replies; 5+ messages in thread
From: Benjamin R. Haskell @ 2012-08-07 13:17 UTC (permalink / raw)
  To: TjL; +Cc: Zsh Users

[-- Attachment #1: Type: TEXT/PLAIN, Size: 2396 bytes --]

On Mon, 6 Aug 2012, TjL wrote:

> On Mon, Aug 6, 2012 at 6:38 PM, Benjamin R. Haskell wrote:
>> 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
>
> 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'
>
> For example:
>
> 	foo
>
> 	EXIT="$?"
>
> 	if [ "$EXIT" = "0" ]
> 	then
> 		# do whatever
>
> 	else
> 		echo "$0: failed (\$EXIT = $EXIT)"
>
> 		exit 1
> 	fi

Unless there's a command in between 'foo' and 'EXIT="$?"', this is cleaner:

if foo
then
     # do whatever
else
     echo "$0: failed (\$EXIT = $?)"
     exit 1
fi



> Is there a way to do something like that with $+param?

You don't need the '+' in $+param.  The '+' tests for whether the 
parameter is set, and changes the return to 0 or 1.  $EXIT will always 
be set, and isn't an associative array (It's just a normal parameter).


> I tried this:
>
> 	EXIT+=( test -d ~/etc )
>
> 	if (( $+EXIT[test] ))
> 	then
> 		echo yes
> 	else
> 		echo no
> 	fi

test -d ~/etc
ERROR=$?

if (( ! ERROR ))
then
     echo yes
else
     echo no
fi

I've written 'ERROR' rather than 'EXIT' here, because the "truthy" value 
of command returns (0) and the "truthy" value expected by ((...))-style 
parens (non-zero) are reversed.


> 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.

The way you'd written it, EXIT will be an array (a normal array, not an 
associative array) with the values:

EXIT[1]=test
EXIT[2]=-d
EXIT[3]=~/etc

Then, since EXIT isn't an associative array, testing whether it has the 
key 'test' always returns false.


> So I assume that I'm misunderstanding something, possibly trying to 
> make apple pie uses oranges and wondering why it doesn't taste right.

Good analogy. :-)

-- 
Best,
Ben

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

end of thread, other threads:[~2012-08-07 13:18 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-08-06 22:13 equivalent of "if (( $+commands[FOO] ))" for functions? TjL
2012-08-06 22:36 ` Frank Terbeck
2012-08-06 22:38 ` Benjamin R. Haskell
2012-08-06 23:12   ` TjL
2012-08-07 13:17     ` Benjamin R. Haskell

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).