qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH v2] tests: Use "command -v" instead of which(1) in shell scripts
@ 2014-11-19  7:07 Fam Zheng
  2014-11-19 13:19 ` Eric Blake
  2014-11-26 16:21 ` Stefan Hajnoczi
  0 siblings, 2 replies; 4+ messages in thread
From: Fam Zheng @ 2014-11-19  7:07 UTC (permalink / raw)
  To: qemu-devel; +Cc: kwolf, qemu-trivial, mjt, mreitz, stefanha

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

Fix it by using "command -v", a native way to query the existence of a
command.

Suggested-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Fam Zheng <famz@redhat.com>

---
v2: Use "command -v" as suggested by Eric. Also change a few other
occasions of which(1) in tests/qemu-iotests/common.
---
 tests/qemu-iotests/common        | 8 ++++----
 tests/qemu-iotests/common.config | 2 +-
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/tests/qemu-iotests/common b/tests/qemu-iotests/common
index 9e12bec..bc27f6a 100644
--- a/tests/qemu-iotests/common
+++ b/tests/qemu-iotests/common
@@ -289,10 +289,10 @@ testlist options
 
             if [ ! -z "$DISPLAY" ]
             then
-                which xdiff >/dev/null 2>&1 && diff=xdiff
-                which gdiff >/dev/null 2>&1 && diff=gdiff
-                which tkdiff >/dev/null 2>&1 && diff=tkdiff
-                which xxdiff >/dev/null 2>&1 && diff=xxdiff
+                command -v xdiff >/dev/null 2>&1 && diff=xdiff
+                command -v gdiff >/dev/null 2>&1 && diff=gdiff
+                command -v tkdiff >/dev/null 2>&1 && diff=tkdiff
+                command -v xxdiff >/dev/null 2>&1 && diff=xxdiff
             fi
             ;;
 
diff --git a/tests/qemu-iotests/common.config b/tests/qemu-iotests/common.config
index bd6790b..91a5ef6 100644
--- a/tests/qemu-iotests/common.config
+++ 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
 set_prog_path()
 {
-    p=`which $1 2> /dev/null`
+    p=`command -v $1 2> /dev/null`
     if [ -n "$p" -a -x "$p" ]; then
         echo $p
         return 0
-- 
1.9.3

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

* Re: [Qemu-devel] [PATCH v2] tests: Use "command -v" instead of which(1) in shell scripts
  2014-11-19  7:07 [Qemu-devel] [PATCH v2] tests: Use "command -v" instead of which(1) in shell scripts Fam Zheng
@ 2014-11-19 13:19 ` Eric Blake
  2014-11-19 14:07   ` Peter Maydell
  2014-11-26 16:21 ` Stefan Hajnoczi
  1 sibling, 1 reply; 4+ messages in thread
From: Eric Blake @ 2014-11-19 13:19 UTC (permalink / raw)
  To: Fam Zheng, qemu-devel; +Cc: qemu-trivial, kwolf, mjt, stefanha, mreitz

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

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
> wrong.
> 
> Fix it by using "command -v", a native way to query the existence of a
> command.
> 
> Suggested-by: Eric Blake <eblake@redhat.com>
> Signed-off-by: Fam Zheng <famz@redhat.com>

> +++ 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
>  set_prog_path()
>  {
> -    p=`which $1 2> /dev/null`
> +    p=`command -v $1 2> /dev/null`

Reviewed-by: Eric Blake <eblake@redhat.com>

>      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


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 539 bytes --]

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

* Re: [Qemu-devel] [PATCH v2] tests: Use "command -v" instead of which(1) in shell scripts
  2014-11-19 13:19 ` Eric Blake
@ 2014-11-19 14:07   ` Peter Maydell
  0 siblings, 0 replies; 4+ messages in thread
From: Peter Maydell @ 2014-11-19 14:07 UTC (permalink / raw)
  To: Eric Blake
  Cc: Kevin Wolf, Fam Zheng, QEMU Trivial, Michael Tokarev,
	QEMU Developers, Max Reitz, Stefan Hajnoczi

On 19 November 2014 13:19, Eric Blake <eblake@redhat.com> wrote:
> 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.

Yeah, this is one of those issues I tend to pick up in
patches which change our shell scripts, but we still have
a fair amount of existing code that isn't up to that standard.

-- PMM

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

* Re: [Qemu-devel] [PATCH v2] tests: Use "command -v" instead of which(1) in shell scripts
  2014-11-19  7:07 [Qemu-devel] [PATCH v2] tests: Use "command -v" instead of which(1) in shell scripts Fam Zheng
  2014-11-19 13:19 ` Eric Blake
@ 2014-11-26 16:21 ` Stefan Hajnoczi
  1 sibling, 0 replies; 4+ messages in thread
From: Stefan Hajnoczi @ 2014-11-26 16:21 UTC (permalink / raw)
  To: Fam Zheng; +Cc: kwolf, qemu-trivial, mjt, qemu-devel, mreitz, stefanha

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

On Wed, Nov 19, 2014 at 03:07:12PM +0800, 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
> wrong.
> 
> Fix it by using "command -v", a native way to query the existence of a
> command.
> 
> Suggested-by: Eric Blake <eblake@redhat.com>
> Signed-off-by: Fam Zheng <famz@redhat.com>
> 
> ---
> v2: Use "command -v" as suggested by Eric. Also change a few other
> occasions of which(1) in tests/qemu-iotests/common.
> ---
>  tests/qemu-iotests/common        | 8 ++++----
>  tests/qemu-iotests/common.config | 2 +-
>  2 files changed, 5 insertions(+), 5 deletions(-)

Thanks, applied to my block-next tree:
https://github.com/stefanha/qemu/commits/block-next

Stefan

[-- Attachment #2: Type: application/pgp-signature, Size: 473 bytes --]

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

end of thread, other threads:[~2014-11-26 16:21 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-11-19  7:07 [Qemu-devel] [PATCH v2] tests: Use "command -v" instead of which(1) in shell scripts Fam Zheng
2014-11-19 13:19 ` Eric Blake
2014-11-19 14:07   ` Peter Maydell
2014-11-26 16:21 ` Stefan Hajnoczi

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