mirror of
https://github.com/natelandau/shell-scripting-templates.git
synced 2025-11-10 22:13:48 -05:00
Improve shellcheck compatibility
This commit is contained in:
@@ -21,7 +21,8 @@ _mainScript_() {
|
||||
|
||||
[[ $# == 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
|
||||
|
||||
@@ -32,7 +33,7 @@ _mainScript_() {
|
||||
debug "$(basename "${1}"): Checking for stop words..."
|
||||
|
||||
# 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
|
||||
git diff --cached -- "${1}" | grep '^+' >"${_gitDiffTmp}"
|
||||
@@ -60,9 +61,11 @@ _mainScript_() {
|
||||
# USAGE:
|
||||
# _ignoreSymlinks_
|
||||
|
||||
local _gitIgnore="${GITROOT}/.gitignore"
|
||||
local _haveSymlink=false
|
||||
local _gitIgnore
|
||||
local _haveSymlink
|
||||
local _f
|
||||
_gitIgnore="${GITROOT}/.gitignore"
|
||||
_haveSymlink=false
|
||||
|
||||
debug "Checking for symlinks..."
|
||||
|
||||
@@ -81,7 +84,7 @@ _mainScript_() {
|
||||
done
|
||||
|
||||
# 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 ! grep "${_f}" "${_gitIgnore}"; then
|
||||
if printf "\n%s" "${_f}" >>"${_gitIgnore}"; then
|
||||
@@ -115,7 +118,8 @@ _mainScript_() {
|
||||
|
||||
[[ $# == 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
|
||||
debug "${_filename}: Linting YAML..."
|
||||
@@ -159,14 +163,22 @@ _mainScript_() {
|
||||
|
||||
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
|
||||
|
||||
local _filename="$(_fileName_ "${1}")"
|
||||
|
||||
local _filename
|
||||
_filename="$(_fileName_ "${1}")"
|
||||
if command -v shellcheck >/dev/null; then
|
||||
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
|
||||
return 0
|
||||
if [[ ${_filename} == "*.j2" ]]; then
|
||||
if shellcheck -x --exclude=1009,1054,1056,1072,1073,1083,2001,2148 "${1}"; then
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
else
|
||||
return 1
|
||||
if shellcheck -x --exclude=2001,2148 "${1}"; then
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
else
|
||||
notice "Shellcheck not installed. Continuing..."
|
||||
@@ -187,9 +199,11 @@ _mainScript_() {
|
||||
|
||||
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
|
||||
|
||||
local _filename="$(_fileName_ "${1}")"
|
||||
local _filename
|
||||
_filename="$(_fileName_ "${1}")"
|
||||
|
||||
debug "${_filename}: Runing bats tests..."
|
||||
if bats -t $1; then
|
||||
if bats -t "$1"; then
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
@@ -198,7 +212,7 @@ _mainScript_() {
|
||||
|
||||
_lintAnsible_() {
|
||||
# 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:
|
||||
# $1 (Required): Path to file
|
||||
# OUTS:
|
||||
@@ -209,7 +223,8 @@ _mainScript_() {
|
||||
|
||||
[[ $# == 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
|
||||
notice "ansible-lint not intstalled. Continuing..."
|
||||
@@ -409,7 +424,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)
|
||||
@@ -440,6 +455,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"
|
||||
@@ -497,9 +513,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}"
|
||||
@@ -534,9 +550,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
|
||||
@@ -611,7 +628,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]}"
|
||||
@@ -645,7 +662,7 @@ _safeExit_() {
|
||||
fi
|
||||
|
||||
trap - INT TERM EXIT
|
||||
exit ${1:-0}
|
||||
exit "${1:-0}"
|
||||
}
|
||||
|
||||
_trapCleanup_() {
|
||||
@@ -659,7 +676,7 @@ _trapCleanup_() {
|
||||
# $5: Scriptname
|
||||
# $6: $BASH_SOURCE
|
||||
# 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:
|
||||
# Exits script with error code 1
|
||||
|
||||
@@ -671,7 +688,9 @@ _trapCleanup_() {
|
||||
local _sourced="${6:-}"
|
||||
|
||||
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
|
||||
fatal "${7:-} command: '${_command}' (line: ${_line}) [func: $(_printFuncStack_)]"
|
||||
else
|
||||
@@ -760,7 +779,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
|
||||
@@ -850,6 +869,7 @@ _parseOptions_() {
|
||||
unset _options
|
||||
|
||||
# Read the options and set stuff
|
||||
# shellcheck disable=SC2034
|
||||
while [[ ${1:-} == -?* ]]; do
|
||||
case $1 in
|
||||
# Custom options
|
||||
|
||||
12
README.md
12
README.md
@@ -77,7 +77,7 @@ IFS=$' \n\t'
|
||||
# set -o xtrace
|
||||
|
||||
# Source utility functions
|
||||
_sourceUtilities_ "${HOME}/repos/shell-scripting-templates/utilities"
|
||||
_sourceUtilities_
|
||||
|
||||
# Initialize color constants
|
||||
_setColors_
|
||||
@@ -117,11 +117,11 @@ Within the `utilities` folder are many BASH functions meant to ease development
|
||||
|
||||
#### 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
|
||||
|
||||
`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
|
||||
|
||||
@@ -304,14 +304,14 @@ Functions required to allow the script template and alert functions to be used
|
||||
|
||||
# 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_`
|
||||
- Local variable names use camel case with a starting underscore: `_localVariable`
|
||||
- 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.
|
||||
- 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
|
||||
- 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
|
||||
|
||||
|
||||
157
template.sh
157
template.sh
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env bash
|
||||
# shellcheck source-path=SCRIPTDIR/../shell-scripting-templates/utilities
|
||||
|
||||
_mainScript_() {
|
||||
# Replace everything in _mainScript_() with your script's code
|
||||
@@ -52,7 +53,9 @@ _trapCleanup_() {
|
||||
local _sourced="${6:-}"
|
||||
|
||||
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
|
||||
fatal "${7:-} command: '${_command}' (line: ${_line}) [func: $(_printFuncStack_)]"
|
||||
else
|
||||
@@ -69,58 +72,123 @@ _trapCleanup_() {
|
||||
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_() {
|
||||
# DESC:
|
||||
# Sources bash utility functions
|
||||
# Sources utility functions. Absolute paths are required for shellcheck to correctly
|
||||
# parse the sourced files
|
||||
# ARGS:
|
||||
# $1 (required): Directories or files containing utility functions
|
||||
# NONE
|
||||
# OUTS:
|
||||
# 0 if success
|
||||
# 1 if failure
|
||||
# 0: Success
|
||||
# 1: Failure
|
||||
# USAGE:
|
||||
# _sourceHelperFiles_ "/path/to/dir" "path/to/file.sh"
|
||||
# _sourceUtilities_
|
||||
|
||||
local _filesSourced=true
|
||||
local _utilsPath
|
||||
_utilsPath="$(_findBaseDir_)/../shell-scripting-templates/utilities/"
|
||||
|
||||
[[ $# == 0 ]] && _filesSourced=false
|
||||
|
||||
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
|
||||
if [ -f "${_utilsPath}/alerts.bash" ]; then
|
||||
source "${_utilsPath}/alerts.bash"
|
||||
else
|
||||
printf "%s\n" "ERROR: alerts.bash not found"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ ${_filesSourced} == true ]; then
|
||||
return 0
|
||||
if [ -f "${_utilsPath}/arrays.bash" ]; then
|
||||
source "${_utilsPath}/arrays.bash"
|
||||
else
|
||||
printf "%s\n" "ERROR: Invalid argument to ${FUNCNAME[0]}: ${_location}"
|
||||
if [ "$(declare -f "_safeExit_")" ]; then
|
||||
_safeExit_ 1
|
||||
else
|
||||
exit 1
|
||||
fi
|
||||
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
|
||||
|
||||
}
|
||||
@@ -170,6 +238,7 @@ _parseOptions_() {
|
||||
unset _options
|
||||
|
||||
# Read the options and set stuff
|
||||
# shellcheck disable=SC2034
|
||||
while [[ ${1:-} == -?* ]]; do
|
||||
case $1 in
|
||||
# Custom options
|
||||
@@ -268,7 +337,7 @@ IFS=$' \n\t'
|
||||
# set -o xtrace
|
||||
|
||||
# Source utility functions
|
||||
_sourceUtilities_ "${HOME}/repos/shell-scripting-templates/utilities"
|
||||
_sourceUtilities_
|
||||
|
||||
# Initialize color constants
|
||||
_setColors_
|
||||
|
||||
@@ -40,7 +40,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)
|
||||
@@ -71,6 +71,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"
|
||||
@@ -128,9 +129,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}"
|
||||
@@ -165,9 +166,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
|
||||
@@ -242,7 +244,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]}"
|
||||
@@ -276,7 +278,7 @@ _safeExit_() {
|
||||
fi
|
||||
|
||||
trap - INT TERM EXIT
|
||||
exit ${1:-0}
|
||||
exit "${1:-0}"
|
||||
}
|
||||
|
||||
_trapCleanup_() {
|
||||
@@ -290,7 +292,7 @@ _trapCleanup_() {
|
||||
# $5: Scriptname
|
||||
# $6: $BASH_SOURCE
|
||||
# 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:
|
||||
# Exits script with error code 1
|
||||
|
||||
@@ -302,7 +304,9 @@ _trapCleanup_() {
|
||||
local _sourced="${6:-}"
|
||||
|
||||
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
|
||||
fatal "${7:-} command: '${_command}' (line: ${_line}) [func: $(_printFuncStack_)]"
|
||||
else
|
||||
@@ -391,7 +395,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
|
||||
@@ -481,6 +485,7 @@ _parseOptions_() {
|
||||
unset _options
|
||||
|
||||
# Read the options and set stuff
|
||||
# shellcheck disable=SC2034
|
||||
while [[ ${1:-} == -?* ]]; do
|
||||
case $1 in
|
||||
# Custom options
|
||||
|
||||
@@ -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