Run shellcheck on script files
authorColin Clark <colin.clark@cclark.uk>
Sat, 10 Jun 2023 11:14:13 +0000 (12:14 +0100)
committerColin Clark <colin.clark@cclark.uk>
Sat, 10 Jun 2023 11:14:13 +0000 (12:14 +0100)
Run using:

for file in $(find . -not \( -path ./subprojects -prune \) -name "*.sh")
do
realfile=$(realpath "$file")
shellcheck --norc --format=diff --shell sh
--enable=add-default-case,avoid-nullary-conditions,check-unassigned-uppercase,deprecate-which,quote-safe-variables
"$realfile"| patch "$realfile"
done
exit

Plus manual edits.

13 files changed:
geeqie-install-debian.sh
gen_changelog.sh
plugins/camera-import/geeqie-camera-import-hook-script
plugins/image-crop/geeqie-image-crop
plugins/rotate/geeqie-rotate
plugins/symlink/geeqie-symlink
plugins/tethered-photography/geeqie-tethered-photography
plugins/tethered-photography/geeqie-tethered-photography-hook-script
po/regen_potfiles.sh
scripts/generate-linuxdeploy-for-arm.sh
scripts/keyword_merge.sh
scripts/new-release.sh
version.sh

index 5e9c589..eaf00c9 100755 (executable)
@@ -156,8 +156,8 @@ systemProfile()
                        then
                                DIST="${DIST}[$(tr "\n" ' ' < /etc/UnitedLinux-release | sed s/VERSION.*//)]"
                        fi
-                       OS=$(lowercase $OS)
-                       DistroBasedOn=$(lowercase $DistroBasedOn)
+                       OS=$(lowercase "$OS")
+                       DistroBasedOn=$(lowercase "$DistroBasedOn")
                        readonly OS
                        readonly DIST
                        readonly DistroBasedOn
@@ -244,7 +244,7 @@ package_query()
                        status=0
                fi
        fi
-       return $status
+       return "$status"
 }
 
 package_install()
@@ -343,7 +343,7 @@ do
        esac
 done
 
-if [ "$LIST" ]
+if [ -n "$LIST" ]
 then
        printf '%b\n' "Essential libraries:"
        for file in $essential_array
@@ -417,12 +417,11 @@ then
        exit 1
 fi" > "$install_pass_script"
 chmod +x "$install_pass_script"
-export SUDO_ASKPASS=$install_pass_script
+export SUDO_ASKPASS="$install_pass_script"
 
 if [ "$gtk_version" = "Uninstall" ]
 then
        uninstall
-       exit
 fi
 
 # Put the install log in tmp, to avoid writing to PWD during a new install
@@ -453,7 +452,7 @@ do
        i=$((i + 1))
 done
 
-kill $zen_pid 2> /dev/null
+kill "$zen_pid" 2> /dev/null
 
 # Ask the user which options to install
 if [ -n "$option_string" ]
@@ -511,7 +510,7 @@ fi
 printf '%b\n' "20" > "$zen_pipe"
 printf '%b\n' "#Cleaning installed version..." > "$zen_pipe"
 
-if [ $mode = "install" ]
+if [ "$mode" = "install" ]
 then
        cd geeqie || exit 1
 else
@@ -521,7 +520,7 @@ fi
 printf '%b\n' "30" > "$zen_pipe"
 printf '%b\n' "#Checkout required version..." > "$zen_pipe"
 
-if [ "$BACK" ]
+if [ -n "$BACK" ]
 then
        if ! git checkout master~"$BACK" >> "$install_log" 2>&1
        then
@@ -529,7 +528,7 @@ then
                zenity --title="$title" --width=370 --height=400 --error --text="Git error:\n\n$git_error" 2> /dev/null
                exit_install
        fi
-elif [ "$COMMIT" ]
+elif [ -n "$COMMIT" ]
 then
 
        if ! git checkout "$COMMIT" >> "$install_log" 2>&1
@@ -538,14 +537,13 @@ then
                zenity --title="$title" --width=370 --height=400 --error --text="Git error:\n\n$git_error" 2> /dev/null
                exit_install
        fi
-elif [ "$TAG" ]
+elif [ -n "$TAG" ]
 then
        if ! git checkout "$TAG" >> "$install_log" 2>&1
        then
                git_error=$(tail -n5 "$install_log" 2>&1)
                zenity --title="$title" --width=370 --height=400 --error --text="Git error:\n\n$git_error" 2> /dev/null
                exit_install
-               exit
        fi
 fi
 
index 3b8bb37..b47a1a2 100755 (executable)
@@ -11,7 +11,7 @@
 
 builddir="$2"
 
-cd "$1"
+cd "$1" || exit
 
 [ ! -e "ChangeLog.gqview" ] && exit 1
 [ ! -x "$(command -v git)" ] && exit 1
@@ -40,6 +40,7 @@ mv -f "$builddir/ChangeLog.$$.new.html" "$builddir/ChangeLog.html"
 
 # Meson: distribute in tarballs. The first variable is more reliable, but requires Meson 0.58.
 # Fallback to the older one if necessary
+# shellcheck disable=SC2154
 for distdir in "$MESON_PROJECT_DIST_ROOT" "$MESON_DIST_ROOT"; do
     if [ -n "$distdir" ]; then
         cp -f "$builddir/ChangeLog.html" "$distdir/ChangeLog.html"
index d89cbd9..964d69b 100755 (executable)
@@ -4,6 +4,7 @@
 ## @brief Camera import hook script
 ##
 
+# shellcheck disable=2154
 if [ "$ACTION" = "download" ]
 then
        printf '%s\n' "$ARGUMENT" >> /tmp/geeqie-camera-import-files
index 0cc9dcb..23711dd 100755 (executable)
@@ -34,7 +34,7 @@ process_raw ()
                res=1
        fi
 
-       return $res
+       return "$res"
 }
 
 process_plain ()
index 1a6f0da..dca9df6 100755 (executable)
@@ -10,12 +10,13 @@ GQ_METADATA_DIR="$HOME/.local/share/geeqie/metadata"
 
 rotate()
 {
-    ext=`echo "${1##*.}" |tr "[:upper:]" "[:lower:]"`
-    [ "x$ext" = "x" ] && return 1 #no extension
+    ext=$(echo "${1##*.}" |tr "[:upper:]" "[:lower:]")
+    [ "$ext" = "" ] && return 1 #no extension
 
     gq_metadata="$GQ_METADATA_DIR/$1.gq.xmp"
     if [ -f "$gq_metadata" ] ; then
-               gq_orientation=`exiv2 -PXkv "$gq_metadata"|grep Xmp.tiff.Orientation|sed -e "s|Xmp.tiff.Orientation *||"`
+               gq_orientation=$(exiv2 -PXkv "$gq_metadata"|grep Xmp.tiff.Orientation|sed -e "s|Xmp.tiff.Orientation *||")
+# shellcheck disable=2181
                [ $? != 0 ] && exit 1
     else
                gq_orientation=
@@ -25,16 +26,19 @@ rotate()
        jpg|jpeg) 
                if [ -n "$gq_orientation" ] ; then
                        exiv2 -M "set Exif.Image.Orientation $gq_orientation" "$1"
+# shellcheck disable=2181
                        [ $? != 0 ] && exit 1
                fi
                if exiftran -aip "$1" ; then
                    # exiftran ignores xmp, set it manually
                    exiv2 -M "set Xmp.tiff.Orientation 1" "$1"
+# shellcheck disable=2181
                        [ $? != 0 ] && exit 1
                    #http://dev.exiv2.org/issues/639
                        if [ -n "$gq_orientation" ] ; then
                                exiv2 -M "set Xmp.tiff.Orientation 1" \
                                                                -M "set Exif.Image.Orientation 1" "$gq_metadata"
+# shellcheck disable=2181
                                [ $? != 0 ] && exit 1
                        fi
                    return 0
@@ -46,16 +50,19 @@ rotate()
        tif|tiff|png)
                if [ -n "$gq_orientation" ] ; then
                        exiv2 -M "set Exif.Image.Orientation $gq_orientation" "$1"
+# shellcheck disable=2181
                        [ $? != 0 ] && exit 1
                fi
                if mogrify -auto-orient "$1" ; then
                    # mogrify ignores xmp, set it manually
                    exiv2 -M "set Xmp.tiff.Orientation 1" "$1"
+# shellcheck disable=2181
                        [ $? != 0 ] && exit 1
                    #http://dev.exiv2.org/issues/639
                        if [ -n "$gq_orientation" ] ; then
                                exiv2 -M "set Xmp.tiff.Orientation 1" \
                                                                -M "set Exif.Image.Orientation 1" "$gq_metadata"
+# shellcheck disable=2181
                                [ $? != 0 ] && exit 1
                        fi
                    return 0
@@ -71,18 +78,20 @@ rotate()
 
 rotate_image_file()
 {
-       ext=`echo "${3##*.}" |tr "[:upper:]" "[:lower:]"`
-       [ "x$ext" = "x" ] && return 1 #no extension
+       ext=$(echo "${3##*.}" |tr "[:upper:]" "[:lower:]")
+       [ "$ext" = "" ] && return 1 #no extension
 
        case "$ext" in
        jpg|jpeg)
                exiftran -i "$1" "$3"
+# shellcheck disable=2181
                [ $? != 0 ]  && return 6
                return 0;
                ;;
 
        tif|tiff|png)
                mogrify "$2" "$3"
+# shellcheck disable=2181
                [ $? != 0 ]  && return 7
                return 0;
                ;;
@@ -94,14 +103,14 @@ rotate_image_file()
 }
 
 get_sidecars=
-if [ "x$1" = "x-g" ] ; then
+if [ "$1" = "-g" ] ; then
     get_sidecars=yes
     shift
 fi
 
 rotate_image_file=
 rotation=
-if [ "x$1" = "x-r" ] ; then
+if [ "$1" = "-r" ] ; then
        rotate_image_file=yes
        shift
        rotation="$1"
@@ -109,40 +118,40 @@ if [ "x$1" = "x-r" ] ; then
 fi
 
 preserve_mtime=
-if [ "x$1" = "x-t" ] ; then
+if [ "$1" = "-t" ] ; then
        preserve_mtime=yes
        shift
 fi
 
 if [ -n "$rotation" ] ; then
-       if [ "x$rotation" = "x0" ] ; then
+       if [ "$rotation" = "0" ] ; then
                exit 0
        fi
-       if [ "x$rotation" = "x2" ] ; then
+       if [ "$rotation" = "2" ] ; then
                mogrify_param="-flop"
                exiftran_param="-F"
        fi
-       if [ "x$rotation" = "x3" ] ; then
+       if [ "$rotation" = "3" ] ; then
                mogrify_param="-rotate 180"
                exiftran_param="-1"
        fi
-       if [ "x$rotation" = "x4" ] ; then
+       if [ "$rotation" = "4" ] ; then
                mogrify_param="-flip"
                exiftran_param="-f"
        fi
-       if [ "x$rotation" = "x5" ] ; then
+       if [ "$rotation" = "5" ] ; then
                mogrify_param="-transpose"
                exiftran_param="-t"
        fi
-       if [ "x$rotation" = "x6" ] ; then
+       if [ "$rotation" = "6" ] ; then
                mogrify_param="-rotate 90"
                exiftran_param="-9"
        fi
-       if [ "x$rotation" = "x7" ] ; then
+       if [ "$rotation" = "7" ] ; then
                mogrify_param="-transverse"
                exiftran_param="-T"
        fi
-       if [ "x$rotation" = "x8" ] ; then
+       if [ "$rotation" = "8" ] ; then
                mogrify_param="-rotate -90"
                exiftran_param="-2"
        fi
@@ -161,12 +170,12 @@ for file in "$@" ; do
         done
                # Bourne shell runs DO loops in a sub-shell
                ret=$?
-               [ $ret != 0 ] && exit $ret
+               [ "$ret" != 0 ] && exit "$ret"
     else
                [ ! -w "$file" ] && exit 5
                if [ -n "$rotate_image_file" ] ; then
                        if [ -n "$preserve_mtime" ] ; then
-                               mtime=`mktemp "${TMPDIR:-/tmp}/geeqie-rotate.XXXXXXXXXX"` || exit 3
+                               mtime=$(mktemp "${TMPDIR:-/tmp}/geeqie-rotate.XXXXXXXXXX") || exit 3
                                touch --reference="$file" "$mtime"
                        fi
 
@@ -177,11 +186,11 @@ for file in "$@" ; do
                                touch --reference="$mtime" "$file"
                                rm "$mtime"
                        fi
-                       [ $ret != 0 ]  && exit $ret
+                       [ "$ret" != 0 ]  && exit "$ret"
                else
                        rotate "$file"
                        ret=$?
-                       [ $ret != 0 ] && exit $ret
+                       [ "$ret" != 0 ] && exit "$ret"
                fi
     fi
 done
index 51504e1..9da7f24 100755 (executable)
 for file in "$@" ; do
     # we got only one file for each group, typically the main one
     # get the sidecars:
-    geeqie -r --get-sidecars:"$file" |while read sidecar ; do
+    geeqie -r --get-sidecars:"$file" |while read -r sidecar ; do
        # the main file is included in the sidecar file list, no special handling is required
        # get destination path for each sidecar file:
-        geeqie -r --get-destination:"$sidecar" | if read destination ; then
+        geeqie -r --get-destination:"$sidecar" | if read -r destination ; then
             ln -s "$sidecar" "$destination"
         fi
     done
index 380c6ff..1d47341 100755 (executable)
@@ -47,5 +47,5 @@ then
        zenity --text-info --title="Geeqie tethered photography" --window-icon=/usr/local/share/pixmaps/geeqie.png --ok-label="Stop" --cancel-label="Abort" <"$tmpfile" 2>/dev/null
 fi
 
-kill $gphoto2_pid
+kill "$gphoto2_pid"
 rm "$tmpfile"
index 6b83e1c..394b27b 100755 (executable)
@@ -4,6 +4,7 @@
 ## @brief Tethered photography hook script
 ##
 
+# shellcheck disable=SC2154
 if [ "$ACTION" = "download" ]
 then
        notify-send --expire-time=4000 "$ARGUMENT"  --icon=geeqie
index 540bd41..278499d 100755 (executable)
@@ -9,8 +9,8 @@
 # TODO(xsdg): Re-write this in a simpler way and test that it works: (cd ..; find ... | sort > $TMP)
 
 TMP=POTFILES.$$
-((find ../src/ -type f \( -name '*.c' -o -name '*.cc' \) ; find ../ -type f -name '*.desktop.in' ; find ../ -type f -name '*.appdata.xml.in') | while read f; do
-       (echo $f | sed 's#^../##')
-done) | sort > $TMP
-diff -u POTFILES $TMP
-rm -f $TMP
+( (find ../src/ -type f \( -name '*.c' -o -name '*.cc' \) ; find ../ -type f -name '*.desktop.in' ; find ../ -type f -name '*.appdata.xml.in') | while read -r f; do
+       (echo "$f" | sed 's#^../##')
+done) | sort > "$TMP"
+diff -u POTFILES "$TMP"
+rm -f "$TMP"
index a857197..bccf840 100644 (file)
@@ -41,13 +41,13 @@ sudo apt install libgirepository1.0-dev
 
 rm -rf /tmp/linuxdeploy-*
 
-cd "$HOME"
+cd "$HOME" || exit
 if [ ! -d bin ]
 then
        mkdir bin
 fi
 
-cd "$HOME"/bin
+cd "$HOME"/bin || exit
 
 if [ ! -f appimagetool-aarch64.AppImage ]
 then
@@ -67,7 +67,7 @@ then
        mv linuxdeploy linuxdeploy-download
 fi
 
-cd linuxdeploy-download
+cd linuxdeploy-download || exit
 
 # Use arm architecture
 sed -i 's/"x86_64"/"aarch64"/g' ./ci/build.sh
@@ -104,7 +104,7 @@ then
        git clone --recursive https://github.com/linuxdeploy/linuxdeploy-plugin-appimage.git
 fi
 
-cd linuxdeploy-plugin-appimage
+cd linuxdeploy-plugin-appimage || exit
 
 # Always use /tmp
 sed -i 's/TEMP_BASE=\/dev\/shm/TEMP_BASE=\/tmp/' ./ci/build-appimage.sh
index c533331..0a869dd 100755 (executable)
@@ -14,7 +14,7 @@ merge_file()
 flag=0
 while read -r line_merge
 do
-       if [ $flag -eq 0 ]
+       if [ "$flag" -eq 0 ]
        then
                if [ "$line_merge" != "${line_merge%<keyword_tree>*}" ]
                then
@@ -40,11 +40,11 @@ then
 fi
 
 
-if [ $np -ge 3 ]
+if [ "$np" -ge 3 ]
 then
        zenity --error --text "Too many parameters"
        exit
-elif [ $np -eq 0 ]
+elif [ "$np" -eq 0 ]
 then
        config_main=$(zenity --file-selection --file-filter="geeqierc.xml" --file-filter="*.xml" --file-filter="*" --title="Select main configuration file")
        if [ $? -eq 1 ]
@@ -56,7 +56,7 @@ then
        then
                exit
        fi
-elif [ $np -eq 1 ]
+elif [ "$np" -eq 1 ]
 then
        config_merge=$(zenity --file-selection --file-filter="geeqierc.xml" --file-filter="*.xml" --file-filter="*" --title="Select configuration file to merge from")
        if [ $? -eq 1 ]
index ef48f41..0faee50 100755 (executable)
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/bin/sh
 
 ## @file
 ## @brief Create a new release
 ## It is expected that the first line of NEWS is in the form "Geeqie \<a.b[.d]\>
 ##
 
-error_handler ()
-{
-       echo "Error on line: $1"
-       exit 1
-}
-trap 'error_handler ${LINENO}' ERR
-
-
 version=
 start=
 patch=
index f6e1c5a..772aada 100755 (executable)
@@ -32,7 +32,7 @@ then
                minor_version=$2
 #              patch_version=$3  # not used on master branch
 
-               printf '%s%s%s%s%s%s%s' "$major_version" "." "$minor_version" "+git" $(git log --max-count=1 --date=format:"%Y%m%d" --format="%ad") "-" $(git rev-parse --quiet --verify --short HEAD)
+               printf '%s%s%s%s%s%s%s' "$major_version" "." "$minor_version" "+git" "$(git log --max-count=1 --date=format:"%Y%m%d" --format="%ad")" "-" "$(git rev-parse --quiet --verify --short HEAD)"
        else
                version=$(head -1 NEWS)
                # shellcheck disable=SC2086