mirror of
https://github.com/natelandau/shell-scripting-templates.git
synced 2025-11-15 00:13:47 -05:00
Improve shellcheck compatibility
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
# Functions for providing alerts to the user and logging them
|
||||
# shellcheck disable=SC2034,SC2154
|
||||
|
||||
_setColors_() {
|
||||
# DESC:
|
||||
@@ -8,7 +9,7 @@ _setColors_() {
|
||||
# OUTS:
|
||||
# None
|
||||
# USAGE:
|
||||
# echo "${blue}Some text${reset}"
|
||||
# printf "%s\n" "${blue}Some text${reset}"
|
||||
|
||||
if tput setaf 1 >/dev/null 2>&1; then
|
||||
bold=$(tput bold)
|
||||
@@ -39,6 +40,7 @@ _setColors_() {
|
||||
bold="\033[4;37m"
|
||||
reset="\033[0m"
|
||||
underline="\033[4;37m"
|
||||
# shellcheck disable=SC2034
|
||||
reverse=""
|
||||
white="\033[0;37m"
|
||||
blue="\033[0;34m"
|
||||
@@ -96,9 +98,9 @@ _alert_() {
|
||||
_color="${purple}"
|
||||
elif [ "${_alertType}" == "header" ]; then
|
||||
_color="${bold}${white}${underline}"
|
||||
elif [ ${_alertType} == "notice" ]; then
|
||||
elif [ "${_alertType}" == "notice" ]; then
|
||||
_color="${bold}"
|
||||
elif [ ${_alertType} == "input" ]; then
|
||||
elif [ "${_alertType}" == "input" ]; then
|
||||
_color="${bold}${underline}"
|
||||
elif [ "${_alertType}" = "dryrun" ]; then
|
||||
_color="${blue}"
|
||||
@@ -133,9 +135,10 @@ _alert_() {
|
||||
[[ ! -f ${LOGFILE} ]] && touch "${LOGFILE}"
|
||||
|
||||
# Don't use colors in logs
|
||||
local cleanmessage="$(echo "${_message}" | sed -E 's/(\x1b)?\[(([0-9]{1,2})(;[0-9]{1,3}){0,2})?[mGK]//g')"
|
||||
local _cleanmessage
|
||||
_cleanmessage="$(printf "%s" "${_message}" | sed -E 's/(\x1b)?\[(([0-9]{1,2})(;[0-9]{1,3}){0,2})?[mGK]//g')"
|
||||
# Print message to log file
|
||||
printf "%s [%7s] %s %s\n" "$(date +"%b %d %R:%S")" "${_alertType}" "[$(/bin/hostname)]" "${cleanmessage}" >>"${LOGFILE}"
|
||||
printf "%s [%7s] %s %s\n" "$(date +"%b %d %R:%S")" "${_alertType}" "[$(/bin/hostname)]" "${_cleanmessage}" >>"${LOGFILE}"
|
||||
}
|
||||
|
||||
# Write specified log level data to logfile
|
||||
@@ -210,7 +213,7 @@ _printFuncStack_() {
|
||||
_funcStackResponse=()
|
||||
for ((_i = 1; _i < ${#BASH_SOURCE[@]}; _i++)); do
|
||||
case "${FUNCNAME[$_i]}" in "_alert_" | "_trapCleanup_" | fatal | error | warning | notice | info | debug | dryrun | header | success) continue ;; esac
|
||||
_funcStackResponse+=("${FUNCNAME[$_i]}:$(basename ${BASH_SOURCE[$_i]}):${BASH_LINENO[_i - 1]}")
|
||||
_funcStackResponse+=("${FUNCNAME[$_i]}:$(basename "${BASH_SOURCE[$_i]}"):${BASH_LINENO[_i - 1]}")
|
||||
done
|
||||
printf "( "
|
||||
printf %s "${_funcStackResponse[0]}"
|
||||
|
||||
@@ -190,7 +190,7 @@ _forEachReject_() {
|
||||
fi
|
||||
declare -i _ret=$?
|
||||
if [[ ${_ret} -ne 0 ]]; then
|
||||
echo "${_it}"
|
||||
printf "%s\n" "${_it}"
|
||||
fi
|
||||
done
|
||||
}
|
||||
@@ -211,7 +211,7 @@ _forEachSome_() {
|
||||
|
||||
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
|
||||
local _func="${1}"
|
||||
local _IFS=$'\n'
|
||||
local IFS=$'\n'
|
||||
while read -r _it; do
|
||||
|
||||
if [[ ${_func} == *"$"* ]]; then
|
||||
|
||||
@@ -135,7 +135,7 @@ _isFQDN_() {
|
||||
|
||||
local _input="${1}"
|
||||
|
||||
if printf "${_input}" | grep -Pq '(?=^.{4,253}$)(^(?:[a-zA-Z0-9](?:(?:[a-zA-Z0-9\-]){0,61}[a-zA-Z0-9])?\.)+([a-zA-Z]{2,}|xn--[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])$)'; then
|
||||
if printf "%s" "${_input}" | grep -Pq '(?=^.{4,253}$)(^(?:[a-zA-Z0-9](?:(?:[a-zA-Z0-9\-]){0,61}[a-zA-Z0-9])?\.)+([a-zA-Z]{2,}|xn--[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])$)'; then
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
@@ -218,7 +218,7 @@ _isDir_() {
|
||||
# 1 - Input is not a directory
|
||||
# USAGE:
|
||||
# _varIsDir_ "${var}"
|
||||
# (_isDir_ "${var}") && echo "Is a directory" || echo "Not a directory"
|
||||
# (_isDir_ "${var}") && printf "Is a directory" || printf "Not a directory"
|
||||
# NOTES:
|
||||
#
|
||||
|
||||
@@ -274,7 +274,7 @@ _rootAvailable_() {
|
||||
# https://github.com/ralish/bash-script-template
|
||||
|
||||
local _superuser
|
||||
local _testEUID
|
||||
|
||||
if [[ ${EUID} -eq 0 ]]; then
|
||||
_superuser=true
|
||||
elif [[ -z ${1:-} ]]; then
|
||||
|
||||
@@ -46,7 +46,7 @@ _countdown_() {
|
||||
else
|
||||
echo "${_message} ${ii}"
|
||||
fi
|
||||
sleep ${_sleepTime}
|
||||
sleep "${_sleepTime}"
|
||||
done
|
||||
}
|
||||
|
||||
@@ -120,7 +120,7 @@ _fromSeconds_() {
|
||||
((_h = ${1} / 3600))
|
||||
((_m = (${1} % 3600) / 60))
|
||||
((_s = ${1} % 60))
|
||||
printf "%02d:%02d:%02d\n" ${_h} ${_m} ${_s}
|
||||
printf "%02d:%02d:%02d\n" "${_h}" "${_m}" "${_s}"
|
||||
}
|
||||
|
||||
_monthToNumber_() {
|
||||
@@ -135,7 +135,9 @@ _monthToNumber_() {
|
||||
|
||||
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
|
||||
|
||||
local _mon="$(echo "$1" | tr '[:upper:]' '[:lower:]')"
|
||||
local _mon
|
||||
_mon="$(echo "$1" | tr '[:upper:]' '[:lower:]')"
|
||||
|
||||
case "${_mon}" in
|
||||
january | jan | ja) echo 1 ;;
|
||||
february | feb | fe) echo 2 ;;
|
||||
@@ -232,7 +234,7 @@ _parseDate_() {
|
||||
trap "$(shopt -p nocasematch)" RETURN # reset nocasematch when function exits
|
||||
shopt -s nocasematch # Use case-insensitive regex
|
||||
|
||||
debug "_parseDate_() input ${tan}${_stringToTest}${purple}"
|
||||
debug "_parseDate_() input: ${_stringToTest}"
|
||||
|
||||
# YYYY MM DD or YYYY-MM-DD
|
||||
_pat="(.*[^0-9]|^)((20[0-2][0-9])[-\.\/_ ]+([0-9]{1,2})[-\.\/_ ]+([0-9]{1,2}))([^0-9].*|$)"
|
||||
@@ -242,25 +244,25 @@ _parseDate_() {
|
||||
PARSE_DATE_MONTH=$((10#${BASH_REMATCH[4]}))
|
||||
PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")"
|
||||
PARSE_DATE_DAY=$((10#${BASH_REMATCH[5]}))
|
||||
debug "regex match: ${tan}YYYY-MM-DD${purple}"
|
||||
debug "regex match: YYYY-MM-DD "
|
||||
|
||||
# Month DD, YYYY
|
||||
elif [[ ${_stringToTest} =~ ((january|jan|ja|february|feb|fe|march|mar|ma|april|apr|ap|may|june|jun|july|jul|ju|august|aug|september|sep|october|oct|november|nov|december|dec)[-\./_ ]+([0-9]{1,2})(nd|rd|th|st)?,?[-\./_ ]+(20[0-2][0-9]))([^0-9].*|$) ]]; then
|
||||
PARSE_DATE_FOUND="${BASH_REMATCH[1]:-}"
|
||||
PARSE_DATE_MONTH=$(_monthToNumber_ ${BASH_REMATCH[2]:-})
|
||||
PARSE_DATE_MONTH=$(_monthToNumber_ "${BASH_REMATCH[2]:-}")
|
||||
PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH:-}")"
|
||||
PARSE_DATE_DAY=$((10#${BASH_REMATCH[3]:-}))
|
||||
PARSE_DATE_YEAR=$((10#${BASH_REMATCH[5]:-}))
|
||||
debug "regex match: ${tan}Month DD, YYYY${purple}"
|
||||
debug "regex match: Month DD, YYYY"
|
||||
|
||||
# Month DD, YY
|
||||
elif [[ ${_stringToTest} =~ ((january|jan|ja|february|feb|fe|march|mar|ma|april|apr|ap|may|june|jun|july|jul|ju|august|aug|september|sep|october|oct|november|nov|december|dec)[-\./_ ]+([0-9]{1,2})(nd|rd|th|st)?,?[-\./_ ]+([0-9]{2}))([^0-9].*|$) ]]; then
|
||||
PARSE_DATE_FOUND="${BASH_REMATCH[1]}"
|
||||
PARSE_DATE_MONTH=$(_monthToNumber_ ${BASH_REMATCH[2]})
|
||||
PARSE_DATE_MONTH=$(_monthToNumber_ "${BASH_REMATCH[2]}")
|
||||
PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")"
|
||||
PARSE_DATE_DAY=$((10#${BASH_REMATCH[3]}))
|
||||
PARSE_DATE_YEAR="20$((10#${BASH_REMATCH[5]}))"
|
||||
debug "regex match: ${tan}Month DD, YY${purple}"
|
||||
debug "regex match: Month DD, YY"
|
||||
|
||||
# DD Month YYYY
|
||||
elif [[ ${_stringToTest} =~ (.*[^0-9]|^)(([0-9]{2})[-\./_ ]+(january|jan|ja|february|feb|fe|march|mar|ma|april|apr|ap|may|june|jun|july|jul|ju|august|aug|september|sep|october|oct|november|nov|december|dec),?[-\./_ ]+(20[0-2][0-9]))([^0-9].*|$) ]]; then
|
||||
@@ -269,7 +271,7 @@ _parseDate_() {
|
||||
PARSE_DATE_MONTH="$(_monthToNumber_ "${BASH_REMATCH[4]}")"
|
||||
PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")"
|
||||
PARSE_DATE_YEAR=$((10#"${BASH_REMATCH[5]}"))
|
||||
debug "regex match: ${tan}DD Month, YYYY${purple}"
|
||||
debug "regex match: DD Month, YYYY"
|
||||
|
||||
# MM-DD-YYYY or DD-MM-YYYY
|
||||
elif [[ ${_stringToTest} =~ (.*[^0-9]|^)(([0-9]{1,2})[-\.\/_ ]+([0-9]{1,2})[-\.\/_ ]+(20[0-2][0-9]))([^0-9].*|$) ]]; then
|
||||
@@ -283,7 +285,7 @@ _parseDate_() {
|
||||
PARSE_DATE_MONTH=$((10#${BASH_REMATCH[3]}))
|
||||
PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")"
|
||||
PARSE_DATE_DAY=$((10#${BASH_REMATCH[4]}))
|
||||
debug "regex match: ${tan}MM-DD-YYYY${purple}"
|
||||
debug "regex match: MM-DD-YYYY"
|
||||
elif [[ $((10#${BASH_REMATCH[3]})) -gt 12 &&
|
||||
$((10#${BASH_REMATCH[3]})) -lt 32 &&
|
||||
$((10#${BASH_REMATCH[4]})) -lt 13 ]] \
|
||||
@@ -293,7 +295,7 @@ _parseDate_() {
|
||||
PARSE_DATE_MONTH=$((10#${BASH_REMATCH[4]}))
|
||||
PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")"
|
||||
PARSE_DATE_DAY=$((10#${BASH_REMATCH[3]}))
|
||||
debug "regex match: ${tan}DD-MM-YYYY${purple}"
|
||||
debug "regex match: DD-MM-YYYY"
|
||||
elif [[ $((10#${BASH_REMATCH[3]})) -lt 32 &&
|
||||
$((10#${BASH_REMATCH[4]})) -lt 13 ]] \
|
||||
; then
|
||||
@@ -302,7 +304,7 @@ _parseDate_() {
|
||||
PARSE_DATE_MONTH=$((10#${BASH_REMATCH[3]}))
|
||||
PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")"
|
||||
PARSE_DATE_DAY=$((10#${BASH_REMATCH[4]}))
|
||||
debug "regex match: ${tan}MM-DD-YYYY${purple}"
|
||||
debug "regex match: MM-DD-YYYY"
|
||||
else
|
||||
shopt -u nocasematch
|
||||
return 1
|
||||
@@ -319,7 +321,7 @@ _parseDate_() {
|
||||
PARSE_DATE_MONTH=$((10#${BASH_REMATCH[3]}))
|
||||
PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")"
|
||||
PARSE_DATE_DAY=$((10#${BASH_REMATCH[4]}))
|
||||
debug "regex match: ${tan}MM-DD-YYYY${purple}"
|
||||
debug "regex match: MM-DD-YYYY"
|
||||
elif [[ $((10#${BASH_REMATCH[3]})) -gt 12 &&
|
||||
$((10#${BASH_REMATCH[3]})) -lt 32 &&
|
||||
$((10#${BASH_REMATCH[4]})) -lt 13 ]] \
|
||||
@@ -329,7 +331,7 @@ _parseDate_() {
|
||||
PARSE_DATE_MONTH=$((10#${BASH_REMATCH[4]}))
|
||||
PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")"
|
||||
PARSE_DATE_DAY=$((10#${BASH_REMATCH[3]}))
|
||||
debug "regex match: ${tan}DD-MM-YYYY${purple}"
|
||||
debug "regex match: DD-MM-YYYY"
|
||||
elif [[ $((10#${BASH_REMATCH[3]})) -lt 32 &&
|
||||
$((10#${BASH_REMATCH[4]})) -lt 13 ]] \
|
||||
; then
|
||||
@@ -338,7 +340,7 @@ _parseDate_() {
|
||||
PARSE_DATE_MONTH=$((10#${BASH_REMATCH[3]}))
|
||||
PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")"
|
||||
PARSE_DATE_DAY=$((10#${BASH_REMATCH[4]}))
|
||||
debug "regex match: ${tan}MM-DD-YYYY${purple}"
|
||||
debug "regex match: MM-DD-YYYY"
|
||||
else
|
||||
shopt -u nocasematch
|
||||
return 1
|
||||
@@ -349,9 +351,9 @@ _parseDate_() {
|
||||
PARSE_DATE_FOUND="${BASH_REMATCH[1]}"
|
||||
PARSE_DATE_DAY="1"
|
||||
PARSE_DATE_MONTH="$(_monthToNumber_ "${BASH_REMATCH[2]}")"
|
||||
PARSE_DATE_MONTH_NAME="$(_numberToMonth_ $PARSE_DATE_MONTH)"
|
||||
PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")"
|
||||
PARSE_DATE_YEAR="$((10#${BASH_REMATCH[3]}))"
|
||||
debug "regex match: ${tan}Month, YYYY${purple}"
|
||||
debug "regex match: Month, YYYY"
|
||||
|
||||
# YYYYMMDDHHMM
|
||||
elif [[ ${_stringToTest} =~ (.*[^0-9]|^)((20[0-2][0-9])([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2}))([^0-9].*|$) ]]; then
|
||||
@@ -362,7 +364,7 @@ _parseDate_() {
|
||||
PARSE_DATE_YEAR="$((10#${BASH_REMATCH[3]}))"
|
||||
PARSE_DATE_HOUR="$((10#${BASH_REMATCH[6]}))"
|
||||
PARSE_DATE_MINUTE="$((10#${BASH_REMATCH[7]}))"
|
||||
debug "regex match: ${tan}YYYYMMDDHHMM${purple}"
|
||||
debug "regex match: YYYYMMDDHHMM"
|
||||
|
||||
# YYYYMMDDHH 1 2 3 4 5 6
|
||||
elif [[ ${_stringToTest} =~ (.*[^0-9]|^)((20[0-2][0-9])([0-9]{2})([0-9]{2})([0-9]{2}))([^0-9].*|$) ]]; then
|
||||
@@ -373,7 +375,7 @@ _parseDate_() {
|
||||
PARSE_DATE_YEAR="$((10#${BASH_REMATCH[3]}))"
|
||||
PARSE_DATE_HOUR="${BASH_REMATCH[6]}"
|
||||
PARSE_DATE_MINUTE="00"
|
||||
debug "regex match: ${tan}YYYYMMDDHHMM${purple}"
|
||||
debug "regex match: YYYYMMDDHHMM"
|
||||
|
||||
# MMDDYYYY or YYYYMMDD or DDMMYYYY
|
||||
# 1 2 3 4 5 6
|
||||
@@ -389,7 +391,7 @@ _parseDate_() {
|
||||
PARSE_DATE_MONTH="$((10#${BASH_REMATCH[3]}))"
|
||||
PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")"
|
||||
PARSE_DATE_YEAR="${BASH_REMATCH[5]}${BASH_REMATCH[6]}"
|
||||
debug "regex match: ${tan}MMDDYYYY${purple}"
|
||||
debug "regex match: MMDDYYYY"
|
||||
# DDMMYYYY
|
||||
elif [[ $((10#${BASH_REMATCH[5]})) -eq 20 &&
|
||||
$((10#${BASH_REMATCH[3]})) -gt 12 &&
|
||||
@@ -401,7 +403,7 @@ _parseDate_() {
|
||||
PARSE_DATE_MONTH="$((10#${BASH_REMATCH[4]}))"
|
||||
PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")"
|
||||
PARSE_DATE_YEAR="${BASH_REMATCH[5]}${BASH_REMATCH[6]}"
|
||||
debug "regex match: ${tan}DDMMYYYY${purple}"
|
||||
debug "regex match: DDMMYYYY"
|
||||
# YYYYMMDD
|
||||
elif [[ $((10#${BASH_REMATCH[3]})) -eq 20 &&
|
||||
$((10#${BASH_REMATCH[6]})) -gt 12 &&
|
||||
@@ -413,7 +415,7 @@ _parseDate_() {
|
||||
PARSE_DATE_MONTH="$((10#${BASH_REMATCH[5]}))"
|
||||
PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")"
|
||||
PARSE_DATE_YEAR="${BASH_REMATCH[3]}${BASH_REMATCH[4]}"
|
||||
debug "regex match: ${tan}YYYYMMDD${purple}"
|
||||
debug "regex match: YYYYMMDD"
|
||||
# YYYYDDMM
|
||||
elif [[ $((10#${BASH_REMATCH[3]})) -eq 20 &&
|
||||
$((10#${BASH_REMATCH[5]})) -gt 12 &&
|
||||
@@ -425,7 +427,7 @@ _parseDate_() {
|
||||
PARSE_DATE_MONTH="$((10#${BASH_REMATCH[6]}))"
|
||||
PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")"
|
||||
PARSE_DATE_YEAR="${BASH_REMATCH[3]}${BASH_REMATCH[4]}"
|
||||
debug "regex match: ${tan}YYYYMMDD${purple}"
|
||||
debug "regex match: YYYYMMDD"
|
||||
# Assume YYYMMDD
|
||||
elif [[ $((10#${BASH_REMATCH[3]})) -eq 20 &&
|
||||
$((10#${BASH_REMATCH[6]})) -lt 32 &&
|
||||
@@ -436,7 +438,7 @@ _parseDate_() {
|
||||
PARSE_DATE_MONTH="$((10#${BASH_REMATCH[5]}))"
|
||||
PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")"
|
||||
PARSE_DATE_YEAR="${BASH_REMATCH[3]}${BASH_REMATCH[4]}"
|
||||
debug "regex match: ${tan}YYYYMMDD${purple}"
|
||||
debug "regex match: YYYYMMDD"
|
||||
else
|
||||
shopt -u nocasematch
|
||||
return 1
|
||||
@@ -485,13 +487,13 @@ _parseDate_() {
|
||||
return 1
|
||||
}
|
||||
|
||||
debug "${tan}\$PARSE_DATE_FOUND: ${PARSE_DATE_FOUND}${purple}"
|
||||
debug "${tan}\$PARSE_DATE_YEAR: ${PARSE_DATE_YEAR}${purple}"
|
||||
debug "${tan}\$PARSE_DATE_MONTH: ${PARSE_DATE_MONTH}${purple}"
|
||||
debug "${tan}\$PARSE_DATE_MONTH_NAME: ${PARSE_DATE_MONTH_NAME}${purple}"
|
||||
debug "${tan}\$PARSE_DATE_DAY: ${PARSE_DATE_DAY}${purple}"
|
||||
[[ -z ${PARSE_DATE_HOUR:-} ]] || debug "${tan}\$PARSE_DATE_HOUR: ${PARSE_DATE_HOUR}${purple}"
|
||||
[[ -z ${PARSE_DATE_MINUTE:-} ]] || debug "${tan}\$PARSE_DATE_MINUTE: ${PARSE_DATE_MINUTE}${purple}"
|
||||
debug "\$PARSE_DATE_FOUND: ${PARSE_DATE_FOUND}"
|
||||
debug "\$PARSE_DATE_YEAR: ${PARSE_DATE_YEAR}"
|
||||
debug "\$PARSE_DATE_MONTH: ${PARSE_DATE_MONTH}"
|
||||
debug "\$PARSE_DATE_MONTH_NAME: ${PARSE_DATE_MONTH_NAME}"
|
||||
debug "\$PARSE_DATE_DAY: ${PARSE_DATE_DAY}"
|
||||
[[ -z ${PARSE_DATE_HOUR:-} ]] || debug "\$PARSE_DATE_HOUR: ${PARSE_DATE_HOUR}"
|
||||
[[ -z ${PARSE_DATE_MINUTE:-} ]] || debug "\$PARSE_DATE_MINUTE: ${PARSE_DATE_MINUTE}"
|
||||
|
||||
shopt -u nocasematch
|
||||
|
||||
@@ -527,7 +529,8 @@ _readableUnixTimestamp_() {
|
||||
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
|
||||
local _timestamp="${1}"
|
||||
local _format="${2:-"%F %T"}"
|
||||
local _out="$(date -d "@${_timestamp}" +"${_format}")" || return 1
|
||||
local _out
|
||||
_out="$(date -d "@${_timestamp}" +"${_format}")" || return 1
|
||||
printf "%s\n" "${_out}"
|
||||
}
|
||||
|
||||
@@ -559,7 +562,7 @@ _toSeconds_() {
|
||||
|
||||
if [[ $1 =~ [0-9]{1,2}(:|,|-|_|,| |[hHmMsS])[0-9]{1,2}(:|,|-|_|,| |[hHmMsS])[0-9]{1,2} ]]; then
|
||||
_saveIFS="${IFS}"
|
||||
IFS=":,;-_, HhMmSs" read -r h m s <<<"$1"
|
||||
IFS=":,;-_, HhMmSs" read -r _h _m _s <<<"$1"
|
||||
IFS="${_saveIFS}"
|
||||
else
|
||||
_h="$1"
|
||||
|
||||
@@ -31,7 +31,7 @@ _printAnsi_() {
|
||||
|
||||
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
|
||||
|
||||
#echo $(tr -dc '[:print:]'<<<$1)
|
||||
#printf "%s\n" "$(tr -dc '[:print:]'<<<$1)"
|
||||
printf "%s\n" "${1//$'\e'/\\e}"
|
||||
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ _createUniqueFilename_() {
|
||||
# USAGE:
|
||||
# _createUniqueFilename_ "/some/dir/file.txt" --> /some/dir/file.txt.1
|
||||
# _createUniqueFilename_ -i"/some/dir/file.txt" "-" --> /some/dir/file-1.txt
|
||||
# echo "line" > "$(_createUniqueFilename_ "/some/dir/file.txt")"
|
||||
# printf "%s" "line" > "$(_createUniqueFilename_ "/some/dir/file.txt")"
|
||||
|
||||
[[ $# -lt 1 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
|
||||
|
||||
@@ -155,7 +155,7 @@ _createUniqueFilename_() {
|
||||
_fn="${_originalFile}"
|
||||
for ((i = 0; i < _levels; i++)); do
|
||||
_ext=${_fn##*.}
|
||||
if [ $i == 0 ]; then
|
||||
if [[ $i == 0 ]]; then
|
||||
_extension=${_ext}${_extension:-}
|
||||
else
|
||||
_extension=${_ext}.${_extension:-}
|
||||
@@ -187,7 +187,7 @@ _createUniqueFilename_() {
|
||||
fi
|
||||
fi
|
||||
|
||||
echo "${_newFilename}"
|
||||
printf "%s\n" "${_newFilename}"
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -391,7 +391,7 @@ _fileExtension_() {
|
||||
_fn="$_file"
|
||||
for ((i = 0; i < _levels; i++)); do
|
||||
_ext=${_fn##*.}
|
||||
if [ $i == 0 ]; then
|
||||
if [[ $i == 0 ]]; then
|
||||
_exts=${_ext}${_exts:-}
|
||||
else
|
||||
_exts=${_ext}.${_exts:-}
|
||||
@@ -490,7 +490,7 @@ _listFiles_() {
|
||||
local _searchType="${1}"
|
||||
local _pattern="${2}"
|
||||
local _directory="${3:-.}"
|
||||
local _fileMatch e
|
||||
local _fileMatch
|
||||
declare -a _matchedFiles=()
|
||||
|
||||
case "${_searchType}" in
|
||||
@@ -661,7 +661,9 @@ _parseYAML_() {
|
||||
|
||||
local _s='[[:space:]]*'
|
||||
local _w='[a-zA-Z0-9_]*'
|
||||
local _fs="$(echo @ | tr @ '\034')"
|
||||
local _fs
|
||||
_fs="$(printf @ | tr @ '\034')"
|
||||
|
||||
sed -ne "s|^\(${_s}\)\(${_w}\)${_s}:${_s}\"\(.*\)\"${_s}\$|\1${_fs}\2${_fs}\3|p" \
|
||||
-e "s|^\(${_s}\)\(${_w}\)${_s}[:-]${_s}\(.*\)${_s}\$|\1${_fs}\2${_fs}\3|p" "${_yamlFile}" \
|
||||
| awk -F"${_fs}" '{
|
||||
@@ -693,12 +695,12 @@ _readFile_() {
|
||||
|
||||
[ ! -f "$_fileToRead" ] \
|
||||
&& {
|
||||
echo "'$c' not found"
|
||||
error "'${_fileToRead}' not found"
|
||||
return 1
|
||||
}
|
||||
|
||||
while read -r result; do
|
||||
printf "%s\n" "${result}"
|
||||
while read -r _result; do
|
||||
printf "%s\n" "${_result}"
|
||||
done <"${_fileToRead}"
|
||||
}
|
||||
|
||||
@@ -716,7 +718,7 @@ _sourceFile_() {
|
||||
local _fileToSource="$1"
|
||||
|
||||
[ ! -f "${_fileToSource}" ] && fatal "Attempted to source '${_fileToSource}'. Not found"
|
||||
|
||||
# shellcheck disable=SC1090
|
||||
if source "${_fileToSource}"; then
|
||||
return 0
|
||||
else
|
||||
|
||||
@@ -31,7 +31,8 @@ _guiInput_() {
|
||||
# https://github.com/herrbischoff/awesome-osx-command-line/blob/master/functions.md
|
||||
if _haveScriptableFinder_; then
|
||||
local _guiPrompt="${1:-Password:}"
|
||||
local _guiInput=$(
|
||||
local _guiInput
|
||||
_guiInput=$(
|
||||
osascript &>/dev/null <<GUI_INPUT_MESSAGE
|
||||
tell application "System Events"
|
||||
activate
|
||||
@@ -39,7 +40,7 @@ _guiInput_() {
|
||||
end tell
|
||||
GUI_INPUT_MESSAGE
|
||||
)
|
||||
echo -n "${_guiInput}"
|
||||
printf "%s\n" "${_guiInput}"
|
||||
else
|
||||
error "No GUI input without macOS"
|
||||
return 1
|
||||
|
||||
@@ -174,7 +174,7 @@ _execute_() {
|
||||
# -v Always print output from the execute function to STDOUT
|
||||
# -n Use NOTICE level alerting (default is INFO)
|
||||
# -p Pass a failed command with 'return 0'. This effectively bypasses set -e.
|
||||
# -e Bypass _alert_ functions and use 'echo RESULT'
|
||||
# -e Bypass _alert_ functions and use 'printf RESULT'
|
||||
# -s Use '_alert_ success' for successful output. (default is 'info')
|
||||
# -q Do not print output (QUIET mode)
|
||||
# OUTS:
|
||||
@@ -312,7 +312,7 @@ _findBaseDir_() {
|
||||
_source="$(readlink "${_source}")"
|
||||
[[ ${_source} != /* ]] && _source="${_dir}/${_source}" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
|
||||
done
|
||||
printf "%s/n" "$(cd -P "$(dirname "${_source}")" && pwd)"
|
||||
printf "%s\n" "$(cd -P "$(dirname "${_source}")" && pwd)"
|
||||
}
|
||||
|
||||
_generateUUID_() {
|
||||
@@ -370,8 +370,8 @@ _makeProgressBar_() {
|
||||
[[ $# == 0 ]] && return # Do nothing if no arguments are passed
|
||||
(${QUIET}) && return
|
||||
(${VERBOSE}) && return
|
||||
[ ! -t 1 ] && return # Do nothing if the output is not a terminal
|
||||
[ ${1} == 1 ] && return # Do nothing with a single element
|
||||
[ ! -t 1 ] && return # Do nothing if the output is not a terminal
|
||||
[[ ${1} == 1 ]] && return # Do nothing with a single element
|
||||
|
||||
local n="${1}"
|
||||
local _width=30
|
||||
@@ -390,7 +390,7 @@ _makeProgressBar_() {
|
||||
tput civis # Hide the cursor
|
||||
trap 'tput cnorm; exit 1' SIGINT
|
||||
|
||||
if [ ! "${progressBarProgress}" -eq $n ]; then
|
||||
if [[ ! ${progressBarProgress} -eq $n ]]; then
|
||||
#echo "progressBarProgress: $progressBarProgress"
|
||||
# Compute the percentage.
|
||||
_percentage=$((progressBarProgress * 100 / $1))
|
||||
@@ -455,7 +455,7 @@ _seekConfirmation_() {
|
||||
# 0 if answer is "yes"
|
||||
# 1 if answer is "no"
|
||||
# USAGE:
|
||||
# _seekConfirmation_ "Do something?" && echo "okay" || echo "not okay"
|
||||
# _seekConfirmation_ "Do something?" && printf "okay" || printf "not okay"
|
||||
# OR
|
||||
# if _seekConfirmation_ "Answer this question"; then
|
||||
# something
|
||||
@@ -467,7 +467,7 @@ _seekConfirmation_() {
|
||||
input "${1}"
|
||||
if "${FORCE}"; then
|
||||
debug "Forcing confirmation with '--force' flag set"
|
||||
echo -e ""
|
||||
printf "%s\n" " "
|
||||
return 0
|
||||
else
|
||||
while true; do
|
||||
|
||||
@@ -37,8 +37,9 @@ _httpStatus_() {
|
||||
local _status
|
||||
|
||||
# __________ get the CODE which is numeric:
|
||||
# shellcheck disable=SC1083
|
||||
_code=$(curl --write-out %{http_code} --silent --connect-timeout "${_timeout}" \
|
||||
--no-keepalive ${_curlops} --output /dev/null ${_url})
|
||||
--no-keepalive "${_curlops}" --output /dev/null "${_url}")
|
||||
|
||||
# __________ get the STATUS (from code) which is human interpretable:
|
||||
case $_code in
|
||||
@@ -88,11 +89,11 @@ _httpStatus_() {
|
||||
esac
|
||||
|
||||
case ${_flag} in
|
||||
--status) echo "${_code} ${_status}" ;;
|
||||
-s) echo "${_code} ${_status}" ;;
|
||||
--code) echo "${_code}" ;;
|
||||
-c) echo "${_code}" ;;
|
||||
*) echo " _httpStatus_: bad flag" && _safeExit_ ;;
|
||||
--status) printf "%s %s\n" "${_code}" "${_status}" ;;
|
||||
-s) printf "%s %s\n" "${_code}" "${_status}" ;;
|
||||
--code) printf "%s\n" "${_code}" ;;
|
||||
-c) printf "%s\n" "${_code}" ;;
|
||||
*) printf "%s\n" "_httpStatus_: bad flag" && _safeExit_ ;;
|
||||
esac
|
||||
|
||||
IFS="${_saveIFS}"
|
||||
@@ -117,6 +118,7 @@ _pushover_() {
|
||||
[[ $# -lt 4 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
|
||||
|
||||
local _pushoverURL="https://api.pushover.net/1/messages.json"
|
||||
local _messageTitle="${1}"
|
||||
local _message="${2}"
|
||||
local _apiKey="${3}"
|
||||
local _userKey="${4}"
|
||||
|
||||
@@ -65,36 +65,36 @@ _cleanString_() {
|
||||
IFS=',' read -r -a _arrayToClean <<<"${_userChars}"
|
||||
|
||||
# trim trailing/leading white space and duplicate spaces/tabs
|
||||
_string="$(echo "${_string}" | awk '{$1=$1};1')"
|
||||
_string="$(printf "%s" "${_string}" | awk '{$1=$1};1')"
|
||||
|
||||
local i
|
||||
for i in "${_arrayToClean[@]}"; do
|
||||
debug "cleaning: $i"
|
||||
_string="$(echo "${_string}" | sed "s/$i//g")"
|
||||
_string="$(printf "%s" "${_string}" | sed "s/$i//g")"
|
||||
done
|
||||
|
||||
("${_lc}") \
|
||||
&& _string="$(echo "${_string}" | tr '[:upper:]' '[:lower:]')"
|
||||
&& _string="$(printf "%s" "${_string}" | tr '[:upper:]' '[:lower:]')"
|
||||
|
||||
("${_uc}") \
|
||||
&& _string="$(echo "${_string}" | tr '[:lower:]' '[:upper:]')"
|
||||
&& _string="$(printf "%s" "${_string}" | tr '[:lower:]' '[:upper:]')"
|
||||
|
||||
if "${_alphanumeric}" && "${_us}"; then
|
||||
_string="$(echo "${_string}" | tr -c '[:alnum:]_ -' ' ')"
|
||||
_string="$(printf "%s" "${_string}" | tr -c '[:alnum:]_ -' ' ')"
|
||||
elif "${_alphanumeric}"; then
|
||||
_string="$(echo "${_string}" | sed "s/[^a-zA-Z0-9_ \-]//g")"
|
||||
_string="$(printf "%s" "${_string}" | sed "s/[^a-zA-Z0-9_ \-]//g")"
|
||||
fi
|
||||
|
||||
if "${_replace}"; then
|
||||
_string="$(echo "${_string}" | sed -E "s/${_pairs[0]}/${_pairs[1]}/g")"
|
||||
_string="$(printf "%s" "${_string}" | sed -E "s/${_pairs[0]}/${_pairs[1]}/g")"
|
||||
fi
|
||||
|
||||
# trim trailing/leading white space and duplicate dashes & spaces
|
||||
_string="$(echo "${_string}" | tr -s '-' | tr -s '_')"
|
||||
_string="$(echo "${_string}" | sed -E 's/([_\-]) /\1/g' | sed -E 's/ ([_\-])/\1/g')"
|
||||
_string="$(echo "${_string}" | awk '{$1=$1};1')"
|
||||
_string="$(printf "%s" "${_string}" | tr -s '-' | tr -s '_')"
|
||||
_string="$(printf "%s" "${_string}" | sed -E 's/([_\-]) /\1/g' | sed -E 's/ ([_\-])/\1/g')"
|
||||
_string="$(printf "%s" "${_string}" | awk '{$1=$1};1')"
|
||||
|
||||
echo "${_string}"
|
||||
printf "%s\n" "${_string}"
|
||||
|
||||
}
|
||||
|
||||
@@ -158,7 +158,7 @@ _encodeHTML_() {
|
||||
_sedFile="${HOME}/.sed/htmlEncode.sed"
|
||||
|
||||
[ -f "${_sedFile}" ] \
|
||||
&& { echo "${1}" | sed -f "${_sedFile}"; } \
|
||||
&& { printf "%s" "${1}" | sed -f "${_sedFile}"; } \
|
||||
|| return 1
|
||||
}
|
||||
|
||||
@@ -181,7 +181,7 @@ _encodeURL_() {
|
||||
|
||||
for ((i = 0; i < ${#1}; i++)); do
|
||||
if [[ ${1:i:1} =~ ^[a-zA-Z0-9\.\~_-]$ ]]; then
|
||||
printf "${1:i:1}"
|
||||
printf "%s" "${1:i:1}"
|
||||
else
|
||||
printf '%%%02X' "'${1:i:1}"
|
||||
fi
|
||||
@@ -212,7 +212,7 @@ _lower_() {
|
||||
# None
|
||||
# USAGE:
|
||||
# text=$(_lower_ <<<"$1")
|
||||
# echo "STRING" | _lower_
|
||||
# printf "STRING" | _lower_
|
||||
tr '[:upper:]' '[:lower:]'
|
||||
}
|
||||
|
||||
@@ -225,7 +225,7 @@ _ltrim_() {
|
||||
# None
|
||||
# USAGE:
|
||||
# text=$(_ltrim_ <<<"$1")
|
||||
# echo "STRING" | _ltrim_
|
||||
# printf "STRING" | _ltrim_
|
||||
local _char=${1:-[:space:]}
|
||||
sed "s%^[${_char//%/\\%}]*%%"
|
||||
}
|
||||
@@ -285,7 +285,7 @@ _rtrim_() {
|
||||
# None
|
||||
# USAGE:
|
||||
# text=$(_rtrim_ <<<"$1")
|
||||
# echo "STRING" | _rtrim_
|
||||
# printf "STRING" | _rtrim_
|
||||
local _char=${1:-[:space:]}
|
||||
sed "s%[${_char//%/\\%}]*$%%"
|
||||
}
|
||||
@@ -414,7 +414,7 @@ _stripStopwords_() {
|
||||
local _w
|
||||
|
||||
if [ -f "${_sedFile}" ]; then
|
||||
_string="$(echo "${_string}" | sed -f "${_sedFile}")"
|
||||
_string="$(printf "%s" "${_string}" | sed -f "${_sedFile}")"
|
||||
else
|
||||
fatal "_stripStopwords_: Missing sedfile expected at: ${_sedFile}"
|
||||
fi
|
||||
@@ -424,12 +424,12 @@ _stripStopwords_() {
|
||||
|
||||
if [[ ${#_localStopWords[@]} -gt 0 ]]; then
|
||||
for _w in "${_localStopWords[@]}"; do
|
||||
_string="$(echo "${_string}" | sed -E "s/\b${_w}\b//gI")"
|
||||
_string="$(printf "%s" "${_string}" | sed -E "s/\b${_w}\b//gI")"
|
||||
done
|
||||
fi
|
||||
|
||||
# Remove double spaces and trim left/right
|
||||
_string="$(echo "${_string}" | sed -E 's/[ ]{2,}/ /g' | _trim_)"
|
||||
_string="$(printf "%s" "${_string}" | sed -E 's/[ ]{2,}/ /g' | _trim_)"
|
||||
|
||||
printf "%s\n" "${_string}"
|
||||
|
||||
@@ -472,7 +472,7 @@ _trim_() {
|
||||
# stdout: Prints string with leading/trailing whitespace removed
|
||||
# USAGE:
|
||||
# text=$(_trim_ <<<"$1")
|
||||
# echo "STRING" | _trim_
|
||||
# printf "%s" "STRING" | _trim_
|
||||
# NOTE:
|
||||
# Used through a pipe or here string.
|
||||
|
||||
@@ -488,6 +488,6 @@ _upper_() {
|
||||
# None
|
||||
# USAGE:
|
||||
# text=$(_upper_ <<<"$1")
|
||||
# echo "STRING" | _upper_
|
||||
# printf "%s" "STRING" | _upper_
|
||||
tr '[:lower:]' '[:upper:]'
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
# Functions required to allow the script template and alert functions to be used
|
||||
# shellcheck disable=SC2154
|
||||
|
||||
_acquireScriptLock_() {
|
||||
# DESC:
|
||||
@@ -69,7 +70,7 @@ _safeExit_() {
|
||||
if command rm -rf "${SCRIPT_LOCK}"; then
|
||||
debug "Removing script lock"
|
||||
else
|
||||
warning "Script lock could not be removed. Try manually deleting ${yellow}'${LOCK_DIR}'"
|
||||
warning "Script lock could not be removed. Try manually deleting ${tan}'${LOCK_DIR}'"
|
||||
fi
|
||||
fi
|
||||
|
||||
@@ -83,7 +84,7 @@ _safeExit_() {
|
||||
fi
|
||||
|
||||
trap - INT TERM EXIT
|
||||
exit ${1:-0}
|
||||
exit "${1:-0}"
|
||||
}
|
||||
|
||||
_setPATH_() {
|
||||
@@ -101,7 +102,7 @@ _setPATH_() {
|
||||
|
||||
for _newPath in "$@"; do
|
||||
if [ -d "${_newPath}" ]; then
|
||||
if ! echo "${PATH}" | grep -Eq "(^|:)${_newPath}($|:)"; then
|
||||
if ! printf "%s" "${PATH}" | grep -Eq "(^|:)${_newPath}($|:)"; then
|
||||
if PATH="${_newPath}:${PATH}"; then
|
||||
debug "Added '${_newPath}' to PATH"
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user