From bf4860512124e764b7d76de4cb264b2206344924 Mon Sep 17 00:00:00 2001 From: Nathaniel Landau Date: Fri, 30 Jul 2021 12:53:55 -0400 Subject: [PATCH] fix pass unset variable syntax --- scriptTemplate.sh | 56 +++++++++++++++++------------------ utilities/alerts.bash | 28 +++++++++--------- utilities/baseHelpers.bash | 18 +++++------ utilities/files.bash | 20 ++++++------- utilities/textProcessing.bash | 2 +- 5 files changed, 62 insertions(+), 62 deletions(-) diff --git a/scriptTemplate.sh b/scriptTemplate.sh index 6717122..d29fc02 100755 --- a/scriptTemplate.sh +++ b/scriptTemplate.sh @@ -64,7 +64,7 @@ _alert_() { local function_name color local alertType="${1}" local message="${2}" - local line="${3-}" # Optional line number + local line="${3:-}" # Optional line number if [[ -n "${line}" && "${alertType}" =~ ^(fatal|error) && "${FUNCNAME[2]}" != "_trapCleanup_" ]]; then message="${message} (line: ${line}) $(_functionStack_)" @@ -109,7 +109,7 @@ _alert_() { _writeToLog_() { [[ "${alertType}" == "input" ]] && return 0 [[ "${LOGLEVEL}" =~ (off|OFF|Off) ]] && return 0 - [ -z "${LOGFILE-}" ] && fatal "\$LOGFILE must be set" + [ -z "${LOGFILE:-}" ] && fatal "\$LOGFILE must be set" [ ! -d "$(dirname "${LOGFILE}")" ] && mkdir -p "$(dirname "${LOGFILE}")" [[ ! -f "${LOGFILE}" ]] && touch "${LOGFILE}" @@ -162,25 +162,25 @@ _alert_() { } # /_alert_ -error() { _alert_ error "${1}" "${2-}"; } -warning() { _alert_ warning "${1}" "${2-}"; } -notice() { _alert_ notice "${1}" "${2-}"; } -info() { _alert_ info "${1}" "${2-}"; } -success() { _alert_ success "${1}" "${2-}"; } -dryrun() { _alert_ dryrun "${1}" "${2-}"; } -input() { _alert_ input "${1}" "${2-}"; } -header() { _alert_ header "== ${1} ==" "${2-}"; } -die() { _alert_ fatal "${1}" "${2-}"; _safeExit_ "1" ; } -fatal() { _alert_ fatal "${1}" "${2-}"; _safeExit_ "1" ; } -debug() { _alert_ debug "${1}" "${2-}"; } -verbose() { _alert_ debug "${1}" "${2-}"; } +error() { _alert_ error "${1}" "${2:-}"; } +warning() { _alert_ warning "${1}" "${2:-}"; } +notice() { _alert_ notice "${1}" "${2:-}"; } +info() { _alert_ info "${1}" "${2:-}"; } +success() { _alert_ success "${1}" "${2:-}"; } +dryrun() { _alert_ dryrun "${1}" "${2:-}"; } +input() { _alert_ input "${1}" "${2:-}"; } +header() { _alert_ header "== ${1} ==" "${2:-}"; } +die() { _alert_ fatal "${1}" "${2:-}"; _safeExit_ "1" ; } +fatal() { _alert_ fatal "${1}" "${2:-}"; _safeExit_ "1" ; } +debug() { _alert_ debug "${1}" "${2:-}"; } +verbose() { _alert_ debug "${1}" "${2:-}"; } _safeExit_() { # DESC: Cleanup and exit from a script # ARGS: $1 (optional) - Exit code (defaults to 0) # OUTS: None - if [[ -d "${script_lock-}" ]]; then + if [[ -d "${script_lock:-}" ]]; then if command rm -rf "${script_lock}"; then debug "Removing script lock" else @@ -188,8 +188,8 @@ _safeExit_() { fi fi - if [[ -n "${tmpDir-}" && -d "${tmpDir-}" ]]; then - if [[ ${1-} == 1 && -n "$(ls "${tmpDir}")" ]]; then + if [[ -n "${tmpDir:-}" && -d "${tmpDir:-}" ]]; then + if [[ ${1:-} == 1 && -n "$(ls "${tmpDir}")" ]]; then command rm -r "${tmpDir}" else command rm -r "${tmpDir}" @@ -211,19 +211,19 @@ _trapCleanup_() { # $6 - $BASH_SOURCE # OUTS: None - local line=${1-} # LINENO - local linecallfunc=${2-} - local command="${3-}" - local funcstack="${4-}" - local script="${5-}" - local sourced="${6-}" + local line=${1:-} # LINENO + local linecallfunc=${2:-} + local command="${3:-}" + local funcstack="${4:-}" + local script="${5:-}" + local sourced="${6:-}" funcstack="'$(echo "$funcstack" | sed -E 's/ / < /g')'" if [[ "${script##*/}" == "${sourced##*/}" ]]; then - fatal "${7-} command: '$command' (line: $line) [func: $(_functionStack_)]" + fatal "${7:-} command: '$command' (line: $line) [func: $(_functionStack_)]" else - fatal "${7-} command: '$command' (func: ${funcstack} called at line $linecallfunc of '${script##*/}') (line: $line of '${sourced##*/}') " + fatal "${7:-} command: '$command' (func: ${funcstack} called at line $linecallfunc of '${script##*/}') (line: $line of '${sourced##*/}') " fi _safeExit_ "1" @@ -237,7 +237,7 @@ _makeTempDir_() { [ -d "${tmpDir:-}" ] && return 0 - if [ -n "${1-}" ]; then + if [ -n "${1:-}" ]; then tmpDir="${TMPDIR:-/tmp/}${1}.$RANDOM.$RANDOM.$$" else tmpDir="${TMPDIR:-/tmp/}$(basename "$0").$RANDOM.$RANDOM.$RANDOM.$$" @@ -258,7 +258,7 @@ _acquireScriptLock_() { # If the lock was acquired it's automatically released in _safeExit_() local lock_dir - if [[ ${1-} == 'system' ]]; then + if [[ ${1:-} == 'system' ]]; then lock_dir="${TMPDIR:-/tmp/}$(basename "$0").lock" else lock_dir="${TMPDIR:-/tmp/}$(basename "$0").$UID.lock" @@ -324,7 +324,7 @@ _parseOptions_() { unset options # Read the options and set stuff - while [[ ${1-} == -?* ]]; do + while [[ ${1:-} == -?* ]]; do case $1 in -h | --help) _usage_ >&2 diff --git a/utilities/alerts.bash b/utilities/alerts.bash index 23c9fac..4141e13 100644 --- a/utilities/alerts.bash +++ b/utilities/alerts.bash @@ -39,7 +39,7 @@ _alert_() { local function_name color local alertType="${1}" local message="${2}" - local line="${3-}" # Optional line number + local line="${3:-}" # Optional line number if [[ -n "${line}" && "${alertType}" =~ ^(fatal|error) && "${FUNCNAME[2]}" != "_trapCleanup_" ]]; then message="${message} (line: ${line}) $(_functionStack_)" @@ -84,7 +84,7 @@ _alert_() { _writeToLog_() { [[ "${alertType}" == "input" ]] && return 0 [[ "${LOGLEVEL}" =~ (off|OFF|Off) ]] && return 0 - [ -z "${LOGFILE-}" ] && fatal "\$LOGFILE must be set" + [ -z "${LOGFILE:-}" ] && fatal "\$LOGFILE must be set" [ ! -d "$(dirname "${LOGFILE}")" ] && mkdir -p "$(dirname "${LOGFILE}")" [[ ! -f "${LOGFILE}" ]] && touch "${LOGFILE}" @@ -137,18 +137,18 @@ esac } # /_alert_ -error() { _alert_ error "${1}" "${2-}"; } -warning() { _alert_ warning "${1}" "${2-}"; } -notice() { _alert_ notice "${1}" "${2-}"; } -info() { _alert_ info "${1}" "${2-}"; } -success() { _alert_ success "${1}" "${2-}"; } -dryrun() { _alert_ dryrun "${1}" "${2-}"; } -input() { _alert_ input "${1}" "${2-}"; } -header() { _alert_ header "== ${1} ==" "${2-}"; } -die() { _alert_ fatal "${1}" "${2-}"; _safeExit_ "1" ; } -fatal() { _alert_ fatal "${1}" "${2-}"; _safeExit_ "1" ; } -debug() { _alert_ debug "${1}" "${2-}"; } -verbose() { _alert_ debug "${1}" "${2-}"; } +error() { _alert_ error "${1}" "${2:-}"; } +warning() { _alert_ warning "${1}" "${2:-}"; } +notice() { _alert_ notice "${1}" "${2:-}"; } +info() { _alert_ info "${1}" "${2:-}"; } +success() { _alert_ success "${1}" "${2:-}"; } +dryrun() { _alert_ dryrun "${1}" "${2:-}"; } +input() { _alert_ input "${1}" "${2:-}"; } +header() { _alert_ header "== ${1} ==" "${2:-}"; } +die() { _alert_ fatal "${1}" "${2:-}"; _safeExit_ "1" ; } +fatal() { _alert_ fatal "${1}" "${2:-}"; _safeExit_ "1" ; } +debug() { _alert_ debug "${1}" "${2:-}"; } +verbose() { _alert_ debug "${1}" "${2:-}"; } _functionStack_() { # DESC: Prints the function stack in use diff --git a/utilities/baseHelpers.bash b/utilities/baseHelpers.bash index f080550..038a7d7 100644 --- a/utilities/baseHelpers.bash +++ b/utilities/baseHelpers.bash @@ -54,7 +54,7 @@ _execute_() { VERBOSE=$saveVerbose return 0 fi - if [ -n "${2-}" ]; then + if [ -n "${2:-}" ]; then dryrun "${1} (${2})" "$(caller)" else dryrun "${1}" "$(caller)" @@ -239,7 +239,7 @@ _rootAvailable_() { local superuser if [[ ${EUID} -eq 0 ]]; then superuser=true - elif [[ -z ${1-} ]]; then + elif [[ -z ${1:-} ]]; then if command -v sudo &>/dev/null; then debug 'Sudo: Updating cached credentials ...' if ! sudo -v; then @@ -254,7 +254,7 @@ _rootAvailable_() { fi fi - if [[ -z ${superuser-} ]]; then + if [[ -z ${superuser:-} ]]; then notice 'Unable to acquire superuser credentials.' return 1 fi @@ -274,14 +274,14 @@ _runAsRoot_() { fatal 'Missing required argument to _runAsRoot_()!' fi - if [[ ${1-} =~ ^0$ ]]; then + if [[ ${1:-} =~ ^0$ ]]; then local skip_sudo=true shift fi if [[ ${EUID} -eq 0 ]]; then "$@" - elif [[ -z ${skip_sudo-} ]]; then + elif [[ -z ${skip_sudo:-} ]]; then sudo -H -- "$@" else fatal "Unable to run requested command as root: $*" @@ -298,7 +298,7 @@ _seekConfirmation_() { # something # fi - input "${1-}" + input "${1:-}" if "${FORCE}"; then debug "Forcing confirmation with '--force' flag set" echo -e "" @@ -339,7 +339,7 @@ _safeExit_() { # ARGS: $1 (optional) - Exit code (defaults to 0) # OUTS: None - if [[ -d "${script_lock-}" ]]; then + if [[ -d "${script_lock:-}" ]]; then if command rm -rf "${script_lock}"; then debug "Removing script lock" else @@ -347,8 +347,8 @@ _safeExit_() { fi fi - if [[ -n "${tmpDir-}" && -d "${tmpDir-}" ]]; then - if [[ ${1-} == 1 && -n "$(ls "${tmpDir}")" ]]; then + if [[ -n "${tmpDir:-}" && -d "${tmpDir:-}" ]]; then + if [[ ${1:-} == 1 && -n "$(ls "${tmpDir}")" ]]; then command rm -r "${tmpDir}" else command rm -r "${tmpDir}" diff --git a/utilities/files.bash b/utilities/files.bash index b1ded00..3684455 100644 --- a/utilities/files.bash +++ b/utilities/files.bash @@ -165,12 +165,12 @@ _parseFilename_() { PARSE_FULL="$(realpath "${fileToParse}")" \ && debug "\${PARSE_FULL}: ${PARSE_FULL:-}" PARSE_BASE=$(basename "${fileToParse}") \ - && debug "\${PARSE_BASE}: ${PARSE_BASE-}" + && debug "\${PARSE_BASE}: ${PARSE_BASE:-}" PARSE_PATH="$(realpath "$(dirname "${fileToParse}")")" \ && debug "\${PARSE_PATH}: ${PARSE_PATH:-}" # Detect some common multi-extensions - if [[ ! ${levels-} ]]; then + if [[ ! ${levels:-} ]]; then case $(tr '[:upper:]' '[:lower:]' <<<"${PARSE_BASE}") in *.tar.gz | *.tar.bz2) levels=2 ;; esac @@ -182,9 +182,9 @@ _parseFilename_() { for ((i = 0; i < levels; i++)); do ext=${fn##*.} if [ $i == 0 ]; then - exts=${ext}${exts-} + exts=${ext}${exts:-} else - exts=${ext}.${exts-} + exts=${ext}.${exts:-} fi fn=${fn%.$ext} done @@ -273,7 +273,7 @@ _extract_() { [[ $# -lt 1 ]] && fatal 'Missing required argument to _extract_()!' - [[ "${2-}" == "v" ]] && vv="v" + [[ "${2:-}" == "v" ]] && vv="v" if [ -f "$1" ]; then case "$1" in @@ -369,7 +369,7 @@ _makeSymlink_() { local s="$1" local d="$2" - local b="${3-}" + local b="${3:-}" local o # Fix files where $HOME is written as '~' @@ -459,7 +459,7 @@ _parseYAML_() { # https://gist.github.com/epiloque/8cf512c6d64641bde388 local yamlFile="${1:?_parseYAML_ needs a file}" - local prefix="${2-}" + local prefix="${2:-}" [ ! -s "${yamlFile}" ] && return 1 @@ -558,14 +558,14 @@ _uniqueFileName_() { filename="${filename%.*}" fi - newfile="${directory}/${filename}${extension-}" + newfile="${directory}/${filename}${extension:-}" if [ -e "${newfile}" ]; then n=1 - while [[ -e "${directory}/${filename}${extension-}${spacer}${n}" ]]; do + while [[ -e "${directory}/${filename}${extension:-}${spacer}${n}" ]]; do ((n++)) done - newfile="${directory}/${filename}${extension-}${spacer}${n}" + newfile="${directory}/${filename}${extension:-}${spacer}${n}" fi echo "${newfile}" diff --git a/utilities/textProcessing.bash b/utilities/textProcessing.bash index f9cc1a3..4e687a2 100644 --- a/utilities/textProcessing.bash +++ b/utilities/textProcessing.bash @@ -119,7 +119,7 @@ _stopWords_() { fi declare -a localStopWords=() - IFS=',' read -r -a localStopWords <<<"${2-}" + IFS=',' read -r -a localStopWords <<<"${2:-}" if [[ ${#localStopWords[@]} -gt 0 ]]; then for w in "${localStopWords[@]}"; do