From: Ville Skyttä <ville.skytta@iki.fi>
Date: Fri, 12 Nov 2021 21:47:56 +0000 (+0200)
Subject: Use `command -v` instead of `which`
X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=32f057d08a0b28d218cd5c51a31a8e6502c48854;p=users%2Fdwmw2%2Fvpnc-scripts.git

Use `command -v` instead of `which`

`which` is not standard and might not be installed, whereas `command -v`
is in POSIX and its predecessor specs at least since 1994,
specified to produce no output when the arg command is not found.
https://pubs.opengroup.org/onlinepubs/7908799/xcu/command.html

`which` is also deprecated in Debian's debianutils 5.0+ and emits a
warning when used. At the moment, 5.0+ is only in Debian unstable, and
there is no other package in it providing `which`.
https://salsa.debian.org/debian/debianutils/-/commit/3a8dd10b4502f7bae8fc6973c13ce23fc9da7efb
---

diff --git a/tests/common.sh b/tests/common.sh
index 5fbe882..28f0b4a 100644
--- a/tests/common.sh
+++ b/tests/common.sh
@@ -19,10 +19,10 @@
 builddir=${builddir:-.}
 srcdir=${srcdir:-.}
 
-OPENCONNECT=${OPENCONNECT:-$(which openconnect)}
-OCCTL=${OCCTL:-$(which occtl)}
-OCSERV=${OCSERV:-$(which ocserv)}
-IP=${IP:-$(which ip)}
+OPENCONNECT=${OPENCONNECT:-$(command -v openconnect)}
+OCCTL=${OCCTL:-$(command -v occtl)}
+OCSERV=${OCSERV:-$(command -v ocserv)}
+IP=${IP:-$(command -v ip)}
 
 if test -z "${OPENCONNECT}" || ! test -x ${OPENCONNECT};then
 	echo "You need openconnect to run this test"
@@ -81,14 +81,14 @@ update_config() {
 # command in the caller's PFCMD, or exit, indicating an unsupported
 # test.  Prefer ss from iproute2 over the older netstat.
 have_port_finder() {
-	for file in $(which ss 2> /dev/null) /*bin/ss /usr/*bin/ss /usr/local/*bin/ss;do
+	for file in $(command -v ss) /*bin/ss /usr/*bin/ss /usr/local/*bin/ss;do
 		if test -x "$file";then
 			PFCMD="$file";return 0
 		fi
 	done
 
 	if test -z "$PFCMD";then
-	for file in $(which netstat 2> /dev/null) /bin/netstat /usr/bin/netstat /usr/local/bin/netstat;do
+	for file in $(command -v netstat) /bin/netstat /usr/bin/netstat /usr/local/bin/netstat;do
 		if test -x "$file";then
 			PFCMD="$file";return 0
 		fi
diff --git a/tests/ns.sh b/tests/ns.sh
index 4d112ab..2b65f81 100644
--- a/tests/ns.sh
+++ b/tests/ns.sh
@@ -37,7 +37,7 @@
 
 PATH=${PATH}:/usr/sbin
 if test -z ${IP};then
-	IP=$(which ip)
+	IP=$(command -v ip)
 fi
 
 if test "$(id -u)" != "0";then
diff --git a/vpnc-script b/vpnc-script
index f75cc34..113b628 100755
--- a/vpnc-script
+++ b/vpnc-script
@@ -120,7 +120,7 @@ else
 fi
 
 if [ "$OS" = "Linux" ]; then
-	IPROUTE="`which ip 2> /dev/null | grep '^/'`"
+	IPROUTE="`command -v ip | grep '^/'`"
 	ifconfig_syntax_ptp="pointopoint"
 	route_syntax_gw="gw"
 	route_syntax_del="del"
@@ -132,7 +132,7 @@ if [ "$OS" = "Linux" ]; then
 	ifconfig_syntax_del() { case "$1" in *:*) echo del "$1" ;; *) echo 0.0.0.0 ;; esac; }
 	netstat_syntax_ipv6="-6"
 else
-	# iproute2 is Linux only; if `which ip` returns something on another OS, it's likely an unrelated tool
+	# iproute2 is Linux only; if `command -v ip` returns something on another OS, it's likely an unrelated tool
 	# (see https://github.com/dlenski/openconnect/issues/132#issuecomment-470475009)
 	IPROUTE=""
 	ifconfig_syntax_ptp=""
diff --git a/vpnc-script-ptrtd b/vpnc-script-ptrtd
index b97143b..d0a547b 100755
--- a/vpnc-script-ptrtd
+++ b/vpnc-script-ptrtd
@@ -39,7 +39,7 @@
 #  - AAAA queries return the A result, converted to the above IPv6 space.
 #  - PTR queries within the IPv6 space converted appropriately.
 
-IP="`which ip 2> /dev/null | grep '^/'`"
+IP="`command -v ip | grep '^/'`"
 
 connect_parent()
 {
diff --git a/vpnc-script-sshd b/vpnc-script-sshd
index 40e9205..d36ae19 100755
--- a/vpnc-script-sshd
+++ b/vpnc-script-sshd
@@ -154,7 +154,7 @@
 # David Woodhouse <dwmw2@infradead.org>
 # 2009-06-06
 
-IP="`which ip 2> /dev/null | grep '^/'`"
+IP="`command -v ip | grep '^/'`"
 SCRIPTNAME=`basename $0`
 NETNSNAME=$SCRIPTNAME