From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 20653 invoked by alias); 1 Feb 2011 19:12:36 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 28688 Received: (qmail 20251 invoked from network); 1 Feb 2011 19:12:34 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW, T_TO_NO_BRKTS_FREEMAIL autolearn=ham version=3.3.1 Received-SPF: pass (ns1.primenet.com.au: SPF record at _spf.google.com designates 209.85.161.43 as permitted sender) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:from:reply-to:to:subject:date:user-agent :references:in-reply-to:mime-version:content-type :content-transfer-encoding:message-id; bh=r49TRKJqkCbNSaVy2iAD1rzF25fIVG8K6/RMHsN06Dg=; b=LNJgryZdgQ96Gv95dfNEe1S3YQM1aoP3Z9yEkfhJR4vp5JowIQcKrkL+HFy/xUdgcm sjFfsWdEdLuEKQpeN+Z1A4RqLipeVlqCMe7ozzVBI03L+3WemTjjZo9l2N9C6mK/6XCq 7S3LTJRktE5gGt4t91buKCsm6emq3eeBGVTMI= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:reply-to:to:subject:date:user-agent:references:in-reply-to :mime-version:content-type:content-transfer-encoding:message-id; b=LjJIuYUsnfq6AmdZRMm9VxdpVb8IjqnOcOadwZxx2hAJFFwm6+LMXYK+/VOp8RIaAe eKcUVv0DC3mxtLVFw4lFIwG8+JDCFPi7IJ8pKBWl+6X9sOFU1yvfl2/0rdSKsYMai5n2 BkvrXPJAOb58uEiM6gC7zzv/hhoV1jJwgWgy8= From: ZyX Reply-To: 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 User-Agent: KMail/1.13.5 (Linux/2.6.36-zen1; KDE/4.5.5; x86_64; ; ) References: <4D4850F7.6060205@gmail.com> In-Reply-To: <4D4850F7.6060205@gmail.com> MIME-Version: 1.0 Content-Type: multipart/signed; boundary="nextPart1654333.nLSzMOqgO2"; protocol="application/pgp-signature"; micalg=pgp-sha1 Content-Transfer-Encoding: 7bit Message-Id: <201102012212.10494@-zyx> --nextPart1654333.nLSzMOqgO2 Content-Type: Text/Plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Reply to message =ABCheck existence of a program=BB,=20 sent 21:29:11 01 February 2011, Tuesday by Anonymous bin Ich: prog=3Dexiftime path=3D=3D$prog if [[ $? -ne 0 ]] ; then prog=3Didentify path=3D=3D$prog endif It works because zsh takes first =3D as assignment operator and expands sec= ond=20 `=3D$prog' construct into a full path. If `=3D$prog' expansion fails, it th= rows an=20 exception, exception prevents variable from being set and thus last express= ion=20 fails what is indicated in $?. If you use $(cmd) construct, then though `cmd' fails, $(cmd) that does not = care=20 about exit code just expands into output of `cmd', so last expression (whic= h is=20 variable assignment, NOT `cmd') does not fail. Also note that `=3D$prog' do= es not=20 produce new fork. Original message: > Hello! >=20 > I am having trouble checking for existence of a program. >=20 > This works: >=20 > % cat working.zsh > #!/bin/zsh > set -x > prog=3D"identify" > path=3D$(which ${prog}) > % > % ./working.zsh > +./working.zsh:3> prog=3Didentify > +./working.zsh:4> path=3D+./working.zsh:4> which identify > +./working.zsh:4> path=3D/usr/bin/identify > % >=20 > But this doesn't: >=20 > % cat notworking.zsh > #!/bin/zsh > set -x > prog=3D"exiftime" > path=3D$(which ${prog}) > if [[ ${?} -ne 0 ]]; then > prog=3D"identify" > path=3D$(which ${prog}) > fi > % > % ./notworking.zsh > +./notworking.zsh:3> prog=3Dexiftime > +./notworking.zsh:4> path=3D+./notworking.zsh:4> which exiftime > +./notworking.zsh:4> path=3D'exiftime not found' > +./notworking.zsh:5> [[ 1 -ne 0 ]] > +./notworking.zsh:6> prog=3Didentify > +./notworking.zsh:7> path=3D+./notworking.zsh:7> which identify > +./notworking.zsh:7> path=3D'identify not found' > % >=20 > Any idea? --nextPart1654333.nLSzMOqgO2 Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part. -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.16 (GNU/Linux) iEYEABECAAYFAk1IWwoACgkQkOgHluiXxU8AOACeNrYiHQkkp4Ox9uztGyv0GPLk TK8AoI6Ll58yCSCPzF6wYB0dIKCPPizJ =akGs -----END PGP SIGNATURE----- --nextPart1654333.nLSzMOqgO2--