]> arthur.barton.de Git - backup-script.git/commitdiff
Fix and mark several notices & warnings of shellcheck(1)
authorAlexander Barton <alex@barton.de>
Tue, 12 Jul 2016 10:23:19 +0000 (12:23 +0200)
committerAlexander Barton <alex@barton.de>
Tue, 12 Jul 2016 10:23:19 +0000 (12:23 +0200)
bin/backup-audit
bin/backup-script
bin/backup-script-wrapper
bin/backup-status

index 0cf89e4440c14ba70913e36bd599499c8a46edf1..ef0a9e324d6c60e571ed1afea7020310acaa83d7 100755 (executable)
@@ -10,7 +10,7 @@
 # Please read the file COPYING, README and AUTHORS for more information.
 #
 
-NAME=`basename $0`
+NAME=$(basename "$0")
 
 VERBOSE=0
 QUIET=0
@@ -55,7 +55,7 @@ BeginDiff() {
 PipeDiff() {
        local line
        IFS=
-       while read line; do
+       while read -r line; do
                echo -e "     | $line"
        done
 }
@@ -110,6 +110,7 @@ HandleSystem() {
        echo "Found latest generation in \"$latest_d\"."
 
        declare -i code=-1
+       # shellcheck source=/dev/null
        source "$latest_d/.stamp"
 
        if [[ $code -ne 0 && $code -ne 24 ]]; then
@@ -119,10 +120,12 @@ HandleSystem() {
 
        # Search previous generation without errors
        local previous_d=""
-       for d in $(ls -1dt $target/[0-9]*-[0-9]* 2>/dev/null); do
+       # shellcheck disable=SC2045
+       for d in $(ls -1dt "$target/"[0-9]*-[0-9]* 2>/dev/null); do
                [[ -d "$d" && -r "$d/.stamp" ]] || return 0
 
                declare -i code=-1
+               # shellcheck source=/dev/null
                source "$d/.stamp"
 
                if [[ $code -eq 0 || $code -eq 24 ]]; then
@@ -212,10 +215,10 @@ DiffGenerations() {
 }
 
 MkTempFiles() {
-       tmp_1=$(mktemp /tmp/$NAME.XXXXXX) || exit 1
-       tmp_2=$(mktemp /tmp/$NAME.XXXXXX) || exit 1
-       tmp_diff=$(mktemp /tmp/$NAME.XXXXXX) || exit 1
-       tmp_out=$(mktemp /tmp/$NAME.XXXXXX) || exit 1
+       tmp_1=$(mktemp "/tmp/$NAME.XXXXXX") || exit 1
+       tmp_2=$(mktemp "/tmp/$NAME.XXXXXX") || exit 1
+       tmp_diff=$(mktemp "/tmp/$NAME.XXXXXX") || exit 1
+       tmp_out=$(mktemp "/tmp/$NAME.XXXXXX") || exit 1
 }
 
 CleanUp() {
@@ -263,7 +266,7 @@ MkTempFiles
 for f in "${sys[@]}"; do
        [[ -r "$f" && -f "$f" ]] || continue
 
-       fname=`basename $f`
+       fname=$(basename "$f")
        case "$fname" in
                "backup-script.conf"|*.sh)
                        continue
index fdd642719cc68e062e8a55c86fbfcde35c002701..bd329411ae4d709c79a924ae698d70ab35245615 100755 (executable)
@@ -61,6 +61,7 @@ Usage() {
        echo
        echo "When no <system> is given, all defined systems are used."
        echo
+       # shellcheck disable=SC2086
        echo -e $config_info
        echo
        exit 2
@@ -234,6 +235,7 @@ Initialize_Last_SysTarget_Snapshot() {
        case "$fs" in
          "btrfs")
                # Search directory of last generation, if any
+               # shellcheck disable=SC2012
                last=$(ls -1d "$sys_target"/[0-9]* 2>/dev/null | sort -r | head -n1)
                if [ -n "$last" ]; then
                        if [ ! -d "$last" ]; then
@@ -658,12 +660,14 @@ for f in "${sys[@]}"; do
                $SHELL -c "$cmd"; ret=$?
                end_t=$(date "+%s")
 
-               echo "code=$ret" >"$stamp_file"
-               echo "start_t=$start_t" >>"$stamp_file"
-               echo "end_t=$end_t" >>"$stamp_file"
-               echo "cmd='$cmd'" >>"$stamp_file"
-               echo "backup_host='`hostname -f`'" >>"$stamp_file"
-               echo "backup_user='`id -un`'" >>"$stamp_file"
+               {
+                       echo "code=$ret"
+                       echo "start_t=$start_t"
+                       echo "end_t=$end_t"
+                       echo "cmd='$cmd'"
+                       echo "backup_host='$(hostname -f)'"
+                       echo "backup_user='$(id -un)'"
+               } >"$stamp_file"
        else
                echo " *** Trial run, not executing save command!"
                ret=0
@@ -698,6 +702,7 @@ for f in "${sys[@]}"; do
                fi
                # Clean up old generations
                declare -i gen_count=$generations+2
+               # shellcheck disable=SC2012
                to_delete=$(ls -1t "$sys_root" 2>/dev/null | tail -n+$gen_count | sort)
                if [[ -n "$to_delete" && $ok -eq 1 ]]; then
                        [ "$DRYRUN" -eq 0 ] \
@@ -711,6 +716,7 @@ for f in "${sys[@]}"; do
                                fi
                                last=$(stat "$dir/.stamp" 2>/dev/null | grep "^Modify: " \
                                 | cut -d':' -f2- | cut -d. -f1)
+                               # shellcheck disable=SC2086
                                echo "Removing backup from" $last "..."
                                if [ "$DRYRUN" -eq 0 ]; then
                                        DeleteSubvolume "$dir"
@@ -735,6 +741,7 @@ sync
 if [ "$DRYRUN" -eq 0 ]; then
        paths=""
        paths_zfs=""
+       # shellcheck disable=SC2086
        for dest in $(echo $destinations | sed -e 's/ /\n/g' | sort | uniq); do
                fs=$(GetFS "$dest")
                case $fs in
@@ -746,10 +753,12 @@ if [ "$DRYRUN" -eq 0 ]; then
                esac
        done
        if [ -n "$paths" ]; then
+               # shellcheck disable=SC2086
                df -h $paths
                echo
        fi
        if [ -n "$paths_zfs" ]; then
+               # shellcheck disable=SC2086
                zfs list $paths_zfs
                echo
        fi
index 2e07bc8860fc9753cbbf6b75e283c4f04380f8d4..925be37c958463a0500274eaefd0df2499f86a2f 100755 (executable)
@@ -23,10 +23,10 @@ fi
 NAME="backup-script"
 
 DELIMITER="- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
-HOST=`hostname -f`
+HOST=$(hostname -f)
 LOGFILE="/var/log/$NAME.log"
 SUBJECT="$NAME results"
-TMP=`mktemp /tmp/$NAME.XXXXXXXX` || exit 1
+TMP=$(mktemp /tmp/$NAME.XXXXXXXX) || exit 1
 
 PATH="$PATH:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
 export PATH
@@ -35,8 +35,8 @@ exec >"$TMP" 2>&1
 
 echo "$NAME Report"
 echo
-echo " - Host: `hostname -f`"
-echo " - User: `id -un`"
+echo " - Host: $(hostname -f)"
+echo " - User: $(id -un)"
 echo
 echo "Command:"
 echo "$0" "$@"
@@ -59,7 +59,7 @@ case "$r" in
                SUBJECT="$SUBJECT - with ERRORS!"
 esac
 
-cat "$TMP" | mail -s "$HOST: $SUBJECT" "$MAILTO"
+mail -s "$HOST: $SUBJECT" "$MAILTO" <"$TMP"
 
 rm -f "$TMP"
 exit $r
index c2479466dfe400c14d3bcbe36923e6e29c124bbe..a871c96c2ef18e3ff7b1c3516fad9cf153b8ebe6 100755 (executable)
@@ -10,7 +10,7 @@
 # Please read the file COPYING, README and AUTHORS for more information.
 #
 
-NAME=`basename $0`
+NAME=$(basename "$0")
 PIDFILE="/var/run/backup-script.pid"
 QUICK=0
 ONLY_ERRORS=0
@@ -59,7 +59,8 @@ Check_Size() {
        # $2: padding
 
        if [ "$QUICK" = "0" ]; then
-               size=`du -Hhs "$1" | cut -f1`
+               size=$(du -Hhs "$1" | cut -f1)
+               # shellcheck disable=SC2086
                echo "$2  - Size:" $size
        fi
 }
@@ -91,25 +92,27 @@ Check_Stamp() {
                        duration_t=$end_t-$start_t
                else
                        if [ "$(uname)" = "Linux" ]; then
-                               end=`LC_ALL=C stat "$1" | grep "^Modify: " \
-                                | cut -d':' -f2- | cut -d. -f1`
+                               end=$(LC_ALL=C stat "$1" | grep "^Modify: " \
+                                | cut -d':' -f2- | cut -d. -f1)
                        else
-                               end=`LC_ALL=C stat -f "%Sc" "$1"`
+                               end=$(LC_ALL=C stat -f "%Sc" "$1")
                        fi
                fi
+               # shellcheck disable=SC2086
                [ -n "$start" ] && echo "$2  - Start date:" $start
+               # shellcheck disable=SC2086
                [ -n "$end" ] && echo "$2  - End date:" $end
                if [ $duration_t -gt -1 ]; then
                        declare -i s=$duration_t
                        if [ $s -ge 60 ]; then
-                               declare -i m=$(($s/60))
-                               declare -i s=$(($s%60))
+                               declare -i m=$((s / 60))
+                               declare -i s=$((s % 60))
                                if [ $m -ge 60 ]; then
-                                       declare -i h=$(($m/60))
-                                       declare -i m=$(($m%60))
+                                       declare -i h=$((m / 60))
+                                       declare -i m=$((m % 60))
                                        if [ $h -ge 24 ]; then
-                                               declare -i d=$(($h/24))
-                                               declare -i h=$(($h%24))
+                                               declare -i d=$((h / 24))
+                                               declare -i h=$((h % 24))
                                                duration="${d}d${h}h${m}m${s}s"
                                        else
                                                duration="${h}h${m}m${s}s"
@@ -201,7 +204,7 @@ fi
 for f in "${sys[@]}"; do
        [[ -r "$f" && -f "$f" ]] || continue
 
-       fname=`basename $f`
+       fname=$(basename "$f")
        case "$fname" in
                "backup-script.conf"|*.sh)
                        continue