mirror of
https://github.com/natelandau/shell-scripting-templates.git
synced 2025-11-10 14:13:45 -05:00
fix pass unset variable syntax
This commit is contained in:
@@ -64,7 +64,7 @@ _alert_() {
|
|||||||
local function_name color
|
local function_name color
|
||||||
local alertType="${1}"
|
local alertType="${1}"
|
||||||
local message="${2}"
|
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
|
if [[ -n "${line}" && "${alertType}" =~ ^(fatal|error) && "${FUNCNAME[2]}" != "_trapCleanup_" ]]; then
|
||||||
message="${message} (line: ${line}) $(_functionStack_)"
|
message="${message} (line: ${line}) $(_functionStack_)"
|
||||||
@@ -109,7 +109,7 @@ _alert_() {
|
|||||||
_writeToLog_() {
|
_writeToLog_() {
|
||||||
[[ "${alertType}" == "input" ]] && return 0
|
[[ "${alertType}" == "input" ]] && return 0
|
||||||
[[ "${LOGLEVEL}" =~ (off|OFF|Off) ]] && 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}")"
|
[ ! -d "$(dirname "${LOGFILE}")" ] && mkdir -p "$(dirname "${LOGFILE}")"
|
||||||
[[ ! -f "${LOGFILE}" ]] && touch "${LOGFILE}"
|
[[ ! -f "${LOGFILE}" ]] && touch "${LOGFILE}"
|
||||||
|
|
||||||
@@ -162,25 +162,25 @@ _alert_() {
|
|||||||
|
|
||||||
} # /_alert_
|
} # /_alert_
|
||||||
|
|
||||||
error() { _alert_ error "${1}" "${2-}"; }
|
error() { _alert_ error "${1}" "${2:-}"; }
|
||||||
warning() { _alert_ warning "${1}" "${2-}"; }
|
warning() { _alert_ warning "${1}" "${2:-}"; }
|
||||||
notice() { _alert_ notice "${1}" "${2-}"; }
|
notice() { _alert_ notice "${1}" "${2:-}"; }
|
||||||
info() { _alert_ info "${1}" "${2-}"; }
|
info() { _alert_ info "${1}" "${2:-}"; }
|
||||||
success() { _alert_ success "${1}" "${2-}"; }
|
success() { _alert_ success "${1}" "${2:-}"; }
|
||||||
dryrun() { _alert_ dryrun "${1}" "${2-}"; }
|
dryrun() { _alert_ dryrun "${1}" "${2:-}"; }
|
||||||
input() { _alert_ input "${1}" "${2-}"; }
|
input() { _alert_ input "${1}" "${2:-}"; }
|
||||||
header() { _alert_ header "== ${1} ==" "${2-}"; }
|
header() { _alert_ header "== ${1} ==" "${2:-}"; }
|
||||||
die() { _alert_ fatal "${1}" "${2-}"; _safeExit_ "1" ; }
|
die() { _alert_ fatal "${1}" "${2:-}"; _safeExit_ "1" ; }
|
||||||
fatal() { _alert_ fatal "${1}" "${2-}"; _safeExit_ "1" ; }
|
fatal() { _alert_ fatal "${1}" "${2:-}"; _safeExit_ "1" ; }
|
||||||
debug() { _alert_ debug "${1}" "${2-}"; }
|
debug() { _alert_ debug "${1}" "${2:-}"; }
|
||||||
verbose() { _alert_ debug "${1}" "${2-}"; }
|
verbose() { _alert_ debug "${1}" "${2:-}"; }
|
||||||
|
|
||||||
_safeExit_() {
|
_safeExit_() {
|
||||||
# DESC: Cleanup and exit from a script
|
# DESC: Cleanup and exit from a script
|
||||||
# ARGS: $1 (optional) - Exit code (defaults to 0)
|
# ARGS: $1 (optional) - Exit code (defaults to 0)
|
||||||
# OUTS: None
|
# OUTS: None
|
||||||
|
|
||||||
if [[ -d "${script_lock-}" ]]; then
|
if [[ -d "${script_lock:-}" ]]; then
|
||||||
if command rm -rf "${script_lock}"; then
|
if command rm -rf "${script_lock}"; then
|
||||||
debug "Removing script lock"
|
debug "Removing script lock"
|
||||||
else
|
else
|
||||||
@@ -188,8 +188,8 @@ _safeExit_() {
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ -n "${tmpDir-}" && -d "${tmpDir-}" ]]; then
|
if [[ -n "${tmpDir:-}" && -d "${tmpDir:-}" ]]; then
|
||||||
if [[ ${1-} == 1 && -n "$(ls "${tmpDir}")" ]]; then
|
if [[ ${1:-} == 1 && -n "$(ls "${tmpDir}")" ]]; then
|
||||||
command rm -r "${tmpDir}"
|
command rm -r "${tmpDir}"
|
||||||
else
|
else
|
||||||
command rm -r "${tmpDir}"
|
command rm -r "${tmpDir}"
|
||||||
@@ -211,19 +211,19 @@ _trapCleanup_() {
|
|||||||
# $6 - $BASH_SOURCE
|
# $6 - $BASH_SOURCE
|
||||||
# OUTS: None
|
# OUTS: None
|
||||||
|
|
||||||
local line=${1-} # LINENO
|
local line=${1:-} # LINENO
|
||||||
local linecallfunc=${2-}
|
local linecallfunc=${2:-}
|
||||||
local command="${3-}"
|
local command="${3:-}"
|
||||||
local funcstack="${4-}"
|
local funcstack="${4:-}"
|
||||||
local script="${5-}"
|
local script="${5:-}"
|
||||||
local sourced="${6-}"
|
local sourced="${6:-}"
|
||||||
|
|
||||||
funcstack="'$(echo "$funcstack" | sed -E 's/ / < /g')'"
|
funcstack="'$(echo "$funcstack" | sed -E 's/ / < /g')'"
|
||||||
|
|
||||||
if [[ "${script##*/}" == "${sourced##*/}" ]]; then
|
if [[ "${script##*/}" == "${sourced##*/}" ]]; then
|
||||||
fatal "${7-} command: '$command' (line: $line) [func: $(_functionStack_)]"
|
fatal "${7:-} command: '$command' (line: $line) [func: $(_functionStack_)]"
|
||||||
else
|
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
|
fi
|
||||||
|
|
||||||
_safeExit_ "1"
|
_safeExit_ "1"
|
||||||
@@ -237,7 +237,7 @@ _makeTempDir_() {
|
|||||||
|
|
||||||
[ -d "${tmpDir:-}" ] && return 0
|
[ -d "${tmpDir:-}" ] && return 0
|
||||||
|
|
||||||
if [ -n "${1-}" ]; then
|
if [ -n "${1:-}" ]; then
|
||||||
tmpDir="${TMPDIR:-/tmp/}${1}.$RANDOM.$RANDOM.$$"
|
tmpDir="${TMPDIR:-/tmp/}${1}.$RANDOM.$RANDOM.$$"
|
||||||
else
|
else
|
||||||
tmpDir="${TMPDIR:-/tmp/}$(basename "$0").$RANDOM.$RANDOM.$RANDOM.$$"
|
tmpDir="${TMPDIR:-/tmp/}$(basename "$0").$RANDOM.$RANDOM.$RANDOM.$$"
|
||||||
@@ -258,7 +258,7 @@ _acquireScriptLock_() {
|
|||||||
# If the lock was acquired it's automatically released in _safeExit_()
|
# If the lock was acquired it's automatically released in _safeExit_()
|
||||||
|
|
||||||
local lock_dir
|
local lock_dir
|
||||||
if [[ ${1-} == 'system' ]]; then
|
if [[ ${1:-} == 'system' ]]; then
|
||||||
lock_dir="${TMPDIR:-/tmp/}$(basename "$0").lock"
|
lock_dir="${TMPDIR:-/tmp/}$(basename "$0").lock"
|
||||||
else
|
else
|
||||||
lock_dir="${TMPDIR:-/tmp/}$(basename "$0").$UID.lock"
|
lock_dir="${TMPDIR:-/tmp/}$(basename "$0").$UID.lock"
|
||||||
@@ -324,7 +324,7 @@ _parseOptions_() {
|
|||||||
unset options
|
unset options
|
||||||
|
|
||||||
# Read the options and set stuff
|
# Read the options and set stuff
|
||||||
while [[ ${1-} == -?* ]]; do
|
while [[ ${1:-} == -?* ]]; do
|
||||||
case $1 in
|
case $1 in
|
||||||
-h | --help)
|
-h | --help)
|
||||||
_usage_ >&2
|
_usage_ >&2
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ _alert_() {
|
|||||||
local function_name color
|
local function_name color
|
||||||
local alertType="${1}"
|
local alertType="${1}"
|
||||||
local message="${2}"
|
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
|
if [[ -n "${line}" && "${alertType}" =~ ^(fatal|error) && "${FUNCNAME[2]}" != "_trapCleanup_" ]]; then
|
||||||
message="${message} (line: ${line}) $(_functionStack_)"
|
message="${message} (line: ${line}) $(_functionStack_)"
|
||||||
@@ -84,7 +84,7 @@ _alert_() {
|
|||||||
_writeToLog_() {
|
_writeToLog_() {
|
||||||
[[ "${alertType}" == "input" ]] && return 0
|
[[ "${alertType}" == "input" ]] && return 0
|
||||||
[[ "${LOGLEVEL}" =~ (off|OFF|Off) ]] && 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}")"
|
[ ! -d "$(dirname "${LOGFILE}")" ] && mkdir -p "$(dirname "${LOGFILE}")"
|
||||||
[[ ! -f "${LOGFILE}" ]] && touch "${LOGFILE}"
|
[[ ! -f "${LOGFILE}" ]] && touch "${LOGFILE}"
|
||||||
|
|
||||||
@@ -137,18 +137,18 @@ esac
|
|||||||
|
|
||||||
} # /_alert_
|
} # /_alert_
|
||||||
|
|
||||||
error() { _alert_ error "${1}" "${2-}"; }
|
error() { _alert_ error "${1}" "${2:-}"; }
|
||||||
warning() { _alert_ warning "${1}" "${2-}"; }
|
warning() { _alert_ warning "${1}" "${2:-}"; }
|
||||||
notice() { _alert_ notice "${1}" "${2-}"; }
|
notice() { _alert_ notice "${1}" "${2:-}"; }
|
||||||
info() { _alert_ info "${1}" "${2-}"; }
|
info() { _alert_ info "${1}" "${2:-}"; }
|
||||||
success() { _alert_ success "${1}" "${2-}"; }
|
success() { _alert_ success "${1}" "${2:-}"; }
|
||||||
dryrun() { _alert_ dryrun "${1}" "${2-}"; }
|
dryrun() { _alert_ dryrun "${1}" "${2:-}"; }
|
||||||
input() { _alert_ input "${1}" "${2-}"; }
|
input() { _alert_ input "${1}" "${2:-}"; }
|
||||||
header() { _alert_ header "== ${1} ==" "${2-}"; }
|
header() { _alert_ header "== ${1} ==" "${2:-}"; }
|
||||||
die() { _alert_ fatal "${1}" "${2-}"; _safeExit_ "1" ; }
|
die() { _alert_ fatal "${1}" "${2:-}"; _safeExit_ "1" ; }
|
||||||
fatal() { _alert_ fatal "${1}" "${2-}"; _safeExit_ "1" ; }
|
fatal() { _alert_ fatal "${1}" "${2:-}"; _safeExit_ "1" ; }
|
||||||
debug() { _alert_ debug "${1}" "${2-}"; }
|
debug() { _alert_ debug "${1}" "${2:-}"; }
|
||||||
verbose() { _alert_ debug "${1}" "${2-}"; }
|
verbose() { _alert_ debug "${1}" "${2:-}"; }
|
||||||
|
|
||||||
_functionStack_() {
|
_functionStack_() {
|
||||||
# DESC: Prints the function stack in use
|
# DESC: Prints the function stack in use
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ _execute_() {
|
|||||||
VERBOSE=$saveVerbose
|
VERBOSE=$saveVerbose
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
if [ -n "${2-}" ]; then
|
if [ -n "${2:-}" ]; then
|
||||||
dryrun "${1} (${2})" "$(caller)"
|
dryrun "${1} (${2})" "$(caller)"
|
||||||
else
|
else
|
||||||
dryrun "${1}" "$(caller)"
|
dryrun "${1}" "$(caller)"
|
||||||
@@ -239,7 +239,7 @@ _rootAvailable_() {
|
|||||||
local superuser
|
local superuser
|
||||||
if [[ ${EUID} -eq 0 ]]; then
|
if [[ ${EUID} -eq 0 ]]; then
|
||||||
superuser=true
|
superuser=true
|
||||||
elif [[ -z ${1-} ]]; then
|
elif [[ -z ${1:-} ]]; then
|
||||||
if command -v sudo &>/dev/null; then
|
if command -v sudo &>/dev/null; then
|
||||||
debug 'Sudo: Updating cached credentials ...'
|
debug 'Sudo: Updating cached credentials ...'
|
||||||
if ! sudo -v; then
|
if ! sudo -v; then
|
||||||
@@ -254,7 +254,7 @@ _rootAvailable_() {
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ -z ${superuser-} ]]; then
|
if [[ -z ${superuser:-} ]]; then
|
||||||
notice 'Unable to acquire superuser credentials.'
|
notice 'Unable to acquire superuser credentials.'
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
@@ -274,14 +274,14 @@ _runAsRoot_() {
|
|||||||
fatal 'Missing required argument to _runAsRoot_()!'
|
fatal 'Missing required argument to _runAsRoot_()!'
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ ${1-} =~ ^0$ ]]; then
|
if [[ ${1:-} =~ ^0$ ]]; then
|
||||||
local skip_sudo=true
|
local skip_sudo=true
|
||||||
shift
|
shift
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ ${EUID} -eq 0 ]]; then
|
if [[ ${EUID} -eq 0 ]]; then
|
||||||
"$@"
|
"$@"
|
||||||
elif [[ -z ${skip_sudo-} ]]; then
|
elif [[ -z ${skip_sudo:-} ]]; then
|
||||||
sudo -H -- "$@"
|
sudo -H -- "$@"
|
||||||
else
|
else
|
||||||
fatal "Unable to run requested command as root: $*"
|
fatal "Unable to run requested command as root: $*"
|
||||||
@@ -298,7 +298,7 @@ _seekConfirmation_() {
|
|||||||
# something
|
# something
|
||||||
# fi
|
# fi
|
||||||
|
|
||||||
input "${1-}"
|
input "${1:-}"
|
||||||
if "${FORCE}"; then
|
if "${FORCE}"; then
|
||||||
debug "Forcing confirmation with '--force' flag set"
|
debug "Forcing confirmation with '--force' flag set"
|
||||||
echo -e ""
|
echo -e ""
|
||||||
@@ -339,7 +339,7 @@ _safeExit_() {
|
|||||||
# ARGS: $1 (optional) - Exit code (defaults to 0)
|
# ARGS: $1 (optional) - Exit code (defaults to 0)
|
||||||
# OUTS: None
|
# OUTS: None
|
||||||
|
|
||||||
if [[ -d "${script_lock-}" ]]; then
|
if [[ -d "${script_lock:-}" ]]; then
|
||||||
if command rm -rf "${script_lock}"; then
|
if command rm -rf "${script_lock}"; then
|
||||||
debug "Removing script lock"
|
debug "Removing script lock"
|
||||||
else
|
else
|
||||||
@@ -347,8 +347,8 @@ _safeExit_() {
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ -n "${tmpDir-}" && -d "${tmpDir-}" ]]; then
|
if [[ -n "${tmpDir:-}" && -d "${tmpDir:-}" ]]; then
|
||||||
if [[ ${1-} == 1 && -n "$(ls "${tmpDir}")" ]]; then
|
if [[ ${1:-} == 1 && -n "$(ls "${tmpDir}")" ]]; then
|
||||||
command rm -r "${tmpDir}"
|
command rm -r "${tmpDir}"
|
||||||
else
|
else
|
||||||
command rm -r "${tmpDir}"
|
command rm -r "${tmpDir}"
|
||||||
|
|||||||
@@ -165,12 +165,12 @@ _parseFilename_() {
|
|||||||
PARSE_FULL="$(realpath "${fileToParse}")" \
|
PARSE_FULL="$(realpath "${fileToParse}")" \
|
||||||
&& debug "\${PARSE_FULL}: ${PARSE_FULL:-}"
|
&& debug "\${PARSE_FULL}: ${PARSE_FULL:-}"
|
||||||
PARSE_BASE=$(basename "${fileToParse}") \
|
PARSE_BASE=$(basename "${fileToParse}") \
|
||||||
&& debug "\${PARSE_BASE}: ${PARSE_BASE-}"
|
&& debug "\${PARSE_BASE}: ${PARSE_BASE:-}"
|
||||||
PARSE_PATH="$(realpath "$(dirname "${fileToParse}")")" \
|
PARSE_PATH="$(realpath "$(dirname "${fileToParse}")")" \
|
||||||
&& debug "\${PARSE_PATH}: ${PARSE_PATH:-}"
|
&& debug "\${PARSE_PATH}: ${PARSE_PATH:-}"
|
||||||
|
|
||||||
# Detect some common multi-extensions
|
# Detect some common multi-extensions
|
||||||
if [[ ! ${levels-} ]]; then
|
if [[ ! ${levels:-} ]]; then
|
||||||
case $(tr '[:upper:]' '[:lower:]' <<<"${PARSE_BASE}") in
|
case $(tr '[:upper:]' '[:lower:]' <<<"${PARSE_BASE}") in
|
||||||
*.tar.gz | *.tar.bz2) levels=2 ;;
|
*.tar.gz | *.tar.bz2) levels=2 ;;
|
||||||
esac
|
esac
|
||||||
@@ -182,9 +182,9 @@ _parseFilename_() {
|
|||||||
for ((i = 0; i < levels; i++)); do
|
for ((i = 0; i < levels; i++)); do
|
||||||
ext=${fn##*.}
|
ext=${fn##*.}
|
||||||
if [ $i == 0 ]; then
|
if [ $i == 0 ]; then
|
||||||
exts=${ext}${exts-}
|
exts=${ext}${exts:-}
|
||||||
else
|
else
|
||||||
exts=${ext}.${exts-}
|
exts=${ext}.${exts:-}
|
||||||
fi
|
fi
|
||||||
fn=${fn%.$ext}
|
fn=${fn%.$ext}
|
||||||
done
|
done
|
||||||
@@ -273,7 +273,7 @@ _extract_() {
|
|||||||
|
|
||||||
[[ $# -lt 1 ]] && fatal 'Missing required argument to _extract_()!'
|
[[ $# -lt 1 ]] && fatal 'Missing required argument to _extract_()!'
|
||||||
|
|
||||||
[[ "${2-}" == "v" ]] && vv="v"
|
[[ "${2:-}" == "v" ]] && vv="v"
|
||||||
|
|
||||||
if [ -f "$1" ]; then
|
if [ -f "$1" ]; then
|
||||||
case "$1" in
|
case "$1" in
|
||||||
@@ -369,7 +369,7 @@ _makeSymlink_() {
|
|||||||
|
|
||||||
local s="$1"
|
local s="$1"
|
||||||
local d="$2"
|
local d="$2"
|
||||||
local b="${3-}"
|
local b="${3:-}"
|
||||||
local o
|
local o
|
||||||
|
|
||||||
# Fix files where $HOME is written as '~'
|
# Fix files where $HOME is written as '~'
|
||||||
@@ -459,7 +459,7 @@ _parseYAML_() {
|
|||||||
# https://gist.github.com/epiloque/8cf512c6d64641bde388
|
# https://gist.github.com/epiloque/8cf512c6d64641bde388
|
||||||
|
|
||||||
local yamlFile="${1:?_parseYAML_ needs a file}"
|
local yamlFile="${1:?_parseYAML_ needs a file}"
|
||||||
local prefix="${2-}"
|
local prefix="${2:-}"
|
||||||
|
|
||||||
[ ! -s "${yamlFile}" ] && return 1
|
[ ! -s "${yamlFile}" ] && return 1
|
||||||
|
|
||||||
@@ -558,14 +558,14 @@ _uniqueFileName_() {
|
|||||||
filename="${filename%.*}"
|
filename="${filename%.*}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
newfile="${directory}/${filename}${extension-}"
|
newfile="${directory}/${filename}${extension:-}"
|
||||||
|
|
||||||
if [ -e "${newfile}" ]; then
|
if [ -e "${newfile}" ]; then
|
||||||
n=1
|
n=1
|
||||||
while [[ -e "${directory}/${filename}${extension-}${spacer}${n}" ]]; do
|
while [[ -e "${directory}/${filename}${extension:-}${spacer}${n}" ]]; do
|
||||||
((n++))
|
((n++))
|
||||||
done
|
done
|
||||||
newfile="${directory}/${filename}${extension-}${spacer}${n}"
|
newfile="${directory}/${filename}${extension:-}${spacer}${n}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "${newfile}"
|
echo "${newfile}"
|
||||||
|
|||||||
@@ -119,7 +119,7 @@ _stopWords_() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
declare -a localStopWords=()
|
declare -a localStopWords=()
|
||||||
IFS=',' read -r -a localStopWords <<<"${2-}"
|
IFS=',' read -r -a localStopWords <<<"${2:-}"
|
||||||
|
|
||||||
if [[ ${#localStopWords[@]} -gt 0 ]]; then
|
if [[ ${#localStopWords[@]} -gt 0 ]]; then
|
||||||
for w in "${localStopWords[@]}"; do
|
for w in "${localStopWords[@]}"; do
|
||||||
|
|||||||
Reference in New Issue
Block a user