Fix shellcheck(1) warnings and make script more portable
authorAlexander Barton <alex@barton.de>
Tue, 20 Dec 2016 20:32:48 +0000 (21:32 +0100)
committerAlexander Barton <alex@barton.de>
Tue, 20 Dec 2016 20:32:48 +0000 (21:32 +0100)
scripts/trigger-rcmd

index d1d13f3..75aeddb 100755 (executable)
@@ -1,7 +1,7 @@
 #!/bin/sh
 #
 # trigger-rcmp: Trigger remote commands
-# Copyright (c)2014-2015 Alexander Barton (alex@barton.de)
+# Copyright (c)2014-2016 Alexander Barton (alex@barton.de)
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -10,7 +10,7 @@
 # Please read the file COPYING, README and AUTHORS for more information.
 #
 
-NAME="`basename "$0"`"
+NAME=$(basename "$0")
 RCMD_D="/usr/local/etc/rcmd.d"
 
 PATH="$PATH:/usr/local/sbin:/usr/sbin"
@@ -30,6 +30,8 @@ do_rcmd() {
        BOOT_DELAY="1m"
        SHUTDOWN_TIME="+10"
 
+       # Read in job/configuration file, but ignore shellcheck(1) warnings:
+       # shellcheck disable=1090
        . "$cnf"
 
        if [ $? -ne 0 ]; then
@@ -37,7 +39,7 @@ do_rcmd() {
                return 2
        fi
 
-       local NAME="$NAME($1)"
+       NAME="$NAME($1)"
 
        echo "$NAME: Checking system \"$SYSTEM\" ..."
        fping -c1 -q "$SYSTEM" 2>/dev/null
@@ -48,13 +50,13 @@ do_rcmd() {
                fi
 
                echo "$NAME: \"$SYSTEM\" seems to be down, wake it up ..."
-               etherwake -b -i "$IFACE" `echo "$SYSTEM" | cut -d'.' -f1`
+               etherwake -b -i "$IFACE" "$(echo "$SYSTEM" | cut -d'.' -f1)"
                if [ $? -ne 0 ]; then
                        echo "$NAME: Failed to wake \"$SYSTEM\"!" >&2
                        return 3
                fi
                echo "$NAME: Waiting for \"$SYSTEM\" to respond ..."
-               for i in `seq 1 $TIMEOUT`; do
+               for i in $(seq 1 $TIMEOUT); do
                        fping -c1 -q "$SYSTEM" 2>/dev/null
                        [ $? -ne 0 ] || break
                        sleep 1s
@@ -71,7 +73,7 @@ do_rcmd() {
                rsys_was_running=1
        fi
 
-       for i in `seq 1 $SSH_CHECKS`; do
+       for i in $(seq 1 $SSH_CHECKS); do
                echo "$NAME: Checking SSH connection ($i/$SSH_CHECKS) ..."
                ssh -q "$SYSTEM" true >/dev/null 2>&1 && break
                echo "$NAME: SSH on \"$SYSTEM\" not ready. Waiting ..."
@@ -87,6 +89,7 @@ do_rcmd() {
        echo "$NAME: Calling \"$CMD\" on \"$SYSTEM\" ..."
        echo
 
+       # shellcheck disable=2029
        ssh -q "$SYSTEM" "$CMD" </dev/null ; r=$?
 
        echo
@@ -94,9 +97,10 @@ do_rcmd() {
        sleep 2
 
        if [ "$rsys_was_running" -eq 0 ]; then
-               rsys_users=`ssh -q "$SYSTEM" w | tail -n +3 | wc -l`
+               rsys_users=$(ssh -q "$SYSTEM" w | tail -n +3 | wc -l)
                if [ "$rsys_users" = "0" ]; then
                        echo "$NAME: Power off \"$SYSTEM\" again ..."
+                       # shellcheck disable=2029
                        ssh -q "$SYSTEM" "sync; shutdown -hP $SHUTDOWN_TIME" >/dev/null
                        if [ $? -ne 0 ]; then
                                echo "$NAME: Failed to power off \"$SYSTEM\"!" >&2
@@ -104,7 +108,7 @@ do_rcmd() {
                        fi
                else
                        echo "$NAME: Not shutting down \"$SYSTEM\", \c"
-                       [ $rsys_users -eq 1 ] \
+                       [ "$rsys_users" -eq 1 ] \
                                && echo "There is 1 user logged in!" \
                                || echo "There are $rsys_users users logged in!"
                fi
@@ -119,8 +123,7 @@ NO_WAKE=
 
 # Check required tools
 for t in fping ssh etherwake; do
-       type "$t" >/dev/null 2>&1
-       if [ $? -ne 0 ]; then
+       if ! command -v "$t" >/dev/null 2>&1; then
                echo "$NAME: Required tool \"$t\" missing, aborting!"
                exit 3
        fi
@@ -147,9 +150,9 @@ while [ $# -gt 0 ]; do
                echo "$NAME: Can't read \"$cnf\"!"
                r=1
        else
-               echo "$NAME: Working on \"$1\" - `date "+%Y-%m-%d %H:%M:%S"`"
+               echo "$NAME: Working on \"$1\" - $(date "+%Y-%m-%d %H:%M:%S")"
                do_rcmd "$1" ; r=$?
-               echo "$NAME: Done ($r) - `date "+%Y-%m-%d %H:%M:%S"`"
+               echo "$NAME: Done ($r) - $(date "+%Y-%m-%d %H:%M:%S")"
        fi
        [ $r -ne 0 ] && result=1
        shift