zsh-users
 help / color / mirror / code / Atom feed
* Incorporating (( $+commands[foo] )) into a larger "if" statement
@ 2015-08-05 22:00 TJ Luoma
  2015-08-06  1:57 ` Bart Schaefer
  2015-08-06  5:53 ` id5
  0 siblings, 2 replies; 5+ messages in thread
From: TJ Luoma @ 2015-08-05 22:00 UTC (permalink / raw)
  To: Zsh-Users List

[-- Attachment #1: Type: text/plain, Size: 955 bytes --]

OK, so I am apparently mis-understanding something or I've forgotten
something about how this works.

Some time ago, someone from the list suggested that I could do this instead
of using `which`

if (( $+commands[foo] ))
then

echo yes

fi


that works fine. However, what I am trying to do now is check for other
things at the same time.

For example, if I want to check that the command `growlnotify` is found in
the $PATH and I want to check to see if `pgrep` finds 'Growl' is running.
In that instance I would use this to check `pgrep`:

if [ "`pgrep -x Growl`" != "" ]
then
echo Not Running
else
echo Is Running
fi

but I can't figure out how to put that together with

if (( $+commands[growlnotify] ))

OR, if I want to see if the command `bbedit` is found, and I want to check
that a variable $APP is equal to "bbedit". I would do the latter

if [ "$APP:l" == "bbedit" ]
then
echo yes
else
echo no
fi


Any help would be appreciated.

Thanks!

TjL

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

* Re: Incorporating (( $+commands[foo] )) into a larger "if" statement
  2015-08-05 22:00 Incorporating (( $+commands[foo] )) into a larger "if" statement TJ Luoma
@ 2015-08-06  1:57 ` Bart Schaefer
  2015-08-06  5:53 ` id5
  1 sibling, 0 replies; 5+ messages in thread
From: Bart Schaefer @ 2015-08-06  1:57 UTC (permalink / raw)
  To: Zsh-Users List

On Aug 5,  6:00pm, TJ Luoma wrote:
} 
} if (( $+commands[foo] ))

$+commands[foo] is either 1 (commands[foo] is set) or 0 (not set).

The (( )) are a math context evaluation, so (( 1 )) is true.  This
whole expression is a single shell command, so it can be used in any
position that an ordinary shell command can be used.

Thus to use this with other tests, you can either:

- Combine the test forms with && or || like:

    if (( $+commands[growlnotify] )) && [ "`pgrep -x Growl`" != "" ]

- Test for 1 or 0 within the ordinary test such as:

    if [ $+commands[growlnotify] = 1 -a "`pgrep -x Growl`" != "" ]


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

* Re: Incorporating (( $+commands[foo] )) into a larger "if" statement
  2015-08-05 22:00 Incorporating (( $+commands[foo] )) into a larger "if" statement TJ Luoma
  2015-08-06  1:57 ` Bart Schaefer
@ 2015-08-06  5:53 ` id5
  2015-08-06 14:57   ` Bart Schaefer
  1 sibling, 1 reply; 5+ messages in thread
From: id5 @ 2015-08-06  5:53 UTC (permalink / raw)
  To: zsh-users

if [[ $+commands[foo] -ge 1 && ... ]]; then
  echo  yes
fi

OR

if [[ (( $+commands[foo] )) && ... ]]; then
  echo yes
fi

OR (the very straightforward way)

if (( $+commands[foo] )); then
  if [[ ... ]]; then
   echo yes
  fi
fi

Also: Please note, that '[[' should be used instead of '[', for the
former is way saner in its behavior.

- René

Am 06.08.2015 um 00:00 schrieb TJ Luoma:
> OK, so I am apparently mis-understanding something or I've forgotten
> something about how this works.
> 
> Some time ago, someone from the list suggested that I could do this instead
> of using `which`
> 
> if (( $+commands[foo] ))
> then
> 
> echo yes
> 
> fi
> 
> 
> that works fine. However, what I am trying to do now is check for other
> things at the same time.
> 
> For example, if I want to check that the command `growlnotify` is found in
> the $PATH and I want to check to see if `pgrep` finds 'Growl' is running.
> In that instance I would use this to check `pgrep`:
> 
> if [ "`pgrep -x Growl`" != "" ]
> then
> echo Not Running
> else
> echo Is Running
> fi
> 
> but I can't figure out how to put that together with
> 
> if (( $+commands[growlnotify] ))
> 
> OR, if I want to see if the command `bbedit` is found, and I want to check
> that a variable $APP is equal to "bbedit". I would do the latter
> 
> if [ "$APP:l" == "bbedit" ]
> then
> echo yes
> else
> echo no
> fi
> 
> 
> Any help would be appreciated.
> 
> Thanks!
> 
> TjL
> 


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

* Re: Incorporating (( $+commands[foo] )) into a larger "if" statement
  2015-08-06  5:53 ` id5
@ 2015-08-06 14:57   ` Bart Schaefer
  2015-08-06 20:59     ` René Neumann
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2015-08-06 14:57 UTC (permalink / raw)
  To: zsh-users

On Aug 6,  7:53am, id5 wrote:
}
} if [[ (( $+commands[foo] )) && ... ]]; then
}   echo yes
} fi

That doesn't work.  [[ (( ... )) ]] is only parsed at all because you
are allowed to have parenthesized subexpressions inside [[ ]], it is
not interpreted as math in that context.

I suspect you meant

    if (( $+commands[foo] )) && [[ ... ]]; then


Aside:  You somehow posted your reply with "From: id5" (no domain name in
the email address).  You should fix that.


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

* Re: Incorporating (( $+commands[foo] )) into a larger "if" statement
  2015-08-06 14:57   ` Bart Schaefer
@ 2015-08-06 20:59     ` René Neumann
  0 siblings, 0 replies; 5+ messages in thread
From: René Neumann @ 2015-08-06 20:59 UTC (permalink / raw)
  To: zsh-users

Am 06.08.2015 um 16:57 schrieb Bart Schaefer:
> On Aug 6,  7:53am, id5 wrote:
> }
> } if [[ (( $+commands[foo] )) && ... ]]; then
> }   echo yes
> } fi
> 
> That doesn't work.  [[ (( ... )) ]] is only parsed at all because you
> are allowed to have parenthesized subexpressions inside [[ ]], it is
> not interpreted as math in that context.

Ah, I see. I tested it beforehand, but it turned out it just seemed to
work, because in the end [[ non_empty_string ]] is true. Thanks for
pointing this out.

> I suspect you meant
> 
>     if (( $+commands[foo] )) && [[ ... ]]; then

Yes. Though this variant looks somewhat odd, as one is mixing (( )) and
[[ ]] in one chain...

> Aside:  You somehow posted your reply with "From: id5" (no domain name in
> the email address).  You should fix that.

Mhm, seems like my TB-Identities-Plugin was broken. Should be fixed now,
hopefully.

- René


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

end of thread, other threads:[~2015-08-06 20:59 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-05 22:00 Incorporating (( $+commands[foo] )) into a larger "if" statement TJ Luoma
2015-08-06  1:57 ` Bart Schaefer
2015-08-06  5:53 ` id5
2015-08-06 14:57   ` Bart Schaefer
2015-08-06 20:59     ` René Neumann

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