From 5ee8d668fe651ae0263c5027adff44265874eddd Mon Sep 17 00:00:00 2001 From: Andreas Romeyke <andreas.romeyke@slub-dresden.de> Date: Tue, 21 Feb 2023 17:29:49 +0100 Subject: [PATCH] - fixed shellcheck warnings --- validate_workflow.sh | 312 +++++++++++++++++++++---------------------- 1 file changed, 156 insertions(+), 156 deletions(-) diff --git a/validate_workflow.sh b/validate_workflow.sh index 125c5bd..98eb9b4 100755 --- a/validate_workflow.sh +++ b/validate_workflow.sh @@ -105,15 +105,15 @@ set -e check_argument_count() { local count=$1 local expected=$2 - if [ "$count" -ne "$expected" ]; then - error "called function ${FUNCNAME[1]} expected $expected params, but got $count by caller function ${FUNCNAME[2]} (line ${BASH_LINENO[2]})" + if [ "${count}" -ne "${expected}" ]; then + error "called function ${FUNCNAME[1]} expected ${expected} params, but got ${count} by caller function ${FUNCNAME[2]} (line ${BASH_LINENO[2]})" exit 1 fi } check_argument_notempty() { local param=$1 - if [ -z "$param" ]; then + if [ -z "${param}" ]; then error "called function ${FUNCNAME[1]} expected non-empty params, but some are empty set in line ${BASH_LINENO[0]}, eventually taken from caller function ${FUNCNAME[1]} (line ${BASH_LINENO[1]})" exit 1 fi @@ -125,7 +125,7 @@ comment_help() { } calc_statistics() { - flock -x "$LOCKFILE" cat "$STATFILE" | awk -F "," '{cnt_invalid+=$3;total++} END {print total, cnt_invalid}' + flock -x "${LOCKFILE}" cat "${STATFILE}" | awk -F "," '{cnt_invalid+=$3;total++} END {print total, cnt_invalid}' } print_statistics() { @@ -135,14 +135,14 @@ print_statistics() { local cnt_invalid local ratio stat=$(calc_statistics) - cnt_total=$(echo "$stat" | awk 'END {print $1}') - cnt_valid=$(echo "$stat" | awk 'END {print $2}') + cnt_total=$(echo "${stat}" | awk 'END {print $1}') + cnt_valid=$(echo "${stat}" | awk 'END {print $2}') cnt_invalid=$((cnt_total - cnt_valid)) ratio=$(( 100*cnt_valid / cnt_total )) echo "Validation Statistics" - echo "valid files: $cnt_valid" - echo "invalid files: $cnt_invalid" - echo "ratio: $ratio% valid" + echo "valid files: ${cnt_valid}" + echo "invalid files: ${cnt_invalid}" + echo "ratio: ${ratio}% valid" } @@ -153,25 +153,25 @@ update_statistics() { local ftype=$3 local workflow=$4 local stage=$5 - check_argument_notempty "$is_valid" - check_argument_notempty "$duration" - check_argument_notempty "$ftype" - check_argument_notempty "$workflow" - check_argument_notempty "$stage" + check_argument_notempty "${is_valid}" + check_argument_notempty "${duration}" + check_argument_notempty "${ftype}" + check_argument_notempty "${workflow}" + check_argument_notempty "${stage}" local date date=$(date +"%F%T") - debug "date=$date" - flock -x $LOCKFILE echo "$date,$is_valid,$duration,$ftype,$workflow,$stage" >> "$STATFILE" + debug "date=${date}" + flock -x "${LOCKFILE}" echo "${date},${is_valid},${duration},${ftype},${workflow},${stage}" >> "${STATFILE}" } trim_statistics() { debug "trim_statistics" - tail -n $MIN_STAT_LINES "$STATFILE" > "$STATFILE.new" || (error "could not trim $STATFILE to $STATFILE.new" ; exit 1 ) - mv "$STATFILE.new" "$STATFILE" || ( error "count not trim $STATFILE,because could not mv $STATFILE.new to $STATFILE"; exit 1 ) + tail -n "${MIN_STAT_LINES}" "${STATFILE}" > "${STATFILE}.new" || (error "could not trim ${STATFILE} to ${STATFILE}.new" ; exit 1 ) + mv "${STATFILE}.new" "${STATFILE}" || ( error "count not trim ${STATFILE},because could not mv ${STATFILE}.new to ${STATFILE}"; exit 1 ) } debug() { - if [ $WITH_DEBUG -eq 1 ]; then + if [ "${WITH_DEBUG}" -eq 1 ]; then >&2 echo "DEBUG: $1" fi } @@ -188,10 +188,10 @@ error() { get_mimetype() { check_argument_count $# 1 local filename=$1 - check_argument_notempty "$filename" + check_argument_notempty "${filename}" local res - res=$(file --mime-type "$filename" | sed -e "s/^.*: //") - echo "$res" + res=$(file --mime-type "${filename}" | sed -e "s/^.*: //") + echo "${res}" } @@ -199,7 +199,7 @@ get_cli_args() { local lines local cachedir while [[ $# -gt 0 ]]; do - case ${1} in + case $1 in -h | --help) comment_help exit 0 @@ -213,32 +213,32 @@ get_cli_args() { exit 0 ;; -w | --watch-folder) - WATCH_FOLDER="${2}" + WATCH_FOLDER="$2" shift shift ;; -r | --result-folder) - RESULT_FOLDER="${2}" + RESULT_FOLDER="$2" shift shift ;; -v | --valid-folder) - VALID_FOLDER="${2}" + VALID_FOLDER="$2" shift shift ;; -i | --invalid-folder) - INVALID_FOLDER="${2}" + INVALID_FOLDER="$2" shift shift ;; -m | --mode) - MODE="${2}" + MODE="$2" shift shift ;; -t | --stage) - STAGE="${2}" + STAGE="$2" shift shift ;; @@ -247,7 +247,7 @@ get_cli_args() { shift ;; -f | --files-mode) - FILES_MODE="${2}" + FILES_MODE="$2" shift shift ;; @@ -256,76 +256,76 @@ get_cli_args() { shift ;; *) - error "'${1}' is invalid param. Please, give '$(basename "${0}") --help' a chance!" + error "'$1' is invalid param. Please, give '$(basename "$0") --help' a chance!" exit 1 ;; esac done - if [ "$FILES_MODE" != "sort" ] && [ "$FILES_MODE" != "delete" ] && [ "$FILES_MODE" != "nothing" ]; then + if [ "${FILES_MODE}" != "sort" ] && [ "${FILES_MODE}" != "delete" ] && [ "${FILES_MODE}" != "nothing" ]; then error "param --files-mode must be 'sort', 'delete' or 'nothing'!" exit 1 fi - if [ "$MODE" != "auto" ] \ - && [ "$MODE" != "mediathek" ] \ - && [ "$MODE" != "fotothek" ] \ - && [ "$MODE" != "save" ] \ - && [ "$MODE" != "ddz" ] \ - && [ "$MODE" != "digas" ]; then + if [ "${MODE}" != "auto" ] \ + && [ "${MODE}" != "mediathek" ] \ + && [ "${MODE}" != "fotothek" ] \ + && [ "${MODE}" != "save" ] \ + && [ "${MODE}" != "ddz" ] \ + && [ "${MODE}" != "digas" ]; then error "param --mode must be 'auto', 'mediathek', 'fotothek', 'save', 'ddz' or 'digas'!" exit 1 fi - if [ "$WITH_PIPE" -eq 1 ]; then + if [ "${WITH_PIPE}" -eq 1 ]; then if - [ "$WITH_DAEMON" -eq 1 ] \ - || [ -n "$WATCH_FOLDER" ] \ - || [ -n "$RESULT_FOLDER" ] \ - || [ -n "$VALID_FOLDER" ] \ - || [ -n "$INVALID_FOLDER" ] \ - || [ "$FILES_MODE" = "sort" ] \ + [ "${WITH_DAEMON}" -eq 1 ] \ + || [ -n "${WATCH_FOLDER}" ] \ + || [ -n "${RESULT_FOLDER}" ] \ + || [ -n "${VALID_FOLDER}" ] \ + || [ -n "${INVALID_FOLDER}" ] \ + || [ "${FILES_MODE}" = "sort" ] \ ; then error "param --pipe not combineable with params --daemon, --result-folder, --watch-folder, --valid-folder, --invalid-folder, --files-mode" exit 1 fi else - if [ "$WITH_DAEMON" -eq 1 ] && [ "$FILES_MODE" = "sort" ]; then + if [ "${WITH_DAEMON}" -eq 1 ] && [ "${FILES_MODE}" = "sort" ]; then error "param --daemon does only work with param --mode='delete' or --mode='nothing'!" exit 1 fi - if [ "$STAGE" != "current" ] && [ "$STAGE" != "upcoming" ] && [ "$STAGE" != "any" ]; then + if [ "${STAGE}" != "current" ] && [ "${STAGE}" != "upcoming" ] && [ "${STAGE}" != "any" ]; then error "--param stage must be 'any', 'current' or 'upcoming'!" exit 1 fi - if [ ! -d "$WATCH_FOLDER" ]; then - error "watch folder '$WATCH_FOLDER' does not exist!" + if [ ! -d "${WATCH_FOLDER}" ]; then + error "watch folder '${WATCH_FOLDER}' does not exist!" exit 1 fi - if [ ! -d "$RESULT_FOLDER" ]; then - error "result folder '$RESULT_FOLDER' does not exist!" + if [ ! -d "${RESULT_FOLDER}" ]; then + error "result folder '${RESULT_FOLDER}' does not exist!" exit 1 fi - if [ "$FILES_MODE" = "sort" ]; then - if [ ! -d "$VALID_FOLDER" ]; then - error "valid folder '$VALID_FOLDER' does not exist!" + if [ "${FILES_MODE}" = "sort" ]; then + if [ ! -d "${VALID_FOLDER}" ]; then + error "valid folder '${VALID_FOLDER}' does not exist!" exit 1 fi - if [ ! -d "$INVALID_FOLDER" ]; then - error "invalid folder '$INVALID_FOLDER' does not exist!" + if [ ! -d "${INVALID_FOLDER}" ]; then + error "invalid folder '${INVALID_FOLDER}' does not exist!" exit 1 fi fi fi - cachedir=$(dirname "$STATFILE") - if [ ! -d "$cachedir" ]; then - mkdir -p "$cachedir" || error "Could not create dir $cachedir, $?" + cachedir=$(dirname "${STATFILE}") + if [ ! -d "${cachedir}" ]; then + mkdir -p "${cachedir}" || error "Could not create dir ${cachedir}, $?" fi - if [ -e "$STATFILE" ]; then - lines=$( flock -x "$LOCKFILE" wc -l "$STATFILE" | cut -d " " -f 1) - debug "found $lines lines in $STATFILE)" - if [ "$lines" -gt $MAX_STAT_LINES ]; then + if [ -e "${STATFILE}" ]; then + lines=$( flock -x "${LOCKFILE}" wc -l "${STATFILE}" | cut -d " " -f 1) + debug "found ${lines} lines in ${STATFILE})" + if [ "${lines}" -gt "${MAX_STAT_LINES}" ]; then ( flock -n 9 || exit 1 trim_statistics - ) 9>"$LOCKFILE" + ) 9>"${LOCKFILE}" fi fi } @@ -335,21 +335,21 @@ prepare_cmd() { local mode=$1 local ftype=$2 local stage=$3 - check_argument_notempty "$mode" - check_argument_notempty "$ftype" - check_argument_notempty "$stage" + check_argument_notempty "${mode}" + check_argument_notempty "${ftype}" + check_argument_notempty "${stage}" local key local cmd - key=$(printf "%11s%4s%9s" "$mode" "$ftype" "$stage"|sed -e "y/ /_/") - check_argument_notempty "$key" - debug "prepare_cmd, key=$key" - if [[ ${validators[$key]:+1} ]]; then - cmd=${validators[$key]}; - check_argument_notempty "$cmd" - debug "prepare_cmd, cmd=$cmd" - echo "$cmd" + key=$(printf "%11s%4s%9s" "${mode}" "${ftype}" "${stage}"|sed -e "y/ /_/") + check_argument_notempty "${key}" + debug "prepare_cmd, key=${key}" + if [[ -n ${validators[${key}]:+1} ]]; then + cmd=${validators[${key}]}; + check_argument_notempty "${cmd}" + debug "prepare_cmd, cmd=${cmd}" + echo "${cmd}" else - debug "no valid command found using key $key" + debug "no valid command found using key ${key}" echo "echo 'no validation tool detected!'" fi } @@ -357,9 +357,9 @@ prepare_cmd() { prepare_ftype() { check_argument_count $# 1 local mimetype=$1 - check_argument_notempty "$mimetype" + check_argument_notempty "${mimetype}" local ftype - debug "prepare_ftype, using mimetype: $mimetype" + debug "prepare_ftype, using mimetype: ${mimetype}" case ${mimetype} in "image/tiff") ftype="tif" @@ -371,20 +371,20 @@ prepare_ftype() { ftype="icc" ;; *) - warn "unknown file format with mime-type '$mimetype'" + warn "unknown file format with mime-type '${mimetype}'" ftype="???" ;; esac - check_argument_notempty "$ftype" - debug "prepare_ftype, detect ftype: $ftype" - echo "$ftype" + check_argument_notempty "${ftype}" + debug "prepare_ftype, detect ftype: ${ftype}" + echo "${ftype}" } estimate_mode() { check_argument_count $# 1 local mimetype=$1 - check_argument_notempty "$mimetype" - debug "estimate_mode, using mimetype: $mimetype" + check_argument_notempty "${mimetype}" + debug "estimate_mode, using mimetype: ${mimetype}" case ${mimetype} in "image/tiff") MODE="ddz" @@ -396,13 +396,13 @@ estimate_mode() { MODE="ddz" ;; *) - warn "workflow not detectable for mimetype $mimetype" + warn "workflow not detectable for mimetype ${mimetype}" MODE="???" ;; esac - check_argument_notempty "$MODE" - debug "estimate_mode, detected mode: $MODE" - echo "$MODE" + check_argument_notempty "${MODE}" + debug "estimate_mode, detected mode: ${MODE}" + echo "${MODE}" } exec_cmd() { @@ -414,41 +414,41 @@ exec_cmd() { local log=$5 local start_t local stop_t - check_argument_notempty "$cmd" - check_argument_notempty "$ftype" - check_argument_notempty "$workflow" - check_argument_notempty "$stage" - check_argument_notempty "$log" + check_argument_notempty "${cmd}" + check_argument_notempty "${ftype}" + check_argument_notempty "${workflow}" + check_argument_notempty "${stage}" + check_argument_notempty "${log}" start_t=$(date +"%s") - debug "scan_file, calling cmd='$cmd'" - $cmd >>"$log" 2>&1 + debug "scan_file, calling cmd='${cmd}'" + ${cmd} >>"${log}" 2>&1 local is_valid=$? - check_argument_notempty $is_valid + check_argument_notempty "${is_valid}" stop_t=$(date +"%s") local duration=$((stop_t - start_t)) - debug "exec_cmd, duration=$duration is_valid=$is_valid log=$log" - update_statistics "$is_valid" "$duration" "$ftype" "$workflow" "$stage" - echo "$is_valid" + debug "exec_cmd, duration=${duration} is_valid=${is_valid} log=${log}" + update_statistics "${is_valid}" "${duration}" "${ftype}" "${workflow}" "${stage}" + echo "${is_valid}" } handle_input_if_requested() { check_argument_count $# 2 local filename=$1 local is_valid=$2 - check_argument_notempty "$filename" - check_argument_notempty "$is_valid" - debug "handle_input_if_requested, filename=$filename is_valid=$is_valid" - if [ "$FILES_MODE" = "sort" ]; then - if [ "$is_valid" -eq 0 ]; then - debug "handle_input_if_requested, mv $filename to $VALID_FOLDER, because valid" - mv "$filename" "$VALID_FOLDER" + check_argument_notempty "${filename}" + check_argument_notempty "${is_valid}" + debug "handle_input_if_requested, filename=${filename} is_valid=${is_valid}" + if [ "${FILES_MODE}" = "sort" ]; then + if [ "${is_valid}" -eq 0 ]; then + debug "handle_input_if_requested, mv ${filename} to ${VALID_FOLDER}, because valid" + mv "${filename}" "${VALID_FOLDER}" else - debug "handle_input_if_requested, mv $filename to $INVALID_FOLDER, because invalid" - mv "$filename" "$INVALID_FOLDER" + debug "handle_input_if_requested, mv ${filename} to ${INVALID_FOLDER}, because invalid" + mv "${filename}" "${INVALID_FOLDER}" fi - elif [ "$FILES_MODE" = "delete" ]; then - debug "handle_input_if_requested, rm $filename from watchfolder $WATCH_FOLDER" - rm -f "$filename" + elif [ "${FILES_MODE}" = "delete" ]; then + debug "handle_input_if_requested, rm ${filename} from watchfolder ${WATCH_FOLDER}" + rm -f "${filename}" fi } @@ -456,21 +456,21 @@ get_logfile() { check_argument_count $# 1 local filename=$1 local logname - check_argument_notempty "$filename" + check_argument_notempty "${filename}" if [ -n "${WATCH_FOLDER}" ] && [ "${WITH_PIPE}" -eq 0 ]; then - logname=$(echo "$filename"| sed -e "s#^${WATCH_FOLDER}#${RESULT_FOLDER}#" -e "s#\$#.log#") + logname=$(echo "${filename}"| sed -e "s#^${WATCH_FOLDER}#${RESULT_FOLDER}#" -e "s#\$#.log#") else # pipe uses a temp filename - logname="$filename.log" + logname="${filename}.log" fi - check_argument_notempty "$logname" - logdir=$(dirname "$logname") - check_argument_notempty "$logdir" - if [ ! -d "$logdir" ]; then - debug "get_logfile, mkdir $logdir" - mkdir -p "$logdir" + check_argument_notempty "${logname}" + logdir=$(dirname "${logname}") + check_argument_notempty "${logdir}" + if [ ! -d "${logdir}" ]; then + debug "get_logfile, mkdir ${logdir}" + mkdir -p "${logdir}" fi - debug "get_logfile, logname=$logname (filename=$filename)" - echo "$logname" + debug "get_logfile, logname=${logname} (filename=${filename})" + echo "${logname}" } @@ -482,37 +482,37 @@ scan_file() { local logname local cmd local is_valid - check_argument_notempty "$filename" - debug "scan_file, using filename: $filename" - mimetype=$(get_mimetype "$filename") - ftype=$(prepare_ftype "$mimetype") - logname=$(get_logfile "$filename") - if [ "$MODE" = "auto" ]; then + check_argument_notempty "${filename}" + debug "scan_file, using filename: ${filename}" + mimetype=$(get_mimetype "${filename}") + ftype=$(prepare_ftype "${mimetype}") + logname=$(get_logfile "${filename}") + if [ "${MODE}" = "auto" ]; then # try best guess - MODE=$(estimate_mode "$mimetype") + MODE=$(estimate_mode "${mimetype}") fi - check_argument_notempty "$mimetype" - check_argument_notempty "$ftype" - check_argument_notempty "$logname" + check_argument_notempty "${mimetype}" + check_argument_notempty "${ftype}" + check_argument_notempty "${logname}" trap "" SIGINT debug "scan_file, === entering protected area ===" - if [ "$STAGE" = "any" ]; then + if [ "${STAGE}" = "any" ]; then is_valid=1 for stage in upcoming current; do - cmd=$(prepare_cmd "$MODE" "$ftype" "$stage" | sed -e "s#FILE#$filename#") - is_valid=$(exec_cmd "$cmd" "$ftype" "$MODE" "$stage" "$logname") - if [ "$is_valid" -eq 0 ]; then + cmd=$(prepare_cmd "${MODE}" "${ftype}" "${stage}" | sed -e "s#FILE#${filename}#") + is_valid=$(exec_cmd "${cmd}" "${ftype}" "${MODE}" "${stage}" "${logname}") + if [ "${is_valid}" -eq 0 ]; then debug "scan_file, early break" break else - debug "scan_file, no early break, because is_valid='$is_valid'" + debug "scan_file, no early break, because is_valid='${is_valid}'" fi done - handle_input_if_requested "$filename" "$is_valid" + handle_input_if_requested "${filename}" "${is_valid}" else - cmd=$(prepare_cmd "$MODE" "$ftype" "$STAGE" | sed -e "s#FILE#$filename#") - is_valid=$(exec_cmd "$cmd" "$ftype" "$MODE" "$STAGE" "$logname") - handle_input_if_requested "$filename" "$is_valid" + cmd=$(prepare_cmd "${MODE}" "${ftype}" "${STAGE}" | sed -e "s#FILE#${filename}#") + is_valid=$(exec_cmd "${cmd}" "${ftype}" "${MODE}" "${STAGE}" "${logname}") + handle_input_if_requested "${filename}" "${is_valid}" fi debug "scan_file, === leaving protected area ===" trap - SIGINT @@ -523,7 +523,7 @@ scan_dir() { check_argument_count $# 1 check_argument_notempty "$1" find "$1" -type f -cmin +1 -mmin +1 -print0 | while IFS= read -r -d '' filename; do - scan_file "$filename" + scan_file "${filename}" done } @@ -531,35 +531,35 @@ scan_dir() { get_cli_args "$@" #trap signalhandler SIGINT SIGABRT #sigint -if [ "$WITH_PIPE" -eq 1 ]; then +if [ "${WITH_PIPE}" -eq 1 ]; then #cli mode, use stdin debug "checking stream" filename=$(mktemp --tmpdir validate_wrg.XXXX) - cat - > "$filename" - scan_file "$filename" - cat "$filename.log" - rm -f "$filename.log" || error "could not remove temporary file'$filename.log'" - rm -f "$filename" || error "could not remove temporary file '$filename'" + cat - > "${filename}" + scan_file "${filename}" + cat "${filename}.log" + rm -f "${filename}.log" || error "could not remove temporary file '${filename}.log'" + rm -f "${filename}" || error "could not remove temporary file '${filename}'" else - if [ "$WITH_DAEMON" -eq 1 ]; then + if [ "${WITH_DAEMON}" -eq 1 ]; then # TODO: protect DAEMON from STRG-C for clean shutdown # echo daemon mode, use inotify to watch changes debug "starting daemon" - while [[ 1 ]]; do - scan_dir "$WATCH_FOLDER" # to clean up existing files + while true; do + scan_dir "${WATCH_FOLDER}" # to clean up existing files sleep 10 done # /usr/bin/inotifywait --monitor --recursive --event create \ -# --event attrib --event moved_to --format "%w%f" "$WATCH_FOLDER" \ +# --event attrib --event moved_to --format "%w%f" "${WATCH_FOLDER}" \ # | while read -r filename; do # debug "called inotifywait using /usr/bin/inotifywait --monitor -# --recursive --event create --event attrib --event moved_to --format '%f' $WATCH_FOLDER" -# scan_file "$filename" +# --recursive --event create --event attrib --event moved_to --format '%f' ${WATCH_FOLDER}" +# scan_file "${filename}" # done debug "stopping daemon" else # cli mode, scan watch folder once - debug "checking dir $WATCH_FOLDER" - scan_dir "$WATCH_FOLDER" + debug "checking dir ${WATCH_FOLDER}" + scan_dir "${WATCH_FOLDER}" fi fi -- GitLab