Pull out ugly 'command -v' code to a utility function

This commit is contained in:
Bryan Boreham committed 2014-08-29 15:49:25 +01:00
1 parent c8da7131a5
commit 48bcaa7851
1 file changed
+9 -3
+9 -3
View File
@@ -17,6 +17,12 @@ usage() {
exit 1 exit 1
} }
# utility function to check whether a command can be executed by the shell
# see http://stackoverflow.com/questions/592620/how-to-check-if-a-program-exists-from-a-bash-script
command_exists () {
command -v $1 >/dev/null 2>&1
}
if [ "$COMMAND" != "status" -a "$#" -lt 2 ]; then if [ "$COMMAND" != "status" -a "$#" -lt 2 ]; then
usage usage
exit 1 exit 1
@@ -56,9 +62,9 @@ case "$COMMAND" in
;; ;;
status) status)
DOCKERIP=$(docker inspect --format='{{ .NetworkSettings.IPAddress }}' weave) DOCKERIP=$(docker inspect --format='{{ .NetworkSettings.IPAddress }}' weave)
if command -v wget >/dev/null 2>&1 ; then if command_exists wget ; then
wget -O - -q $DOCKERIP:6784/status wget -O - -q $DOCKERIP:6784/status
elif command -v curl >/dev/null 2>&1 ; then elif command_exists curl ; then
curl $DOCKERIP:6784/status curl $DOCKERIP:6784/status
else else
echo "weave status needs either curl or wget to function" >&2 echo "weave status needs either curl or wget to function" >&2
@@ -106,7 +112,7 @@ ip netns exec $NSPID ip link set $GUEST_IFNAME name $CONTAINER_IFNAME
ip netns exec $NSPID ip addr add $IPADDR dev $CONTAINER_IFNAME ip netns exec $NSPID ip addr add $IPADDR dev $CONTAINER_IFNAME
ip netns exec $NSPID ip link set $CONTAINER_IFNAME up ip netns exec $NSPID ip link set $CONTAINER_IFNAME up
if command -v ethtool >/dev/null 2>&1 if command_exists ethtool
then then
ip netns exec $NSPID ethtool -K $IFACE tx off > /dev/null ip netns exec $NSPID ethtool -K $IFACE tx off > /dev/null
else else