zsh-users
 help / color / mirror / code / Atom feed
* Check existence of a program
@ 2011-02-01 18:29 Anonymous bin Ich
  2011-02-01 19:01 ` Julien Nicoulaud
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Anonymous bin Ich @ 2011-02-01 18:29 UTC (permalink / raw)
  To: zsh-users

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?


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

* Re: Check existence of a program
  2011-02-01 18:29 Check existence of a program Anonymous bin Ich
@ 2011-02-01 19:01 ` Julien Nicoulaud
  2011-02-01 19:16 ` Benjamin R. Haskell
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Julien Nicoulaud @ 2011-02-01 19:01 UTC (permalink / raw)
  To: Anonymous bin Ich; +Cc: zsh-users

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

if type exiftime &>/dev/null; then ...

2011/2/1 Anonymous bin Ich <ichbinanon@gmail.com>

> 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?
>

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

* Re: Check existence of a program
  2011-02-01 18:29 Check existence of a program Anonymous bin Ich
  2011-02-01 19:01 ` Julien Nicoulaud
@ 2011-02-01 19:16 ` Benjamin R. Haskell
  2011-02-01 19:16 ` Jérémie Roquet
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Benjamin R. Haskell @ 2011-02-01 19:16 UTC (permalink / raw)
  To: Anonymous bin Ich; +Cc: zsh-users

On Tue, 1 Feb 2011, Anonymous bin Ich wrote:

> 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?
>

You shouldn't use a variable named 'path', is perhaps what's going on 
here.  That one bites me occasionally: $path is an array whose contents 
are specially tied to $PATH.  So, by setting path=$(which $prog), you're 
setting your $PATH parameter, which is probably undesirable.

I tend to use this idiom for checking that a command exists:

if (( $+commands[command-name] )) ; then
 	# it exists
fi

In general, (( $+param )) returns true if param is set.  $commands is an 
associative array that lists commands.  (Provided by the zsh/param 
module, IIRC.)  (( $+commands[command-name] )) returns true if the key 
'command-name' exists.

So, e.g.:

#!/bin/zsh
set -x
unset touse
for prog in exiftime identify ; do
 	(( $+commands[$prog] )) || continue
 	touse=$prog
 	break
done
(( $+touse )) || { echo "Couldn't find a usable EXIF info program" ; return 1 }
$touse /path/to/image.jpg

-- 
Best,
Ben


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

* Re: Check existence of a program
  2011-02-01 18:29 Check existence of a program Anonymous bin Ich
  2011-02-01 19:01 ` Julien Nicoulaud
  2011-02-01 19:16 ` Benjamin R. Haskell
@ 2011-02-01 19:16 ` Jérémie Roquet
  2011-02-01 20:16 ` Bart Schaefer
  2011-02-01 21:39 ` Atom Smasher
  4 siblings, 0 replies; 6+ messages in thread
From: Jérémie Roquet @ 2011-02-01 19:16 UTC (permalink / raw)
  To: zsh-users

Sorry for the double-reply, I missed the list.

2011/2/1 Anonymous bin Ich <ichbinanon@gmail.com>:
> 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?
>

You have overridden $path :-)

-- 
Jérémie


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

* Re: Check existence of a program
  2011-02-01 18:29 Check existence of a program Anonymous bin Ich
                   ` (2 preceding siblings ...)
  2011-02-01 19:16 ` Jérémie Roquet
@ 2011-02-01 20:16 ` Bart Schaefer
  2011-02-01 21:39 ` Atom Smasher
  4 siblings, 0 replies; 6+ messages in thread
From: Bart Schaefer @ 2011-02-01 20:16 UTC (permalink / raw)
  To: zsh-users

On Feb 1, 11:59pm, Anonymous bin Ich wrote:
}
} % cat notworking.zsh
} #!/bin/zsh
} set -x
} prog="exiftime"
} path=$(which ${prog})

As has already been pointed out, assigning to $path alters the value of
$PATH, which makes it impossible to find any other executables.

One way to prevent yourself from getting into trouble with this:

    readonly path

Now you can still assign to PATH (and the new value is reflected in
the $path array), but you can't accidentally stomp on it via direct
assignment to path.


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

* Re: Check existence of a program
  2011-02-01 18:29 Check existence of a program Anonymous bin Ich
                   ` (3 preceding siblings ...)
  2011-02-01 20:16 ` Bart Schaefer
@ 2011-02-01 21:39 ` Atom Smasher
  4 siblings, 0 replies; 6+ messages in thread
From: Atom Smasher @ 2011-02-01 21:39 UTC (permalink / raw)
  To: zsh-users, Anonymous bin Ich

On Tue, 1 Feb 2011, Anonymous bin Ich wrote:

> I am having trouble checking for existence of a program.
==============

[[ -x $(whence -p program) ]] && echo $(whence -p program)


-- 
         ...atom

  ________________________
  http://atom.smasher.org/
  762A 3B98 A3C3 96C9 C6B7 582A B88D 52E4 D9F5 7808
  -------------------------------------------------

 	"We do many things at the federal level
 	 that would be considered dishonest and
 	 illegal if done in the private sector."
 		-- Ronald Reagan


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

end of thread, other threads:[~2011-02-01 21:46 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-02-01 18:29 Check existence of a program Anonymous bin Ich
2011-02-01 19:01 ` Julien Nicoulaud
2011-02-01 19:16 ` Benjamin R. Haskell
2011-02-01 19:16 ` Jérémie Roquet
2011-02-01 20:16 ` Bart Schaefer
2011-02-01 21:39 ` Atom Smasher

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