[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Qemu-trivial] [PATCH v2] tests: Use "command -v" instead of which(1
Re: [Qemu-trivial] [PATCH v2] tests: Use "command -v" instead of which(1) in shell scripts
Wed, 19 Nov 2014 06:19:29 -0700
Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0
On 11/19/2014 12:07 AM, Fam Zheng wrote:
> When which(1) is not installed, we would complain "perl not found"
> because it's the first set_prog_path check. The error message is
> Fix it by using "command -v", a native way to query the existence of a
> Suggested-by: Eric Blake <address@hidden>
> Signed-off-by: Fam Zheng <address@hidden>
> +++ b/tests/qemu-iotests/common.config
> @@ -47,7 +47,7 @@ export PWD=`pwd`
> # $1 = prog to look for, $2* = default pathnames if not found in $PATH
> - p=`which $1 2> /dev/null`
> + p=`command -v $1 2> /dev/null`
Reviewed-by: Eric Blake <address@hidden>
> if [ -n "$p" -a -x "$p" ]; then
Unrelated: this line "works" because this is a /bin/bash script, but it
is non-portable. Use of -a and -o inside  is a mistake waiting to
happen. For example, is [ ! "$a" -a "$b" ] supposed to be true or false
for all values of $a and $b? Naively, this says return true if '! "$a"'
(a is empty) and '"$b"' (b is non-empty); but if $a is '(' and $b is ')'
it could also be parsed as returning the negation of whether the
parenthesized string "-a" is non-empty.
Use of -a and -o in [] is a bit better, but I still HIGHLY recommend
that constructs like this be rewritten as [ -n "$p" ] && [ -x "$p" ] for
avoidance of confusion and prevention of copy-pasting the test to
non-bash shells. But that would be a separate patch.
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Description: OpenPGP digital signature