Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Check existence of a program



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: signature.asc
Description: This is a digitally signed message part.



Messages sorted by: Reverse Date, Date, Thread, Author