From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 11471 invoked by alias); 7 Aug 2012 13:18:13 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 17201 Received: (qmail 7086 invoked from network); 7 Aug 2012 13:18:11 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham version=3.3.2 Received-SPF: pass (ns1.primenet.com.au: SPF record at benizi.com designates 64.130.10.15 as permitted sender) Date: Tue, 7 Aug 2012 09:17:40 -0400 (EDT) From: "Benjamin R. Haskell" To: TjL cc: Zsh Users Subject: Re: equivalent of "if (( $+commands[FOO] ))" for functions? In-Reply-To: Message-ID: References: User-Agent: Alpine 2.01 (LNX 1266 2009-07-14) MIME-Version: 1.0 Content-Type: MULTIPART/MIXED; BOUNDARY="-1463810530-954713778-1344345484=:24430" ---1463810530-954713778-1344345484=:24430 Content-Type: TEXT/PLAIN; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8BIT 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 ---1463810530-954713778-1344345484=:24430--