From 1364bf51df1f87f244b39aa4343ec924da1c8aba Mon Sep 17 00:00:00 2001 From: Alexander Barton Date: Tue, 12 Jul 2016 12:23:19 +0200 Subject: [PATCH] Fix and mark several notices & warnings of shellcheck(1) --- bin/backup-audit | 19 +++++++++++-------- bin/backup-script | 21 +++++++++++++++------ bin/backup-script-wrapper | 10 +++++----- bin/backup-status | 27 +++++++++++++++------------ 4 files changed, 46 insertions(+), 31 deletions(-) diff --git a/bin/backup-audit b/bin/backup-audit index 0cf89e4..ef0a9e3 100755 --- a/bin/backup-audit +++ b/bin/backup-audit @@ -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 diff --git a/bin/backup-script b/bin/backup-script index fdd6427..bd32941 100755 --- a/bin/backup-script +++ b/bin/backup-script @@ -61,6 +61,7 @@ Usage() { echo echo "When no 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 diff --git a/bin/backup-script-wrapper b/bin/backup-script-wrapper index 2e07bc8..925be37 100755 --- a/bin/backup-script-wrapper +++ b/bin/backup-script-wrapper @@ -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 diff --git a/bin/backup-status b/bin/backup-status index c247946..a871c96 100755 --- a/bin/backup-status +++ b/bin/backup-status @@ -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 -- 2.39.2