* Re: Check existence of a program
[not found] <4D4850F7.6060205@gmail.com>
@ 2011-02-01 19:12 ` ZyX
2011-02-01 20:21 ` ZyX
0 siblings, 1 reply; 2+ messages in thread
From: ZyX @ 2011-02-01 19:12 UTC (permalink / raw)
To: zsh-workers
[-- Attachment #1: Type: Text/Plain, Size: 1757 bytes --]
Reply to message «Check existence of a program»,
sent 21:29:11 01 February 2011, Tuesday
by Anonymous bin Ich:
prog=exiftime
path==$prog
if [[ $? -ne 0 ]] ; then
prog=identify
path==$prog
endif
It works because zsh takes first = as assignment operator and expands second
`=$prog' construct into a full path. If `=$prog' expansion fails, it throws an
exception, exception prevents variable from being set and thus last expression
fails what is indicated in $?.
If you use $(cmd) construct, then though `cmd' fails, $(cmd) that does not care
about exit code just expands into output of `cmd', so last expression (which is
variable assignment, NOT `cmd') does not fail. Also note that `=$prog' does not
produce new fork.
Original message:
> Hello!
>
> I am having trouble checking for existence of a program.
>
> This works:
>
> % cat working.zsh
> #!/bin/zsh
> set -x
> prog="identify"
> path=$(which ${prog})
> %
> % ./working.zsh
> +./working.zsh:3> prog=identify
> +./working.zsh:4> path=+./working.zsh:4> which identify
> +./working.zsh:4> path=/usr/bin/identify
> %
>
> But this doesn't:
>
> % cat notworking.zsh
> #!/bin/zsh
> set -x
> prog="exiftime"
> path=$(which ${prog})
> if [[ ${?} -ne 0 ]]; then
> prog="identify"
> path=$(which ${prog})
> fi
> %
> % ./notworking.zsh
> +./notworking.zsh:3> prog=exiftime
> +./notworking.zsh:4> path=+./notworking.zsh:4> which exiftime
> +./notworking.zsh:4> path='exiftime not found'
> +./notworking.zsh:5> [[ 1 -ne 0 ]]
> +./notworking.zsh:6> prog=identify
> +./notworking.zsh:7> path=+./notworking.zsh:7> which identify
> +./notworking.zsh:7> path='identify not found'
> %
>
> Any idea?
[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Check existence of a program
2011-02-01 19:12 ` Check existence of a program ZyX
@ 2011-02-01 20:21 ` ZyX
0 siblings, 0 replies; 2+ messages in thread
From: ZyX @ 2011-02-01 20:21 UTC (permalink / raw)
To: zsh-workers
[-- Attachment #1: Type: Text/Plain, Size: 2215 bytes --]
Reply to message «Re: Check existence of a program»,
sent 22:12:09 01 February 2011, Tuesday
by ZyX:
Sorry, I should have read your question more correctly (and test $(cmd) variant
better). $(cmd) actually propagates exit code, so the reason is overriding of
$path as others have mentioned.
Original message:
> Reply to message «Check existence of a program»,
> sent 21:29:11 01 February 2011, Tuesday
> by Anonymous bin Ich:
>
> prog=exiftime
> path==$prog
> if [[ $? -ne 0 ]] ; then
> prog=identify
> path==$prog
> endif
>
> It works because zsh takes first = as assignment operator and expands
> second `=$prog' construct into a full path. If `=$prog' expansion fails,
> it throws an exception, exception prevents variable from being set and
> thus last expression fails what is indicated in $?.
>
> If you use $(cmd) construct, then though `cmd' fails, $(cmd) that does not
> care about exit code just expands into output of `cmd', so last expression
> (which is variable assignment, NOT `cmd') does not fail. Also note that
> `=$prog' does not produce new fork.
>
> Original message:
> > Hello!
> >
> > I am having trouble checking for existence of a program.
> >
> > This works:
> >
> > % cat working.zsh
> > #!/bin/zsh
> > set -x
> > prog="identify"
> > path=$(which ${prog})
> > %
> > % ./working.zsh
> > +./working.zsh:3> prog=identify
> > +./working.zsh:4> path=+./working.zsh:4> which identify
> > +./working.zsh:4> path=/usr/bin/identify
> > %
> >
> > But this doesn't:
> >
> > % cat notworking.zsh
> > #!/bin/zsh
> > set -x
> > prog="exiftime"
> > path=$(which ${prog})
> > if [[ ${?} -ne 0 ]]; then
> >
> > prog="identify"
> > path=$(which ${prog})
> >
> > fi
> > %
> > % ./notworking.zsh
> > +./notworking.zsh:3> prog=exiftime
> > +./notworking.zsh:4> path=+./notworking.zsh:4> which exiftime
> > +./notworking.zsh:4> path='exiftime not found'
> > +./notworking.zsh:5> [[ 1 -ne 0 ]]
> > +./notworking.zsh:6> prog=identify
> > +./notworking.zsh:7> path=+./notworking.zsh:7> which identify
> > +./notworking.zsh:7> path='identify not found'
> > %
> >
> > Any idea?
[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2011-02-01 20:21 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <4D4850F7.6060205@gmail.com>
2011-02-01 19:12 ` Check existence of a program ZyX
2011-02-01 20:21 ` ZyX
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).