Improve shellcheck compatibility

This commit is contained in:
Nathaniel Landau
2021-10-28 17:59:34 -04:00
parent f44ea3d2e6
commit 86300b68c4
15 changed files with 286 additions and 180 deletions

View File

@@ -21,7 +21,8 @@ _mainScript_() {
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}" [[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local _gitDiffTmp="${TMP_DIR}/diff.txt" local _gitDiffTmp
_gitDiffTmp="${TMP_DIR}/diff.txt"
if [ -f "${STOP_WORD_FILE}" ]; then if [ -f "${STOP_WORD_FILE}" ]; then
@@ -32,7 +33,7 @@ _mainScript_() {
debug "$(basename "${1}"): Checking for stop words..." debug "$(basename "${1}"): Checking for stop words..."
# remove blank lines from stopwords file # remove blank lines from stopwords file
cat "${STOP_WORD_FILE}" | sed '/^$/d' >"${TMP_DIR}/pattern_file.txt" sed '/^$/d' "${STOP_WORD_FILE}" >"${TMP_DIR}/pattern_file.txt"
# Add diff to a temporary file # Add diff to a temporary file
git diff --cached -- "${1}" | grep '^+' >"${_gitDiffTmp}" git diff --cached -- "${1}" | grep '^+' >"${_gitDiffTmp}"
@@ -60,9 +61,11 @@ _mainScript_() {
# USAGE: # USAGE:
# _ignoreSymlinks_ # _ignoreSymlinks_
local _gitIgnore="${GITROOT}/.gitignore" local _gitIgnore
local _haveSymlink=false local _haveSymlink
local _f local _f
_gitIgnore="${GITROOT}/.gitignore"
_haveSymlink=false
debug "Checking for symlinks..." debug "Checking for symlinks..."
@@ -81,7 +84,7 @@ _mainScript_() {
done done
# Work on files that were mistakenly staged # Work on files that were mistakenly staged
for f in $(git status --porcelain | grep '^A' | sed 's/^A //'); do for _f in $(git status --porcelain | grep '^A' | sed 's/^A //'); do
if [ -L "${_f}" ]; then if [ -L "${_f}" ]; then
if ! grep "${_f}" "${_gitIgnore}"; then if ! grep "${_f}" "${_gitIgnore}"; then
if printf "\n%s" "${_f}" >>"${_gitIgnore}"; then if printf "\n%s" "${_f}" >>"${_gitIgnore}"; then
@@ -115,7 +118,8 @@ _mainScript_() {
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}" [[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local _filename="$(_fileName_ "${1}")" local _filename
_filename="$(_fileName_ "${1}")"
if command -v yaml-lint >/dev/null; then if command -v yaml-lint >/dev/null; then
debug "${_filename}: Linting YAML..." debug "${_filename}: Linting YAML..."
@@ -159,15 +163,23 @@ _mainScript_() {
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}" [[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local _filename="$(_fileName_ "${1}")" local _filename
_filename="$(_fileName_ "${1}")"
if command -v shellcheck >/dev/null; then if command -v shellcheck >/dev/null; then
debug "${_filename}: Linting shellscript..." debug "${_filename}: Linting shellscript..."
if shellcheck --exclude=2016,2059,2001,2002,2148,1090,2162,2005,2034,2154,2086,2155,2181,2164,2120,2119,1083,1117,2207,1091 "${1}"; then if [[ ${_filename} == "*.j2" ]]; then
if shellcheck -x --exclude=1009,1054,1056,1072,1073,1083,2001,2148 "${1}"; then
return 0 return 0
else else
return 1 return 1
fi fi
else
if shellcheck -x --exclude=2001,2148 "${1}"; then
return 0
else
return 1
fi
fi
else else
notice "Shellcheck not installed. Continuing..." notice "Shellcheck not installed. Continuing..."
return 0 return 0
@@ -187,9 +199,11 @@ _mainScript_() {
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}" [[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local _filename="$(_fileName_ "${1}")" local _filename
_filename="$(_fileName_ "${1}")"
debug "${_filename}: Runing bats tests..." debug "${_filename}: Runing bats tests..."
if bats -t $1; then if bats -t "$1"; then
return 0 return 0
else else
return 1 return 1
@@ -198,7 +212,7 @@ _mainScript_() {
_lintAnsible_() { _lintAnsible_() {
# DESC: # DESC:
# Lint Ansible YMAL files staged for commit. Requires ansible-lint to be installed. # Lint Ansible YAML files staged for commit. Requires ansible-lint to be installed.
# ARGS: # ARGS:
# $1 (Required): Path to file # $1 (Required): Path to file
# OUTS: # OUTS:
@@ -209,7 +223,8 @@ _mainScript_() {
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}" [[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local _filename="$(_fileName_ "${1}")" local _filename
_filename="$(_fileName_ "${1}")"
if ! command -v ansible-lint &>/dev/null; then if ! command -v ansible-lint &>/dev/null; then
notice "ansible-lint not intstalled. Continuing..." notice "ansible-lint not intstalled. Continuing..."
@@ -409,7 +424,7 @@ _setColors_() {
# OUTS: # OUTS:
# None # None
# USAGE: # USAGE:
# echo "${blue}Some text${reset}" # printf "%s\n" "${blue}Some text${reset}"
if tput setaf 1 >/dev/null 2>&1; then if tput setaf 1 >/dev/null 2>&1; then
bold=$(tput bold) bold=$(tput bold)
@@ -440,6 +455,7 @@ _setColors_() {
bold="\033[4;37m" bold="\033[4;37m"
reset="\033[0m" reset="\033[0m"
underline="\033[4;37m" underline="\033[4;37m"
# shellcheck disable=SC2034
reverse="" reverse=""
white="\033[0;37m" white="\033[0;37m"
blue="\033[0;34m" blue="\033[0;34m"
@@ -497,9 +513,9 @@ _alert_() {
_color="${purple}" _color="${purple}"
elif [ "${_alertType}" == "header" ]; then elif [ "${_alertType}" == "header" ]; then
_color="${bold}${white}${underline}" _color="${bold}${white}${underline}"
elif [ ${_alertType} == "notice" ]; then elif [ "${_alertType}" == "notice" ]; then
_color="${bold}" _color="${bold}"
elif [ ${_alertType} == "input" ]; then elif [ "${_alertType}" == "input" ]; then
_color="${bold}${underline}" _color="${bold}${underline}"
elif [ "${_alertType}" = "dryrun" ]; then elif [ "${_alertType}" = "dryrun" ]; then
_color="${blue}" _color="${blue}"
@@ -534,9 +550,10 @@ _alert_() {
[[ ! -f ${LOGFILE} ]] && touch "${LOGFILE}" [[ ! -f ${LOGFILE} ]] && touch "${LOGFILE}"
# Don't use colors in logs # 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 # 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 # Write specified log level data to logfile
@@ -611,7 +628,7 @@ _printFuncStack_() {
_funcStackResponse=() _funcStackResponse=()
for ((_i = 1; _i < ${#BASH_SOURCE[@]}; _i++)); do 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 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 done
printf "( " printf "( "
printf %s "${_funcStackResponse[0]}" printf %s "${_funcStackResponse[0]}"
@@ -645,7 +662,7 @@ _safeExit_() {
fi fi
trap - INT TERM EXIT trap - INT TERM EXIT
exit ${1:-0} exit "${1:-0}"
} }
_trapCleanup_() { _trapCleanup_() {
@@ -659,7 +676,7 @@ _trapCleanup_() {
# $5: Scriptname # $5: Scriptname
# $6: $BASH_SOURCE # $6: $BASH_SOURCE
# USAGE: # USAGE:
# trap '_trapCleanup_ ${LINENO} ${BASH_LINENO} "${BASH_COMMAND}" "${FUNCNAME[*]}" "${0}" "${BASH_SOURCE[0]}"' EXIT INT TERM SIGINT SIGQUIT SIGTERM # trap '_trapCleanup_ ${LINENO} ${BASH_LINENO} "${BASH_COMMAND}" "${FUNCNAME[*]}" "${0}" "${BASH_SOURCE[0]}"' EXIT INT TERM SIGINT SIGQUIT SIGTERM ERR
# OUTS: # OUTS:
# Exits script with error code 1 # Exits script with error code 1
@@ -671,7 +688,9 @@ _trapCleanup_() {
local _sourced="${6:-}" local _sourced="${6:-}"
if [[ "$(declare -f "fatal")" && "$(declare -f "_printFuncStack_")" ]]; then if [[ "$(declare -f "fatal")" && "$(declare -f "_printFuncStack_")" ]]; then
_funcstack="'$(echo "${_funcstack}" | sed -E 's/ / < /g')'"
_funcstack="'$(printf "%s" "${_funcstack}" | sed -E 's/ / < /g')'"
if [[ ${_script##*/} == "${_sourced##*/}" ]]; then if [[ ${_script##*/} == "${_sourced##*/}" ]]; then
fatal "${7:-} command: '${_command}' (line: ${_line}) [func: $(_printFuncStack_)]" fatal "${7:-} command: '${_command}' (line: ${_line}) [func: $(_printFuncStack_)]"
else else
@@ -760,7 +779,7 @@ _setPATH_() {
for _newPath in "$@"; do for _newPath in "$@"; do
if [ -d "${_newPath}" ]; then if [ -d "${_newPath}" ]; then
if ! echo "${PATH}" | grep -Eq "(^|:)${_newPath}($|:)"; then if ! printf "%s" "${PATH}" | grep -Eq "(^|:)${_newPath}($|:)"; then
if PATH="${_newPath}:${PATH}"; then if PATH="${_newPath}:${PATH}"; then
debug "Added '${_newPath}' to PATH" debug "Added '${_newPath}' to PATH"
else else
@@ -850,6 +869,7 @@ _parseOptions_() {
unset _options unset _options
# Read the options and set stuff # Read the options and set stuff
# shellcheck disable=SC2034
while [[ ${1:-} == -?* ]]; do while [[ ${1:-} == -?* ]]; do
case $1 in case $1 in
# Custom options # Custom options

View File

@@ -77,7 +77,7 @@ IFS=$' \n\t'
# set -o xtrace # set -o xtrace
# Source utility functions # Source utility functions
_sourceUtilities_ "${HOME}/repos/shell-scripting-templates/utilities" _sourceUtilities_
# Initialize color constants # Initialize color constants
_setColors_ _setColors_
@@ -117,11 +117,11 @@ Within the `utilities` folder are many BASH functions meant to ease development
#### 1. Copy and paste into standaloneTemplate.sh #### 1. Copy and paste into standaloneTemplate.sh
You can copy any complete function from the Utilities and place it into your script. Copy it beneath the `### Custom utility functions` line. You can copy any complete function from the Utilities and place it into your script. Copy it beneath the `### Custom utility functions` line. Scripts created this way are fully portable among systems
#### 2. Source all the utility files by using template.sh #### 2. Source all the utility files by using template.sh
`template.sh` contains a function to source all the utility files into the script. Beware, this will require a full path to the location of this repository and will result in a script that will not be portable to other systems. `template.sh` contains a function to source all the utility files into the script. Beware, that you'll need to update the paths within the `_sourceUtilities_` function to ensure your script can find this repository.
## alerts.bash ## alerts.bash
@@ -304,14 +304,14 @@ Functions required to allow the script template and alert functions to be used
# Coding conventions # Coding conventions
Where possible, I follow [defensive BASH programming](https://kfirlavi.herokuapp.com/blog/2012/11/14/defensive-bash-programming/) principles.
- Function names use camel case surrounded by underscores: `_nameOfFunction_` - Function names use camel case surrounded by underscores: `_nameOfFunction_`
- Local variable names use camel case with a starting underscore: `_localVariable` - Local variable names use camel case with a starting underscore: `_localVariable`
- Global variables are in ALL_CAPS with underscores seperating words - Global variables are in ALL_CAPS with underscores seperating words
- Exceptions to the variable an function naming rules are made for alerting functions and colors to ease my speed of programming. (Breaking years of habits is hard...) I.e. `notice "Some log item: ${blue}blue text${reset}` Where `notice` is a function and `$blue` and `$reset` are global variables but are lowercase. - Exceptions to the variable an function naming rules are made for alerting functions and colors to ease my speed of programming. (Breaking years of habits is hard...) I.e. `notice "Some log item: ${blue}blue text${reset}` Where `notice` is a function and `$blue` and `$reset` are global variables but are lowercase.
- Variables are always surrounded by quotes and brackets `"${1}"` (It's verbose, but a safe practice) - Variables are always surrounded by quotes and brackets `"${1}"` (Overly verbose true, but a safe practice)
- Formatting is provided by [shfmt](https://github.com/mvdan/sh) using 4 spaces for indentation - Formatting is provided by [shfmt](https://github.com/mvdan/sh) using 4 spaces for indentation
- All scripts and functions are fully [Shellcheck](https://github.com/koalaman/shellcheck) compliant
- Where possible, I follow [defensive BASH programming](https://kfirlavi.herokuapp.com/blog/2012/11/14/defensive-bash-programming/) principles.
## A Note on Code Reuse and Prior Art ## A Note on Code Reuse and Prior Art

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# shellcheck source-path=SCRIPTDIR/../shell-scripting-templates/utilities
_mainScript_() { _mainScript_() {
# Replace everything in _mainScript_() with your script's code # Replace everything in _mainScript_() with your script's code
@@ -52,7 +53,9 @@ _trapCleanup_() {
local _sourced="${6:-}" local _sourced="${6:-}"
if [[ "$(declare -f "fatal")" && "$(declare -f "_printFuncStack_")" ]]; then if [[ "$(declare -f "fatal")" && "$(declare -f "_printFuncStack_")" ]]; then
_funcstack="'$(echo "${_funcstack}" | sed -E 's/ / < /g')'"
_funcstack="'$(printf "%s" "${_funcstack}" | sed -E 's/ / < /g')'"
if [[ ${_script##*/} == "${_sourced##*/}" ]]; then if [[ ${_script##*/} == "${_sourced##*/}" ]]; then
fatal "${7:-} command: '${_command}' (line: ${_line}) [func: $(_printFuncStack_)]" fatal "${7:-} command: '${_command}' (line: ${_line}) [func: $(_printFuncStack_)]"
else else
@@ -69,58 +72,123 @@ _trapCleanup_() {
fi fi
} }
_findBaseDir_() {
# DESC:
# Locates the real directory of the script being run. Similar to GNU readlink -n
# ARGS:
# None
# OUTS:
# stdout: prints result
# USAGE:
# baseDir="$(_findBaseDir_)"
# cp "$(_findBaseDir_ "somefile.txt")" "other_file.txt"
local _source
local _dir
# Is file sourced?
[[ $_ != "$0" ]] \
&& _source="${BASH_SOURCE[1]}" \
|| _source="${BASH_SOURCE[0]}"
while [ -h "${_source}" ]; do # Resolve $SOURCE until the file is no longer a symlink
_dir="$(cd -P "$(dirname "${_source}")" && pwd)"
_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)"
}
_sourceUtilities_() { _sourceUtilities_() {
# DESC: # DESC:
# Sources bash utility functions # Sources utility functions. Absolute paths are required for shellcheck to correctly
# parse the sourced files
# ARGS: # ARGS:
# $1 (required): Directories or files containing utility functions # NONE
# OUTS: # OUTS:
# 0 if success # 0: Success
# 1 if failure # 1: Failure
# USAGE: # USAGE:
# _sourceHelperFiles_ "/path/to/dir" "path/to/file.sh" # _sourceUtilities_
local _filesSourced=true local _utilsPath
_utilsPath="$(_findBaseDir_)/../shell-scripting-templates/utilities/"
[[ $# == 0 ]] && _filesSourced=false if [ -f "${_utilsPath}/alerts.bash" ]; then
source "${_utilsPath}/alerts.bash"
local _fileToSource
local _location
if [ ${_filesSourced} == true ]; then
for _location in "$@"; do
if [[ -d ${_location} ]]; then
for _fileToSource in "${_location}"/*.{sh,bash}; do
if [[ -f ${_fileToSource} ]]; then
if ! source "${_fileToSource}"; then
_filesSourced=false
break 2
fi
else
_filesSourced=false
break 2
fi
done
elif [[ -f ${_location} ]] && [[ ${_location} =~ .*\.(sh|bash)$ ]]; then
if ! source "${_fileToSource}"; then
_filesSourced=false
break
fi
else
_filesSourced=false
break
fi
done
fi
if [ ${_filesSourced} == true ]; then
return 0
else
printf "%s\n" "ERROR: Invalid argument to ${FUNCNAME[0]}: ${_location}"
if [ "$(declare -f "_safeExit_")" ]; then
_safeExit_ 1
else else
printf "%s\n" "ERROR: alerts.bash not found"
exit 1 exit 1
fi fi
if [ -f "${_utilsPath}/arrays.bash" ]; then
source "${_utilsPath}/arrays.bash"
else
printf "%s\n" "ERROR: arrays.bash not found"
exit 1
fi
if [ -f "${_utilsPath}/checks.bash" ]; then
source "${_utilsPath}/checks.bash"
else
printf "%s\n" "ERROR: checks.bash not found"
exit 1
fi
if [ -f "${_utilsPath}/dates.bash" ]; then
source "${_utilsPath}/dates.bash"
else
printf "%s\n" "ERROR: dates.bash not found"
exit 1
fi
if [ -f "${_utilsPath}/debug.bash" ]; then
source "${_utilsPath}/debug.bash"
else
printf "%s\n" "ERROR: debug.bash not found"
exit 1
fi
if [ -f "${_utilsPath}/files.bash" ]; then
source "${_utilsPath}/files.bash"
else
printf "%s\n" "ERROR: files.bash not found"
exit 1
fi
if [ -f "${_utilsPath}/macOS.bash" ]; then
source "${_utilsPath}/macOS.bash"
else
printf "%s\n" "ERROR: macOS.bash not found"
exit 1
fi
if [ -f "${_utilsPath}/misc.bash" ]; then
source "${_utilsPath}/misc.bash"
else
printf "%s\n" "ERROR: misc.bash not found"
exit 1
fi
if [ -f "${_utilsPath}/services.bash" ]; then
source "${_utilsPath}/services.bash"
else
printf "%s\n" "ERROR: services.bash not found"
exit 1
fi
if [ -f "${_utilsPath}/strings.bash" ]; then
source "${_utilsPath}/strings.bash"
else
printf "%s\n" "ERROR: strings.bash not found"
exit 1
fi
if [ -f "${_utilsPath}/template_utils.bash" ]; then
source "${_utilsPath}/template_utils.bash"
else
printf "%s\n" "ERROR: template_utils.bash not found"
exit 1
fi fi
} }
@@ -170,6 +238,7 @@ _parseOptions_() {
unset _options unset _options
# Read the options and set stuff # Read the options and set stuff
# shellcheck disable=SC2034
while [[ ${1:-} == -?* ]]; do while [[ ${1:-} == -?* ]]; do
case $1 in case $1 in
# Custom options # Custom options
@@ -268,7 +337,7 @@ IFS=$' \n\t'
# set -o xtrace # set -o xtrace
# Source utility functions # Source utility functions
_sourceUtilities_ "${HOME}/repos/shell-scripting-templates/utilities" _sourceUtilities_
# Initialize color constants # Initialize color constants
_setColors_ _setColors_

View File

@@ -40,7 +40,7 @@ _setColors_() {
# OUTS: # OUTS:
# None # None
# USAGE: # USAGE:
# echo "${blue}Some text${reset}" # printf "%s\n" "${blue}Some text${reset}"
if tput setaf 1 >/dev/null 2>&1; then if tput setaf 1 >/dev/null 2>&1; then
bold=$(tput bold) bold=$(tput bold)
@@ -71,6 +71,7 @@ _setColors_() {
bold="\033[4;37m" bold="\033[4;37m"
reset="\033[0m" reset="\033[0m"
underline="\033[4;37m" underline="\033[4;37m"
# shellcheck disable=SC2034
reverse="" reverse=""
white="\033[0;37m" white="\033[0;37m"
blue="\033[0;34m" blue="\033[0;34m"
@@ -128,9 +129,9 @@ _alert_() {
_color="${purple}" _color="${purple}"
elif [ "${_alertType}" == "header" ]; then elif [ "${_alertType}" == "header" ]; then
_color="${bold}${white}${underline}" _color="${bold}${white}${underline}"
elif [ ${_alertType} == "notice" ]; then elif [ "${_alertType}" == "notice" ]; then
_color="${bold}" _color="${bold}"
elif [ ${_alertType} == "input" ]; then elif [ "${_alertType}" == "input" ]; then
_color="${bold}${underline}" _color="${bold}${underline}"
elif [ "${_alertType}" = "dryrun" ]; then elif [ "${_alertType}" = "dryrun" ]; then
_color="${blue}" _color="${blue}"
@@ -165,9 +166,10 @@ _alert_() {
[[ ! -f ${LOGFILE} ]] && touch "${LOGFILE}" [[ ! -f ${LOGFILE} ]] && touch "${LOGFILE}"
# Don't use colors in logs # 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 # 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 # Write specified log level data to logfile
@@ -242,7 +244,7 @@ _printFuncStack_() {
_funcStackResponse=() _funcStackResponse=()
for ((_i = 1; _i < ${#BASH_SOURCE[@]}; _i++)); do 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 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 done
printf "( " printf "( "
printf %s "${_funcStackResponse[0]}" printf %s "${_funcStackResponse[0]}"
@@ -276,7 +278,7 @@ _safeExit_() {
fi fi
trap - INT TERM EXIT trap - INT TERM EXIT
exit ${1:-0} exit "${1:-0}"
} }
_trapCleanup_() { _trapCleanup_() {
@@ -290,7 +292,7 @@ _trapCleanup_() {
# $5: Scriptname # $5: Scriptname
# $6: $BASH_SOURCE # $6: $BASH_SOURCE
# USAGE: # USAGE:
# trap '_trapCleanup_ ${LINENO} ${BASH_LINENO} "${BASH_COMMAND}" "${FUNCNAME[*]}" "${0}" "${BASH_SOURCE[0]}"' EXIT INT TERM SIGINT SIGQUIT SIGTERM # trap '_trapCleanup_ ${LINENO} ${BASH_LINENO} "${BASH_COMMAND}" "${FUNCNAME[*]}" "${0}" "${BASH_SOURCE[0]}"' EXIT INT TERM SIGINT SIGQUIT SIGTERM ERR
# OUTS: # OUTS:
# Exits script with error code 1 # Exits script with error code 1
@@ -302,7 +304,9 @@ _trapCleanup_() {
local _sourced="${6:-}" local _sourced="${6:-}"
if [[ "$(declare -f "fatal")" && "$(declare -f "_printFuncStack_")" ]]; then if [[ "$(declare -f "fatal")" && "$(declare -f "_printFuncStack_")" ]]; then
_funcstack="'$(echo "${_funcstack}" | sed -E 's/ / < /g')'"
_funcstack="'$(printf "%s" "${_funcstack}" | sed -E 's/ / < /g')'"
if [[ ${_script##*/} == "${_sourced##*/}" ]]; then if [[ ${_script##*/} == "${_sourced##*/}" ]]; then
fatal "${7:-} command: '${_command}' (line: ${_line}) [func: $(_printFuncStack_)]" fatal "${7:-} command: '${_command}' (line: ${_line}) [func: $(_printFuncStack_)]"
else else
@@ -391,7 +395,7 @@ _setPATH_() {
for _newPath in "$@"; do for _newPath in "$@"; do
if [ -d "${_newPath}" ]; then if [ -d "${_newPath}" ]; then
if ! echo "${PATH}" | grep -Eq "(^|:)${_newPath}($|:)"; then if ! printf "%s" "${PATH}" | grep -Eq "(^|:)${_newPath}($|:)"; then
if PATH="${_newPath}:${PATH}"; then if PATH="${_newPath}:${PATH}"; then
debug "Added '${_newPath}' to PATH" debug "Added '${_newPath}' to PATH"
else else
@@ -481,6 +485,7 @@ _parseOptions_() {
unset _options unset _options
# Read the options and set stuff # Read the options and set stuff
# shellcheck disable=SC2034
while [[ ${1:-} == -?* ]]; do while [[ ${1:-} == -?* ]]; do
case $1 in case $1 in
# Custom options # Custom options

View File

@@ -1,4 +1,5 @@
# Functions for providing alerts to the user and logging them # Functions for providing alerts to the user and logging them
# shellcheck disable=SC2034,SC2154
_setColors_() { _setColors_() {
# DESC: # DESC:
@@ -8,7 +9,7 @@ _setColors_() {
# OUTS: # OUTS:
# None # None
# USAGE: # USAGE:
# echo "${blue}Some text${reset}" # printf "%s\n" "${blue}Some text${reset}"
if tput setaf 1 >/dev/null 2>&1; then if tput setaf 1 >/dev/null 2>&1; then
bold=$(tput bold) bold=$(tput bold)
@@ -39,6 +40,7 @@ _setColors_() {
bold="\033[4;37m" bold="\033[4;37m"
reset="\033[0m" reset="\033[0m"
underline="\033[4;37m" underline="\033[4;37m"
# shellcheck disable=SC2034
reverse="" reverse=""
white="\033[0;37m" white="\033[0;37m"
blue="\033[0;34m" blue="\033[0;34m"
@@ -96,9 +98,9 @@ _alert_() {
_color="${purple}" _color="${purple}"
elif [ "${_alertType}" == "header" ]; then elif [ "${_alertType}" == "header" ]; then
_color="${bold}${white}${underline}" _color="${bold}${white}${underline}"
elif [ ${_alertType} == "notice" ]; then elif [ "${_alertType}" == "notice" ]; then
_color="${bold}" _color="${bold}"
elif [ ${_alertType} == "input" ]; then elif [ "${_alertType}" == "input" ]; then
_color="${bold}${underline}" _color="${bold}${underline}"
elif [ "${_alertType}" = "dryrun" ]; then elif [ "${_alertType}" = "dryrun" ]; then
_color="${blue}" _color="${blue}"
@@ -133,9 +135,10 @@ _alert_() {
[[ ! -f ${LOGFILE} ]] && touch "${LOGFILE}" [[ ! -f ${LOGFILE} ]] && touch "${LOGFILE}"
# Don't use colors in logs # 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 # 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 # Write specified log level data to logfile
@@ -210,7 +213,7 @@ _printFuncStack_() {
_funcStackResponse=() _funcStackResponse=()
for ((_i = 1; _i < ${#BASH_SOURCE[@]}; _i++)); do 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 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 done
printf "( " printf "( "
printf %s "${_funcStackResponse[0]}" printf %s "${_funcStackResponse[0]}"

View File

@@ -190,7 +190,7 @@ _forEachReject_() {
fi fi
declare -i _ret=$? declare -i _ret=$?
if [[ ${_ret} -ne 0 ]]; then if [[ ${_ret} -ne 0 ]]; then
echo "${_it}" printf "%s\n" "${_it}"
fi fi
done done
} }
@@ -211,7 +211,7 @@ _forEachSome_() {
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}" [[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local _func="${1}" local _func="${1}"
local _IFS=$'\n' local IFS=$'\n'
while read -r _it; do while read -r _it; do
if [[ ${_func} == *"$"* ]]; then if [[ ${_func} == *"$"* ]]; then

View File

@@ -135,7 +135,7 @@ _isFQDN_() {
local _input="${1}" 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 return 0
else else
return 1 return 1
@@ -218,7 +218,7 @@ _isDir_() {
# 1 - Input is not a directory # 1 - Input is not a directory
# USAGE: # USAGE:
# _varIsDir_ "${var}" # _varIsDir_ "${var}"
# (_isDir_ "${var}") && echo "Is a directory" || echo "Not a directory" # (_isDir_ "${var}") && printf "Is a directory" || printf "Not a directory"
# NOTES: # NOTES:
# #
@@ -274,7 +274,7 @@ _rootAvailable_() {
# https://github.com/ralish/bash-script-template # https://github.com/ralish/bash-script-template
local _superuser local _superuser
local _testEUID
if [[ ${EUID} -eq 0 ]]; then if [[ ${EUID} -eq 0 ]]; then
_superuser=true _superuser=true
elif [[ -z ${1:-} ]]; then elif [[ -z ${1:-} ]]; then

View File

@@ -46,7 +46,7 @@ _countdown_() {
else else
echo "${_message} ${ii}" echo "${_message} ${ii}"
fi fi
sleep ${_sleepTime} sleep "${_sleepTime}"
done done
} }
@@ -120,7 +120,7 @@ _fromSeconds_() {
((_h = ${1} / 3600)) ((_h = ${1} / 3600))
((_m = (${1} % 3600) / 60)) ((_m = (${1} % 3600) / 60))
((_s = ${1} % 60)) ((_s = ${1} % 60))
printf "%02d:%02d:%02d\n" ${_h} ${_m} ${_s} printf "%02d:%02d:%02d\n" "${_h}" "${_m}" "${_s}"
} }
_monthToNumber_() { _monthToNumber_() {
@@ -135,7 +135,9 @@ _monthToNumber_() {
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}" [[ $# == 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 case "${_mon}" in
january | jan | ja) echo 1 ;; january | jan | ja) echo 1 ;;
february | feb | fe) echo 2 ;; february | feb | fe) echo 2 ;;
@@ -232,7 +234,7 @@ _parseDate_() {
trap "$(shopt -p nocasematch)" RETURN # reset nocasematch when function exits trap "$(shopt -p nocasematch)" RETURN # reset nocasematch when function exits
shopt -s nocasematch # Use case-insensitive regex shopt -s nocasematch # Use case-insensitive regex
debug "_parseDate_() input ${tan}${_stringToTest}${purple}" debug "_parseDate_() input: ${_stringToTest}"
# YYYY MM DD or YYYY-MM-DD # 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].*|$)" _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=$((10#${BASH_REMATCH[4]}))
PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")" PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")"
PARSE_DATE_DAY=$((10#${BASH_REMATCH[5]})) PARSE_DATE_DAY=$((10#${BASH_REMATCH[5]}))
debug "regex match: ${tan}YYYY-MM-DD${purple}" debug "regex match: YYYY-MM-DD "
# Month DD, YYYY # 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 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_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_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH:-}")"
PARSE_DATE_DAY=$((10#${BASH_REMATCH[3]:-})) PARSE_DATE_DAY=$((10#${BASH_REMATCH[3]:-}))
PARSE_DATE_YEAR=$((10#${BASH_REMATCH[5]:-})) PARSE_DATE_YEAR=$((10#${BASH_REMATCH[5]:-}))
debug "regex match: ${tan}Month DD, YYYY${purple}" debug "regex match: Month DD, YYYY"
# Month DD, YY # 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 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_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_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")"
PARSE_DATE_DAY=$((10#${BASH_REMATCH[3]})) PARSE_DATE_DAY=$((10#${BASH_REMATCH[3]}))
PARSE_DATE_YEAR="20$((10#${BASH_REMATCH[5]}))" 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 # 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 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="$(_monthToNumber_ "${BASH_REMATCH[4]}")"
PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")" PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")"
PARSE_DATE_YEAR=$((10#"${BASH_REMATCH[5]}")) 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 # 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 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=$((10#${BASH_REMATCH[3]}))
PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")" PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")"
PARSE_DATE_DAY=$((10#${BASH_REMATCH[4]})) 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 && elif [[ $((10#${BASH_REMATCH[3]})) -gt 12 &&
$((10#${BASH_REMATCH[3]})) -lt 32 && $((10#${BASH_REMATCH[3]})) -lt 32 &&
$((10#${BASH_REMATCH[4]})) -lt 13 ]] \ $((10#${BASH_REMATCH[4]})) -lt 13 ]] \
@@ -293,7 +295,7 @@ _parseDate_() {
PARSE_DATE_MONTH=$((10#${BASH_REMATCH[4]})) PARSE_DATE_MONTH=$((10#${BASH_REMATCH[4]}))
PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")" PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")"
PARSE_DATE_DAY=$((10#${BASH_REMATCH[3]})) 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 && elif [[ $((10#${BASH_REMATCH[3]})) -lt 32 &&
$((10#${BASH_REMATCH[4]})) -lt 13 ]] \ $((10#${BASH_REMATCH[4]})) -lt 13 ]] \
; then ; then
@@ -302,7 +304,7 @@ _parseDate_() {
PARSE_DATE_MONTH=$((10#${BASH_REMATCH[3]})) PARSE_DATE_MONTH=$((10#${BASH_REMATCH[3]}))
PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")" PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")"
PARSE_DATE_DAY=$((10#${BASH_REMATCH[4]})) PARSE_DATE_DAY=$((10#${BASH_REMATCH[4]}))
debug "regex match: ${tan}MM-DD-YYYY${purple}" debug "regex match: MM-DD-YYYY"
else else
shopt -u nocasematch shopt -u nocasematch
return 1 return 1
@@ -319,7 +321,7 @@ _parseDate_() {
PARSE_DATE_MONTH=$((10#${BASH_REMATCH[3]})) PARSE_DATE_MONTH=$((10#${BASH_REMATCH[3]}))
PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")" PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")"
PARSE_DATE_DAY=$((10#${BASH_REMATCH[4]})) 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 && elif [[ $((10#${BASH_REMATCH[3]})) -gt 12 &&
$((10#${BASH_REMATCH[3]})) -lt 32 && $((10#${BASH_REMATCH[3]})) -lt 32 &&
$((10#${BASH_REMATCH[4]})) -lt 13 ]] \ $((10#${BASH_REMATCH[4]})) -lt 13 ]] \
@@ -329,7 +331,7 @@ _parseDate_() {
PARSE_DATE_MONTH=$((10#${BASH_REMATCH[4]})) PARSE_DATE_MONTH=$((10#${BASH_REMATCH[4]}))
PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")" PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")"
PARSE_DATE_DAY=$((10#${BASH_REMATCH[3]})) 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 && elif [[ $((10#${BASH_REMATCH[3]})) -lt 32 &&
$((10#${BASH_REMATCH[4]})) -lt 13 ]] \ $((10#${BASH_REMATCH[4]})) -lt 13 ]] \
; then ; then
@@ -338,7 +340,7 @@ _parseDate_() {
PARSE_DATE_MONTH=$((10#${BASH_REMATCH[3]})) PARSE_DATE_MONTH=$((10#${BASH_REMATCH[3]}))
PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")" PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")"
PARSE_DATE_DAY=$((10#${BASH_REMATCH[4]})) PARSE_DATE_DAY=$((10#${BASH_REMATCH[4]}))
debug "regex match: ${tan}MM-DD-YYYY${purple}" debug "regex match: MM-DD-YYYY"
else else
shopt -u nocasematch shopt -u nocasematch
return 1 return 1
@@ -349,9 +351,9 @@ _parseDate_() {
PARSE_DATE_FOUND="${BASH_REMATCH[1]}" PARSE_DATE_FOUND="${BASH_REMATCH[1]}"
PARSE_DATE_DAY="1" PARSE_DATE_DAY="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_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")"
PARSE_DATE_YEAR="$((10#${BASH_REMATCH[3]}))" PARSE_DATE_YEAR="$((10#${BASH_REMATCH[3]}))"
debug "regex match: ${tan}Month, YYYY${purple}" debug "regex match: Month, YYYY"
# YYYYMMDDHHMM # 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 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_YEAR="$((10#${BASH_REMATCH[3]}))"
PARSE_DATE_HOUR="$((10#${BASH_REMATCH[6]}))" PARSE_DATE_HOUR="$((10#${BASH_REMATCH[6]}))"
PARSE_DATE_MINUTE="$((10#${BASH_REMATCH[7]}))" PARSE_DATE_MINUTE="$((10#${BASH_REMATCH[7]}))"
debug "regex match: ${tan}YYYYMMDDHHMM${purple}" debug "regex match: YYYYMMDDHHMM"
# YYYYMMDDHH 1 2 3 4 5 6 # 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 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_YEAR="$((10#${BASH_REMATCH[3]}))"
PARSE_DATE_HOUR="${BASH_REMATCH[6]}" PARSE_DATE_HOUR="${BASH_REMATCH[6]}"
PARSE_DATE_MINUTE="00" PARSE_DATE_MINUTE="00"
debug "regex match: ${tan}YYYYMMDDHHMM${purple}" debug "regex match: YYYYMMDDHHMM"
# MMDDYYYY or YYYYMMDD or DDMMYYYY # MMDDYYYY or YYYYMMDD or DDMMYYYY
# 1 2 3 4 5 6 # 1 2 3 4 5 6
@@ -389,7 +391,7 @@ _parseDate_() {
PARSE_DATE_MONTH="$((10#${BASH_REMATCH[3]}))" PARSE_DATE_MONTH="$((10#${BASH_REMATCH[3]}))"
PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")" PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")"
PARSE_DATE_YEAR="${BASH_REMATCH[5]}${BASH_REMATCH[6]}" PARSE_DATE_YEAR="${BASH_REMATCH[5]}${BASH_REMATCH[6]}"
debug "regex match: ${tan}MMDDYYYY${purple}" debug "regex match: MMDDYYYY"
# DDMMYYYY # DDMMYYYY
elif [[ $((10#${BASH_REMATCH[5]})) -eq 20 && elif [[ $((10#${BASH_REMATCH[5]})) -eq 20 &&
$((10#${BASH_REMATCH[3]})) -gt 12 && $((10#${BASH_REMATCH[3]})) -gt 12 &&
@@ -401,7 +403,7 @@ _parseDate_() {
PARSE_DATE_MONTH="$((10#${BASH_REMATCH[4]}))" PARSE_DATE_MONTH="$((10#${BASH_REMATCH[4]}))"
PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")" PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")"
PARSE_DATE_YEAR="${BASH_REMATCH[5]}${BASH_REMATCH[6]}" PARSE_DATE_YEAR="${BASH_REMATCH[5]}${BASH_REMATCH[6]}"
debug "regex match: ${tan}DDMMYYYY${purple}" debug "regex match: DDMMYYYY"
# YYYYMMDD # YYYYMMDD
elif [[ $((10#${BASH_REMATCH[3]})) -eq 20 && elif [[ $((10#${BASH_REMATCH[3]})) -eq 20 &&
$((10#${BASH_REMATCH[6]})) -gt 12 && $((10#${BASH_REMATCH[6]})) -gt 12 &&
@@ -413,7 +415,7 @@ _parseDate_() {
PARSE_DATE_MONTH="$((10#${BASH_REMATCH[5]}))" PARSE_DATE_MONTH="$((10#${BASH_REMATCH[5]}))"
PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")" PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")"
PARSE_DATE_YEAR="${BASH_REMATCH[3]}${BASH_REMATCH[4]}" PARSE_DATE_YEAR="${BASH_REMATCH[3]}${BASH_REMATCH[4]}"
debug "regex match: ${tan}YYYYMMDD${purple}" debug "regex match: YYYYMMDD"
# YYYYDDMM # YYYYDDMM
elif [[ $((10#${BASH_REMATCH[3]})) -eq 20 && elif [[ $((10#${BASH_REMATCH[3]})) -eq 20 &&
$((10#${BASH_REMATCH[5]})) -gt 12 && $((10#${BASH_REMATCH[5]})) -gt 12 &&
@@ -425,7 +427,7 @@ _parseDate_() {
PARSE_DATE_MONTH="$((10#${BASH_REMATCH[6]}))" PARSE_DATE_MONTH="$((10#${BASH_REMATCH[6]}))"
PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")" PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")"
PARSE_DATE_YEAR="${BASH_REMATCH[3]}${BASH_REMATCH[4]}" PARSE_DATE_YEAR="${BASH_REMATCH[3]}${BASH_REMATCH[4]}"
debug "regex match: ${tan}YYYYMMDD${purple}" debug "regex match: YYYYMMDD"
# Assume YYYMMDD # Assume YYYMMDD
elif [[ $((10#${BASH_REMATCH[3]})) -eq 20 && elif [[ $((10#${BASH_REMATCH[3]})) -eq 20 &&
$((10#${BASH_REMATCH[6]})) -lt 32 && $((10#${BASH_REMATCH[6]})) -lt 32 &&
@@ -436,7 +438,7 @@ _parseDate_() {
PARSE_DATE_MONTH="$((10#${BASH_REMATCH[5]}))" PARSE_DATE_MONTH="$((10#${BASH_REMATCH[5]}))"
PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")" PARSE_DATE_MONTH_NAME="$(_numberToMonth_ "${PARSE_DATE_MONTH}")"
PARSE_DATE_YEAR="${BASH_REMATCH[3]}${BASH_REMATCH[4]}" PARSE_DATE_YEAR="${BASH_REMATCH[3]}${BASH_REMATCH[4]}"
debug "regex match: ${tan}YYYYMMDD${purple}" debug "regex match: YYYYMMDD"
else else
shopt -u nocasematch shopt -u nocasematch
return 1 return 1
@@ -485,13 +487,13 @@ _parseDate_() {
return 1 return 1
} }
debug "${tan}\$PARSE_DATE_FOUND: ${PARSE_DATE_FOUND}${purple}" debug "\$PARSE_DATE_FOUND: ${PARSE_DATE_FOUND}"
debug "${tan}\$PARSE_DATE_YEAR: ${PARSE_DATE_YEAR}${purple}" debug "\$PARSE_DATE_YEAR: ${PARSE_DATE_YEAR}"
debug "${tan}\$PARSE_DATE_MONTH: ${PARSE_DATE_MONTH}${purple}" debug "\$PARSE_DATE_MONTH: ${PARSE_DATE_MONTH}"
debug "${tan}\$PARSE_DATE_MONTH_NAME: ${PARSE_DATE_MONTH_NAME}${purple}" debug "\$PARSE_DATE_MONTH_NAME: ${PARSE_DATE_MONTH_NAME}"
debug "${tan}\$PARSE_DATE_DAY: ${PARSE_DATE_DAY}${purple}" debug "\$PARSE_DATE_DAY: ${PARSE_DATE_DAY}"
[[ -z ${PARSE_DATE_HOUR:-} ]] || debug "${tan}\$PARSE_DATE_HOUR: ${PARSE_DATE_HOUR}${purple}" [[ -z ${PARSE_DATE_HOUR:-} ]] || debug "\$PARSE_DATE_HOUR: ${PARSE_DATE_HOUR}"
[[ -z ${PARSE_DATE_MINUTE:-} ]] || debug "${tan}\$PARSE_DATE_MINUTE: ${PARSE_DATE_MINUTE}${purple}" [[ -z ${PARSE_DATE_MINUTE:-} ]] || debug "\$PARSE_DATE_MINUTE: ${PARSE_DATE_MINUTE}"
shopt -u nocasematch shopt -u nocasematch
@@ -527,7 +529,8 @@ _readableUnixTimestamp_() {
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}" [[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local _timestamp="${1}" local _timestamp="${1}"
local _format="${2:-"%F %T"}" 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}" 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 if [[ $1 =~ [0-9]{1,2}(:|,|-|_|,| |[hHmMsS])[0-9]{1,2}(:|,|-|_|,| |[hHmMsS])[0-9]{1,2} ]]; then
_saveIFS="${IFS}" _saveIFS="${IFS}"
IFS=":,;-_, HhMmSs" read -r h m s <<<"$1" IFS=":,;-_, HhMmSs" read -r _h _m _s <<<"$1"
IFS="${_saveIFS}" IFS="${_saveIFS}"
else else
_h="$1" _h="$1"

View File

@@ -31,7 +31,7 @@ _printAnsi_() {
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}" [[ $# == 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}" printf "%s\n" "${1//$'\e'/\\e}"
} }

View File

@@ -99,7 +99,7 @@ _createUniqueFilename_() {
# USAGE: # USAGE:
# _createUniqueFilename_ "/some/dir/file.txt" --> /some/dir/file.txt.1 # _createUniqueFilename_ "/some/dir/file.txt" --> /some/dir/file.txt.1
# _createUniqueFilename_ -i"/some/dir/file.txt" "-" --> /some/dir/file-1.txt # _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]}" [[ $# -lt 1 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
@@ -155,7 +155,7 @@ _createUniqueFilename_() {
_fn="${_originalFile}" _fn="${_originalFile}"
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
_extension=${_ext}${_extension:-} _extension=${_ext}${_extension:-}
else else
_extension=${_ext}.${_extension:-} _extension=${_ext}.${_extension:-}
@@ -187,7 +187,7 @@ _createUniqueFilename_() {
fi fi
fi fi
echo "${_newFilename}" printf "%s\n" "${_newFilename}"
return 0 return 0
} }
@@ -391,7 +391,7 @@ _fileExtension_() {
_fn="$_file" _fn="$_file"
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:-}
@@ -490,7 +490,7 @@ _listFiles_() {
local _searchType="${1}" local _searchType="${1}"
local _pattern="${2}" local _pattern="${2}"
local _directory="${3:-.}" local _directory="${3:-.}"
local _fileMatch e local _fileMatch
declare -a _matchedFiles=() declare -a _matchedFiles=()
case "${_searchType}" in case "${_searchType}" in
@@ -661,7 +661,9 @@ _parseYAML_() {
local _s='[[:space:]]*' local _s='[[:space:]]*'
local _w='[a-zA-Z0-9_]*' 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" \ 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}" \ -e "s|^\(${_s}\)\(${_w}\)${_s}[:-]${_s}\(.*\)${_s}\$|\1${_fs}\2${_fs}\3|p" "${_yamlFile}" \
| awk -F"${_fs}" '{ | awk -F"${_fs}" '{
@@ -693,12 +695,12 @@ _readFile_() {
[ ! -f "$_fileToRead" ] \ [ ! -f "$_fileToRead" ] \
&& { && {
echo "'$c' not found" error "'${_fileToRead}' not found"
return 1 return 1
} }
while read -r result; do while read -r _result; do
printf "%s\n" "${result}" printf "%s\n" "${_result}"
done <"${_fileToRead}" done <"${_fileToRead}"
} }
@@ -716,7 +718,7 @@ _sourceFile_() {
local _fileToSource="$1" local _fileToSource="$1"
[ ! -f "${_fileToSource}" ] && fatal "Attempted to source '${_fileToSource}'. Not found" [ ! -f "${_fileToSource}" ] && fatal "Attempted to source '${_fileToSource}'. Not found"
# shellcheck disable=SC1090
if source "${_fileToSource}"; then if source "${_fileToSource}"; then
return 0 return 0
else else

View File

@@ -31,7 +31,8 @@ _guiInput_() {
# https://github.com/herrbischoff/awesome-osx-command-line/blob/master/functions.md # https://github.com/herrbischoff/awesome-osx-command-line/blob/master/functions.md
if _haveScriptableFinder_; then if _haveScriptableFinder_; then
local _guiPrompt="${1:-Password:}" local _guiPrompt="${1:-Password:}"
local _guiInput=$( local _guiInput
_guiInput=$(
osascript &>/dev/null <<GUI_INPUT_MESSAGE osascript &>/dev/null <<GUI_INPUT_MESSAGE
tell application "System Events" tell application "System Events"
activate activate
@@ -39,7 +40,7 @@ _guiInput_() {
end tell end tell
GUI_INPUT_MESSAGE GUI_INPUT_MESSAGE
) )
echo -n "${_guiInput}" printf "%s\n" "${_guiInput}"
else else
error "No GUI input without macOS" error "No GUI input without macOS"
return 1 return 1

View File

@@ -174,7 +174,7 @@ _execute_() {
# -v Always print output from the execute function to STDOUT # -v Always print output from the execute function to STDOUT
# -n Use NOTICE level alerting (default is INFO) # -n Use NOTICE level alerting (default is INFO)
# -p Pass a failed command with 'return 0'. This effectively bypasses set -e. # -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') # -s Use '_alert_ success' for successful output. (default is 'info')
# -q Do not print output (QUIET mode) # -q Do not print output (QUIET mode)
# OUTS: # OUTS:
@@ -312,7 +312,7 @@ _findBaseDir_() {
_source="$(readlink "${_source}")" _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 [[ ${_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 done
printf "%s/n" "$(cd -P "$(dirname "${_source}")" && pwd)" printf "%s\n" "$(cd -P "$(dirname "${_source}")" && pwd)"
} }
_generateUUID_() { _generateUUID_() {
@@ -371,7 +371,7 @@ _makeProgressBar_() {
(${QUIET}) && return (${QUIET}) && return
(${VERBOSE}) && return (${VERBOSE}) && return
[ ! -t 1 ] && return # Do nothing if the output is not a terminal [ ! -t 1 ] && return # Do nothing if the output is not a terminal
[ ${1} == 1 ] && return # Do nothing with a single element [[ ${1} == 1 ]] && return # Do nothing with a single element
local n="${1}" local n="${1}"
local _width=30 local _width=30
@@ -390,7 +390,7 @@ _makeProgressBar_() {
tput civis # Hide the cursor tput civis # Hide the cursor
trap 'tput cnorm; exit 1' SIGINT trap 'tput cnorm; exit 1' SIGINT
if [ ! "${progressBarProgress}" -eq $n ]; then if [[ ! ${progressBarProgress} -eq $n ]]; then
#echo "progressBarProgress: $progressBarProgress" #echo "progressBarProgress: $progressBarProgress"
# Compute the percentage. # Compute the percentage.
_percentage=$((progressBarProgress * 100 / $1)) _percentage=$((progressBarProgress * 100 / $1))
@@ -455,7 +455,7 @@ _seekConfirmation_() {
# 0 if answer is "yes" # 0 if answer is "yes"
# 1 if answer is "no" # 1 if answer is "no"
# USAGE: # USAGE:
# _seekConfirmation_ "Do something?" && echo "okay" || echo "not okay" # _seekConfirmation_ "Do something?" && printf "okay" || printf "not okay"
# OR # OR
# if _seekConfirmation_ "Answer this question"; then # if _seekConfirmation_ "Answer this question"; then
# something # something
@@ -467,7 +467,7 @@ _seekConfirmation_() {
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 "" printf "%s\n" " "
return 0 return 0
else else
while true; do while true; do

View File

@@ -37,8 +37,9 @@ _httpStatus_() {
local _status local _status
# __________ get the CODE which is numeric: # __________ get the CODE which is numeric:
# shellcheck disable=SC1083
_code=$(curl --write-out %{http_code} --silent --connect-timeout "${_timeout}" \ _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: # __________ get the STATUS (from code) which is human interpretable:
case $_code in case $_code in
@@ -88,11 +89,11 @@ _httpStatus_() {
esac esac
case ${_flag} in case ${_flag} in
--status) echo "${_code} ${_status}" ;; --status) printf "%s %s\n" "${_code}" "${_status}" ;;
-s) echo "${_code} ${_status}" ;; -s) printf "%s %s\n" "${_code}" "${_status}" ;;
--code) echo "${_code}" ;; --code) printf "%s\n" "${_code}" ;;
-c) echo "${_code}" ;; -c) printf "%s\n" "${_code}" ;;
*) echo " _httpStatus_: bad flag" && _safeExit_ ;; *) printf "%s\n" "_httpStatus_: bad flag" && _safeExit_ ;;
esac esac
IFS="${_saveIFS}" IFS="${_saveIFS}"
@@ -117,6 +118,7 @@ _pushover_() {
[[ $# -lt 4 ]] && fatal "Missing required argument to ${FUNCNAME[0]}" [[ $# -lt 4 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local _pushoverURL="https://api.pushover.net/1/messages.json" local _pushoverURL="https://api.pushover.net/1/messages.json"
local _messageTitle="${1}"
local _message="${2}" local _message="${2}"
local _apiKey="${3}" local _apiKey="${3}"
local _userKey="${4}" local _userKey="${4}"

View File

@@ -65,36 +65,36 @@ _cleanString_() {
IFS=',' read -r -a _arrayToClean <<<"${_userChars}" IFS=',' read -r -a _arrayToClean <<<"${_userChars}"
# trim trailing/leading white space and duplicate spaces/tabs # 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 local i
for i in "${_arrayToClean[@]}"; do for i in "${_arrayToClean[@]}"; do
debug "cleaning: $i" debug "cleaning: $i"
_string="$(echo "${_string}" | sed "s/$i//g")" _string="$(printf "%s" "${_string}" | sed "s/$i//g")"
done done
("${_lc}") \ ("${_lc}") \
&& _string="$(echo "${_string}" | tr '[:upper:]' '[:lower:]')" && _string="$(printf "%s" "${_string}" | tr '[:upper:]' '[:lower:]')"
("${_uc}") \ ("${_uc}") \
&& _string="$(echo "${_string}" | tr '[:lower:]' '[:upper:]')" && _string="$(printf "%s" "${_string}" | tr '[:lower:]' '[:upper:]')"
if "${_alphanumeric}" && "${_us}"; then if "${_alphanumeric}" && "${_us}"; then
_string="$(echo "${_string}" | tr -c '[:alnum:]_ -' ' ')" _string="$(printf "%s" "${_string}" | tr -c '[:alnum:]_ -' ' ')"
elif "${_alphanumeric}"; then 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 fi
if "${_replace}"; then 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 fi
# trim trailing/leading white space and duplicate dashes & spaces # trim trailing/leading white space and duplicate dashes & spaces
_string="$(echo "${_string}" | tr -s '-' | tr -s '_')" _string="$(printf "%s" "${_string}" | tr -s '-' | tr -s '_')"
_string="$(echo "${_string}" | sed -E 's/([_\-]) /\1/g' | sed -E 's/ ([_\-])/\1/g')" _string="$(printf "%s" "${_string}" | sed -E 's/([_\-]) /\1/g' | sed -E 's/ ([_\-])/\1/g')"
_string="$(echo "${_string}" | awk '{$1=$1};1')" _string="$(printf "%s" "${_string}" | awk '{$1=$1};1')"
echo "${_string}" printf "%s\n" "${_string}"
} }
@@ -158,7 +158,7 @@ _encodeHTML_() {
_sedFile="${HOME}/.sed/htmlEncode.sed" _sedFile="${HOME}/.sed/htmlEncode.sed"
[ -f "${_sedFile}" ] \ [ -f "${_sedFile}" ] \
&& { echo "${1}" | sed -f "${_sedFile}"; } \ && { printf "%s" "${1}" | sed -f "${_sedFile}"; } \
|| return 1 || return 1
} }
@@ -181,7 +181,7 @@ _encodeURL_() {
for ((i = 0; i < ${#1}; i++)); do for ((i = 0; i < ${#1}; i++)); do
if [[ ${1:i:1} =~ ^[a-zA-Z0-9\.\~_-]$ ]]; then if [[ ${1:i:1} =~ ^[a-zA-Z0-9\.\~_-]$ ]]; then
printf "${1:i:1}" printf "%s" "${1:i:1}"
else else
printf '%%%02X' "'${1:i:1}" printf '%%%02X' "'${1:i:1}"
fi fi
@@ -212,7 +212,7 @@ _lower_() {
# None # None
# USAGE: # USAGE:
# text=$(_lower_ <<<"$1") # text=$(_lower_ <<<"$1")
# echo "STRING" | _lower_ # printf "STRING" | _lower_
tr '[:upper:]' '[:lower:]' tr '[:upper:]' '[:lower:]'
} }
@@ -225,7 +225,7 @@ _ltrim_() {
# None # None
# USAGE: # USAGE:
# text=$(_ltrim_ <<<"$1") # text=$(_ltrim_ <<<"$1")
# echo "STRING" | _ltrim_ # printf "STRING" | _ltrim_
local _char=${1:-[:space:]} local _char=${1:-[:space:]}
sed "s%^[${_char//%/\\%}]*%%" sed "s%^[${_char//%/\\%}]*%%"
} }
@@ -285,7 +285,7 @@ _rtrim_() {
# None # None
# USAGE: # USAGE:
# text=$(_rtrim_ <<<"$1") # text=$(_rtrim_ <<<"$1")
# echo "STRING" | _rtrim_ # printf "STRING" | _rtrim_
local _char=${1:-[:space:]} local _char=${1:-[:space:]}
sed "s%[${_char//%/\\%}]*$%%" sed "s%[${_char//%/\\%}]*$%%"
} }
@@ -414,7 +414,7 @@ _stripStopwords_() {
local _w local _w
if [ -f "${_sedFile}" ]; then if [ -f "${_sedFile}" ]; then
_string="$(echo "${_string}" | sed -f "${_sedFile}")" _string="$(printf "%s" "${_string}" | sed -f "${_sedFile}")"
else else
fatal "_stripStopwords_: Missing sedfile expected at: ${_sedFile}" fatal "_stripStopwords_: Missing sedfile expected at: ${_sedFile}"
fi fi
@@ -424,12 +424,12 @@ _stripStopwords_() {
if [[ ${#_localStopWords[@]} -gt 0 ]]; then if [[ ${#_localStopWords[@]} -gt 0 ]]; then
for _w in "${_localStopWords[@]}"; do 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 done
fi fi
# Remove double spaces and trim left/right # 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}" printf "%s\n" "${_string}"
@@ -472,7 +472,7 @@ _trim_() {
# stdout: Prints string with leading/trailing whitespace removed # stdout: Prints string with leading/trailing whitespace removed
# USAGE: # USAGE:
# text=$(_trim_ <<<"$1") # text=$(_trim_ <<<"$1")
# echo "STRING" | _trim_ # printf "%s" "STRING" | _trim_
# NOTE: # NOTE:
# Used through a pipe or here string. # Used through a pipe or here string.
@@ -488,6 +488,6 @@ _upper_() {
# None # None
# USAGE: # USAGE:
# text=$(_upper_ <<<"$1") # text=$(_upper_ <<<"$1")
# echo "STRING" | _upper_ # printf "%s" "STRING" | _upper_
tr '[:lower:]' '[:upper:]' tr '[:lower:]' '[:upper:]'
} }

View File

@@ -1,4 +1,5 @@
# Functions required to allow the script template and alert functions to be used # Functions required to allow the script template and alert functions to be used
# shellcheck disable=SC2154
_acquireScriptLock_() { _acquireScriptLock_() {
# DESC: # DESC:
@@ -69,7 +70,7 @@ _safeExit_() {
if command rm -rf "${SCRIPT_LOCK}"; then if command rm -rf "${SCRIPT_LOCK}"; then
debug "Removing script lock" debug "Removing script lock"
else 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
fi fi
@@ -83,7 +84,7 @@ _safeExit_() {
fi fi
trap - INT TERM EXIT trap - INT TERM EXIT
exit ${1:-0} exit "${1:-0}"
} }
_setPATH_() { _setPATH_() {
@@ -101,7 +102,7 @@ _setPATH_() {
for _newPath in "$@"; do for _newPath in "$@"; do
if [ -d "${_newPath}" ]; then if [ -d "${_newPath}" ]; then
if ! echo "${PATH}" | grep -Eq "(^|:)${_newPath}($|:)"; then if ! printf "%s" "${PATH}" | grep -Eq "(^|:)${_newPath}($|:)"; then
if PATH="${_newPath}:${PATH}"; then if PATH="${_newPath}:${PATH}"; then
debug "Added '${_newPath}' to PATH" debug "Added '${_newPath}' to PATH"
else else