zsh-workers
 help / color / mirror / code / Atom feed
From: ZyX <zyx.vim@gmail.com>
To: zsh-workers@zsh.org
Subject: Re: Check existence of a program
Date: Tue, 1 Feb 2011 22:12:09 +0300	[thread overview]
Message-ID: <201102012212.10494@-zyx> (raw)
In-Reply-To: <4D4850F7.6060205@gmail.com>

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

       reply	other threads:[~2011-02-01 19:12 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <4D4850F7.6060205@gmail.com>
2011-02-01 19:12 ` ZyX [this message]
2011-02-01 20:21   ` ZyX

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=201102012212.10494@-zyx \
    --to=zyx.vim@gmail.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).