mirror of
https://github.com/natelandau/shell-scripting-templates.git
synced 2025-11-09 13:43:46 -05:00
use 4 spaces for indent
This commit is contained in:
@@ -1,139 +1,140 @@
|
|||||||
|
|
||||||
# Colors
|
# Colors
|
||||||
if tput setaf 1 &>/dev/null; then
|
if tput setaf 1 &>/dev/null; then
|
||||||
bold=$(tput bold)
|
bold=$(tput bold)
|
||||||
white=$(tput setaf 7)
|
white=$(tput setaf 7)
|
||||||
reset=$(tput sgr0)
|
reset=$(tput sgr0)
|
||||||
purple=$(tput setaf 171)
|
purple=$(tput setaf 171)
|
||||||
red=$(tput setaf 1)
|
red=$(tput setaf 1)
|
||||||
green=$(tput setaf 76)
|
green=$(tput setaf 76)
|
||||||
tan=$(tput setaf 3)
|
tan=$(tput setaf 3)
|
||||||
yellow=$(tput setaf 3)
|
yellow=$(tput setaf 3)
|
||||||
blue=$(tput setaf 38)
|
blue=$(tput setaf 38)
|
||||||
underline=$(tput sgr 0 1)
|
underline=$(tput sgr 0 1)
|
||||||
else
|
else
|
||||||
bold="\033[4;37m"
|
bold="\033[4;37m"
|
||||||
white="\033[0;37m"
|
white="\033[0;37m"
|
||||||
reset="\033[0m"
|
reset="\033[0m"
|
||||||
purple="\033[0;35m"
|
purple="\033[0;35m"
|
||||||
red="\033[0;31m"
|
red="\033[0;31m"
|
||||||
green="\033[1;32m"
|
green="\033[1;32m"
|
||||||
tan="\033[0;33m"
|
tan="\033[0;33m"
|
||||||
yellow="\033[0;33m"
|
yellow="\033[0;33m"
|
||||||
blue="\033[0;34m"
|
blue="\033[0;34m"
|
||||||
underline="\033[4;37m"
|
underline="\033[4;37m"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
_alert_() {
|
_alert_() {
|
||||||
# DESC: Controls all printing of messages to log files and stdout.
|
# DESC: Controls all printing of messages to log files and stdout.
|
||||||
# ARGS: $1 (required) - The type of alert to print
|
# ARGS: $1 (required) - The type of alert to print
|
||||||
# (success, header, notice, dryrun, debug, warning, error,
|
# (success, header, notice, dryrun, debug, warning, error,
|
||||||
# fatal, info, input)
|
# fatal, info, input)
|
||||||
# $2 (required) - The message to be printed to stdout and/or a log file
|
# $2 (required) - The message to be printed to stdout and/or a log file
|
||||||
# $3 (optional) - Pass '${LINENO}' to print the line number where the _alert_ was triggered
|
# $3 (optional) - Pass '${LINENO}' to print the line number where the _alert_ was triggered
|
||||||
# OUTS: None
|
# OUTS: None
|
||||||
# USAGE: [ALERTTYPE] "[MESSAGE]" "${LINENO}"
|
# USAGE: [ALERTTYPE] "[MESSAGE]" "${LINENO}"
|
||||||
# NOTES: The colors of each alert type are set in this function
|
# NOTES: The colors of each alert type are set in this function
|
||||||
# For specified alert types, the funcstac will be printed
|
# For specified alert types, the funcstac will be printed
|
||||||
|
|
||||||
local function_name color
|
local function_name color
|
||||||
local alertType="${1}"
|
local alertType="${1}"
|
||||||
local message="${2}"
|
local message="${2}"
|
||||||
local line="${3:-}" # Optional line number
|
local line="${3:-}" # Optional line number
|
||||||
|
|
||||||
if [[ -n "${line}" && "${alertType}" =~ ^(fatal|error) && "${FUNCNAME[2]}" != "_trapCleanup_" ]]; then
|
if [[ -n ${line} && ${alertType} =~ ^(fatal|error) && ${FUNCNAME[2]} != "_trapCleanup_" ]]; then
|
||||||
message="${message} (line: ${line}) $(_functionStack_)"
|
message="${message} (line: ${line}) $(_functionStack_)"
|
||||||
elif [[ -n "${line}" && "${FUNCNAME[2]}" != "_trapCleanup_" ]]; then
|
elif [[ -n ${line} && ${FUNCNAME[2]} != "_trapCleanup_" ]]; then
|
||||||
message="${message} (line: ${line})"
|
message="${message} (line: ${line})"
|
||||||
elif [[ -z "${line}" && "${alertType}" =~ ^(fatal|error) && "${FUNCNAME[2]}" != "_trapCleanup_" ]]; then
|
elif [[ -z ${line} && ${alertType} =~ ^(fatal|error) && ${FUNCNAME[2]} != "_trapCleanup_" ]]; then
|
||||||
message="${message} $(_functionStack_)"
|
message="${message} $(_functionStack_)"
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ "${alertType}" =~ ^(error|fatal) ]]; then
|
|
||||||
color="${bold}${red}"
|
|
||||||
elif [ "${alertType}" = "warning" ]; then
|
|
||||||
color="${red}"
|
|
||||||
elif [ "${alertType}" = "success" ]; then
|
|
||||||
color="${green}"
|
|
||||||
elif [ "${alertType}" = "debug" ]; then
|
|
||||||
color="${purple}"
|
|
||||||
elif [ "${alertType}" = "header" ]; then
|
|
||||||
color="${bold}${tan}"
|
|
||||||
elif [[ "${alertType}" =~ ^(input|notice) ]]; then
|
|
||||||
color="${bold}"
|
|
||||||
elif [ "${alertType}" = "dryrun" ]; then
|
|
||||||
color="${blue}"
|
|
||||||
else
|
|
||||||
color=""
|
|
||||||
fi
|
|
||||||
|
|
||||||
_writeToScreen_() {
|
|
||||||
|
|
||||||
("${QUIET}") && return 0 # Print to console when script is not 'quiet'
|
|
||||||
[[ ${VERBOSE} == false && "${alertType}" =~ ^(debug|verbose) ]] && return 0
|
|
||||||
|
|
||||||
if ! [[ -t 1 ]]; then # Don't use colors on non-recognized terminals
|
|
||||||
color=""
|
|
||||||
reset=""
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo -e "$(date +"%r") ${color}$(printf "[%7s]" "${alertType}") ${message}${reset}"
|
if [[ ${alertType} =~ ^(error|fatal) ]]; then
|
||||||
}
|
color="${bold}${red}"
|
||||||
_writeToScreen_
|
elif [ "${alertType}" = "warning" ]; then
|
||||||
|
color="${red}"
|
||||||
_writeToLog_() {
|
elif [ "${alertType}" = "success" ]; then
|
||||||
[[ "${alertType}" == "input" ]] && return 0
|
color="${green}"
|
||||||
[[ "${LOGLEVEL}" =~ (off|OFF|Off) ]] && return 0
|
elif [ "${alertType}" = "debug" ]; then
|
||||||
[ -z "${LOGFILE:-}" ] && fatal "\$LOGFILE must be set"
|
color="${purple}"
|
||||||
[ ! -d "$(dirname "${LOGFILE}")" ] && mkdir -p "$(dirname "${LOGFILE}")"
|
elif [ "${alertType}" = "header" ]; then
|
||||||
[[ ! -f "${LOGFILE}" ]] && touch "${LOGFILE}"
|
color="${bold}${tan}"
|
||||||
|
elif [[ ${alertType} =~ ^(input|notice) ]]; then
|
||||||
# Don't use colors in logs
|
color="${bold}"
|
||||||
if command -v gsed &>/dev/null; then
|
elif [ "${alertType}" = "dryrun" ]; then
|
||||||
local cleanmessage="$(echo "${message}" | gsed -E 's/(\x1b)?\[(([0-9]{1,2})(;[0-9]{1,3}){0,2})?[mGK]//g')"
|
color="${blue}"
|
||||||
else
|
else
|
||||||
local cleanmessage="$(echo "${message}" | sed -E 's/(\x1b)?\[(([0-9]{1,2})(;[0-9]{1,3}){0,2})?[mGK]//g')"
|
color=""
|
||||||
fi
|
fi
|
||||||
echo -e "$(date +"%b %d %R:%S") $(printf "[%7s]" "${alertType}") [$(/bin/hostname)] ${cleanmessage}" >>"${LOGFILE}"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Write specified log level data to LOGFILE
|
_writeToScreen_() {
|
||||||
case "${LOGLEVEL:-ERROR}" in
|
|
||||||
ALL|all|All)
|
("${QUIET}") && return 0 # Print to console when script is not 'quiet'
|
||||||
_writeToLog_
|
[[ ${VERBOSE} == false && ${alertType} =~ ^(debug|verbose) ]] && return 0
|
||||||
;;
|
|
||||||
DEBUG|debug|Debug)
|
if ! [[ -t 1 ]]; then # Don't use colors on non-recognized terminals
|
||||||
_writeToLog_
|
color=""
|
||||||
;;
|
reset=""
|
||||||
INFO|info|Info)
|
fi
|
||||||
if [[ "${alertType}" =~ ^(die|error|fatal|warning|info|notice|success) ]]; then
|
|
||||||
_writeToLog_
|
echo -e "$(date +"%r") ${color}$(printf "[%7s]" "${alertType}") ${message}${reset}"
|
||||||
fi
|
}
|
||||||
;;
|
_writeToScreen_
|
||||||
WARN|warn|Warn)
|
|
||||||
if [[ "${alertType}" =~ ^(die|error|fatal|warning) ]]; then
|
_writeToLog_() {
|
||||||
_writeToLog_
|
[[ ${alertType} == "input" ]] && return 0
|
||||||
fi
|
[[ ${LOGLEVEL} =~ (off|OFF|Off) ]] && return 0
|
||||||
;;
|
if [ -z "${LOGFILE:-}" ]; then
|
||||||
ERROR|error|Error)
|
LOGFILE="$(pwd)/$(basename "$0").log"
|
||||||
if [[ "${alertType}" =~ ^(die|error|fatal) ]]; then
|
fi
|
||||||
_writeToLog_
|
[ ! -d "$(dirname "${LOGFILE}")" ] && mkdir -p "$(dirname "${LOGFILE}")"
|
||||||
fi
|
[[ ! -f ${LOGFILE} ]] && touch "${LOGFILE}"
|
||||||
;;
|
|
||||||
FATAL|fatal|Fatal)
|
# Don't use colors in logs
|
||||||
if [[ "${alertType}" =~ ^(die|fatal) ]]; then
|
if command -v gsed &>/dev/null; then
|
||||||
_writeToLog_
|
local cleanmessage="$(echo "${message}" | gsed -E 's/(\x1b)?\[(([0-9]{1,2})(;[0-9]{1,3}){0,2})?[mGK]//g')"
|
||||||
fi
|
else
|
||||||
;;
|
local cleanmessage="$(echo "${message}" | sed -E 's/(\x1b)?\[(([0-9]{1,2})(;[0-9]{1,3}){0,2})?[mGK]//g')"
|
||||||
OFF|off)
|
fi
|
||||||
return 0
|
echo -e "$(date +"%b %d %R:%S") $(printf "[%7s]" "${alertType}") [$(/bin/hostname)] ${cleanmessage}" >>"${LOGFILE}"
|
||||||
;;
|
}
|
||||||
*)
|
|
||||||
if [[ "${alertType}" =~ ^(die|error|fatal) ]]; then
|
# Write specified log level data to logfile
|
||||||
_writeToLog_
|
case "${LOGLEVEL:-ERROR}" in
|
||||||
fi
|
ALL | all | All)
|
||||||
;;
|
_writeToLog_
|
||||||
esac
|
;;
|
||||||
|
DEBUG | debug | Debug)
|
||||||
|
_writeToLog_
|
||||||
|
;;
|
||||||
|
INFO | info | Info)
|
||||||
|
if [[ ${alertType} =~ ^(die|error|fatal|warning|info|notice|success) ]]; then
|
||||||
|
_writeToLog_
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
WARN | warn | Warn)
|
||||||
|
if [[ ${alertType} =~ ^(die|error|fatal|warning) ]]; then
|
||||||
|
_writeToLog_
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
ERROR | error | Error)
|
||||||
|
if [[ ${alertType} =~ ^(die|error|fatal) ]]; then
|
||||||
|
_writeToLog_
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
FATAL | fatal | Fatal)
|
||||||
|
if [[ ${alertType} =~ ^(die|fatal) ]]; then
|
||||||
|
_writeToLog_
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
OFF | off)
|
||||||
|
return 0
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
if [[ ${alertType} =~ ^(die|error|fatal) ]]; then
|
||||||
|
_writeToLog_
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
} # /_alert_
|
} # /_alert_
|
||||||
|
|
||||||
@@ -145,24 +146,30 @@ success() { _alert_ success "${1}" "${2:-}"; }
|
|||||||
dryrun() { _alert_ dryrun "${1}" "${2:-}"; }
|
dryrun() { _alert_ dryrun "${1}" "${2:-}"; }
|
||||||
input() { _alert_ input "${1}" "${2:-}"; }
|
input() { _alert_ input "${1}" "${2:-}"; }
|
||||||
header() { _alert_ header "== ${1} ==" "${2:-}"; }
|
header() { _alert_ header "== ${1} ==" "${2:-}"; }
|
||||||
die() { _alert_ fatal "${1}" "${2:-}"; _safeExit_ "1" ; }
|
die() {
|
||||||
fatal() { _alert_ fatal "${1}" "${2:-}"; _safeExit_ "1" ; }
|
_alert_ fatal "${1}" "${2:-}"
|
||||||
|
_safeExit_ "1"
|
||||||
|
}
|
||||||
|
fatal() {
|
||||||
|
_alert_ fatal "${1}" "${2:-}"
|
||||||
|
_safeExit_ "1"
|
||||||
|
}
|
||||||
debug() { _alert_ debug "${1}" "${2:-}"; }
|
debug() { _alert_ debug "${1}" "${2:-}"; }
|
||||||
verbose() { _alert_ debug "${1}" "${2:-}"; }
|
verbose() { _alert_ debug "${1}" "${2:-}"; }
|
||||||
|
|
||||||
_functionStack_() {
|
_functionStack_() {
|
||||||
# DESC: Prints the function stack in use
|
# DESC: Prints the function stack in use
|
||||||
# ARGS: None
|
# ARGS: None
|
||||||
# OUTS: Prints [function]:[file]:[line]
|
# OUTS: Prints [function]:[file]:[line]
|
||||||
# NOTE: Does not print functions from the alert class
|
# NOTE: Does not print functions from the alert class
|
||||||
local _i
|
local _i
|
||||||
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 | verbose | debug | die) continue ;; esac
|
case "${FUNCNAME[$_i]}" in "_alert_" | "_trapCleanup_" | fatal | error | warning | notice | info | verbose | debug | dryrun | header | success | die) 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]}"
|
||||||
printf ' < %s' "${funcStackResponse[@]:1}"
|
printf ' < %s' "${funcStackResponse[@]:1}"
|
||||||
printf ' )\n'
|
printf ' )\n'
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,83 +1,82 @@
|
|||||||
|
|
||||||
_inArray_() {
|
_inArray_() {
|
||||||
# DESC: Determine if a value is in an array
|
# DESC: Determine if a value is in an array
|
||||||
# ARGS: $1 (Required) - Value to search for
|
# ARGS: $1 (Required) - Value to search for
|
||||||
# $2 (Required) - Array written as ${ARRAY[@]}
|
# $2 (Required) - Array written as ${ARRAY[@]}
|
||||||
# OUTS: true/false
|
# OUTS: true/false
|
||||||
# USAGE: if _inArray_ "VALUE" "${ARRAY[@]}"; then ...
|
# USAGE: if _inArray_ "VALUE" "${ARRAY[@]}"; then ...
|
||||||
|
|
||||||
[[ $# -lt 2 ]] && fatal 'Missing required argument to _inArray_()!'
|
[[ $# -lt 2 ]] && fatal 'Missing required argument to _inArray_()!'
|
||||||
|
|
||||||
local value="$1"
|
local value="$1"
|
||||||
shift
|
shift
|
||||||
for arrayItem in "$@"; do
|
for arrayItem in "$@"; do
|
||||||
[[ "${arrayItem}" == "${value}" ]] && return 0
|
[[ ${arrayItem} == "${value}" ]] && return 0
|
||||||
done
|
done
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
_join_() {
|
_join_() {
|
||||||
# DESC: Joins items together with a user specified separator
|
# DESC: Joins items together with a user specified separator
|
||||||
# ARGS: $1 (Required) - Separator
|
# ARGS: $1 (Required) - Separator
|
||||||
# $@ (Required) - Items to be joined
|
# $@ (Required) - Items to be joined
|
||||||
# OUTS: Prints joined terms
|
# OUTS: Prints joined terms
|
||||||
# USAGE:
|
# USAGE:
|
||||||
# _join_ , a "b c" d #a,b c,d
|
# _join_ , a "b c" d #a,b c,d
|
||||||
# _join_ / var local tmp #var/local/tmp
|
# _join_ / var local tmp #var/local/tmp
|
||||||
# _join_ , "${foo[@]}" #a,b,c
|
# _join_ , "${foo[@]}" #a,b,c
|
||||||
# NOTE: http://stackoverflow.com/questions/1527049/bash-join-elements-of-an-array
|
# NOTE: http://stackoverflow.com/questions/1527049/bash-join-elements-of-an-array
|
||||||
|
|
||||||
[[ $# -lt 2 ]] && fatal 'Missing required argument to _join_()!'
|
[[ $# -lt 2 ]] && fatal 'Missing required argument to _join_()!'
|
||||||
|
|
||||||
local IFS="${1}"
|
local IFS="${1}"
|
||||||
shift
|
shift
|
||||||
echo "${*}"
|
echo "${*}"
|
||||||
}
|
}
|
||||||
|
|
||||||
_setdiff_() {
|
_setdiff_() {
|
||||||
# DESC: Return items that exist in ARRAY1 that are do not exist in ARRAY2
|
# DESC: Return items that exist in ARRAY1 that are do not exist in ARRAY2
|
||||||
# ARGS: $1 (Required) - Array 1 in format ${ARRAY[*]}
|
# ARGS: $1 (Required) - Array 1 in format ${ARRAY[*]}
|
||||||
# $2 (Required) - Array 2 in format ${ARRAY[*]}
|
# $2 (Required) - Array 2 in format ${ARRAY[*]}
|
||||||
# OUTS: Prints unique terms
|
# OUTS: Prints unique terms
|
||||||
# USAGE: _setdiff_ "${array1[*]}" "${array2[*]}"
|
# USAGE: _setdiff_ "${array1[*]}" "${array2[*]}"
|
||||||
# NOTE: http://stackoverflow.com/a/1617303/142339
|
# NOTE: http://stackoverflow.com/a/1617303/142339
|
||||||
|
|
||||||
[[ $# -lt 2 ]] && fatal 'Missing required argument to _setdiff_()!'
|
[[ $# -lt 2 ]] && fatal 'Missing required argument to _setdiff_()!'
|
||||||
|
|
||||||
local debug skip a b
|
local debug skip a b
|
||||||
if [[ "$1" == 1 ]]; then
|
if [[ $1 == 1 ]]; then
|
||||||
debug=1
|
debug=1
|
||||||
shift
|
shift
|
||||||
fi
|
fi
|
||||||
if [[ "$1" ]]; then
|
if [[ "$1" ]]; then
|
||||||
local setdiffA setdiffB setdiffC
|
local setdiffA setdiffB setdiffC
|
||||||
# shellcheck disable=SC2206
|
# shellcheck disable=SC2206
|
||||||
setdiffA=($1)
|
setdiffA=($1)
|
||||||
# shellcheck disable=SC2206
|
# shellcheck disable=SC2206
|
||||||
setdiffB=($2)
|
setdiffB=($2)
|
||||||
fi
|
fi
|
||||||
setdiffC=()
|
setdiffC=()
|
||||||
for a in "${setdiffA[@]}"; do
|
for a in "${setdiffA[@]}"; do
|
||||||
skip=
|
skip=
|
||||||
for b in "${setdiffB[@]}"; do
|
for b in "${setdiffB[@]}"; do
|
||||||
[[ "$a" == "$b" ]] && skip=1 && break
|
[[ $a == "$b" ]] && skip=1 && break
|
||||||
|
done
|
||||||
|
[[ "$skip" ]] || setdiffC=("${setdiffC[@]}" "$a")
|
||||||
done
|
done
|
||||||
[[ "$skip" ]] || setdiffC=("${setdiffC[@]}" "$a")
|
[[ "$debug" ]] && for a in setdiffA setdiffB setdiffC; do
|
||||||
done
|
#shellcheck disable=SC1087
|
||||||
[[ "$debug" ]] && for a in setdiffA setdiffB setdiffC; do
|
echo "$a ($(eval echo "\${#$a[*]}")) $(eval echo "\${$a[*]}")" 1>&2
|
||||||
#shellcheck disable=SC1087
|
done
|
||||||
echo "$a ($(eval echo "\${#$a[*]}")) $(eval echo "\${$a[*]}")" 1>&2
|
[[ "$1" ]] && echo "${setdiffC[@]}"
|
||||||
done
|
|
||||||
[[ "$1" ]] && echo "${setdiffC[@]}"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_removeDupes_() {
|
_removeDupes_() {
|
||||||
# DESC: Removes duplicate array elements.
|
# DESC: Removes duplicate array elements.
|
||||||
# ARGS: $1 (Required) - Input array
|
# ARGS: $1 (Required) - Input array
|
||||||
# OUTS: Prints de-duped elements to standard out
|
# OUTS: Prints de-duped elements to standard out
|
||||||
# USAGE: _removeDups_ "${array[@]}"
|
# USAGE: _removeDups_ "${array[@]}"
|
||||||
# NOTE: List order may not stay the same.
|
# NOTE: List order may not stay the same.
|
||||||
# https://github.com/dylanaraps/pure-bash-bible
|
# https://github.com/dylanaraps/pure-bash-bible
|
||||||
declare -A tmp_array
|
declare -A tmp_array
|
||||||
|
|
||||||
for i in "$@"; do
|
for i in "$@"; do
|
||||||
@@ -88,11 +87,11 @@ _removeDupes_() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_randomArrayElement_() {
|
_randomArrayElement_() {
|
||||||
# DESC: Selects a random item from an array
|
# DESC: Selects a random item from an array
|
||||||
# ARGS: $1 (Required) - Input array
|
# ARGS: $1 (Required) - Input array
|
||||||
# OUTS: Prints result
|
# OUTS: Prints result
|
||||||
# USAGE: _randomArrayElement_ "${array[@]}"
|
# USAGE: _randomArrayElement_ "${array[@]}"
|
||||||
# NOTE: https://github.com/dylanaraps/pure-bash-bible
|
# NOTE: https://github.com/dylanaraps/pure-bash-bible
|
||||||
# Usage: random_array_element "array"
|
# Usage: random_array_element "array"
|
||||||
local arr=("$@")
|
local arr=("$@")
|
||||||
printf '%s\n' "${arr[RANDOM % $#]}"
|
printf '%s\n' "${arr[RANDOM % $#]}"
|
||||||
|
|||||||
@@ -1,361 +1,360 @@
|
|||||||
|
|
||||||
_execute_() {
|
_execute_() {
|
||||||
# DESC: Executes commands with safety and logging options
|
# DESC: Executes commands with safety and logging options
|
||||||
# ARGS: $1 (Required) - The command to be executed. Quotation marks MUST be escaped.
|
# ARGS: $1 (Required) - The command to be executed. Quotation marks MUST be escaped.
|
||||||
# $2 (Optional) - String to display after command is executed
|
# $2 (Optional) - String to display after command is executed
|
||||||
# OPTS: -v Always print debug output from the execute function
|
# OPTS: -v Always print debug output from the execute function
|
||||||
# -p Pass a failed command with 'return 0'. This effecively bypasses set -e.
|
# -p Pass a failed command with 'return 0'. This effecively bypasses set -e.
|
||||||
# -e Bypass _alert_ functions and use 'echo RESULT'
|
# -e Bypass _alert_ functions and use 'echo 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: None
|
# OUTS: None
|
||||||
# USE : _execute_ "cp -R \"~/dir/somefile.txt\" \"someNewFile.txt\"" "Optional message"
|
# USE : _execute_ "cp -R \"~/dir/somefile.txt\" \"someNewFile.txt\"" "Optional message"
|
||||||
# _execute_ -sv "mkdir \"some/dir\""
|
# _execute_ -sv "mkdir \"some/dir\""
|
||||||
# NOTE:
|
# NOTE:
|
||||||
# If $DRYRUN=true no commands are executed
|
# If $DRYRUN=true no commands are executed
|
||||||
# If $VERBOSE=true the command's native output is printed to
|
# If $VERBOSE=true the command's native output is printed to
|
||||||
# stderr and stdin. This can be forced with `_execute_ -v`
|
# stderr and stdin. This can be forced with `_execute_ -v`
|
||||||
|
|
||||||
local localVerbose=false
|
local LOCAL_VERBOSE=false
|
||||||
local passFailures=false
|
local PASS_FAILURES=false
|
||||||
local echoResult=false
|
local ECHO_RESULT=false
|
||||||
local successResult=false
|
local SUCCESS_RESULT=false
|
||||||
local quietResult=false
|
local QUIET_RESULT=false
|
||||||
local opt
|
local opt
|
||||||
|
|
||||||
local OPTIND=1
|
local OPTIND=1
|
||||||
while getopts ":vVpPeEsSqQ" opt; do
|
while getopts ":vVpPeEsSqQ" opt; do
|
||||||
case $opt in
|
case $opt in
|
||||||
v | V) localVerbose=true ;;
|
v | V) LOCAL_VERBOSE=true ;;
|
||||||
p | P) passFailures=true ;;
|
p | P) PASS_FAILURES=true ;;
|
||||||
e | E) echoResult=true ;;
|
e | E) ECHO_RESULT=true ;;
|
||||||
s | S) successResult=true ;;
|
s | S) SUCCESS_RESULT=true ;;
|
||||||
q | Q) quietResult=true ;;
|
q | Q) QUIET_RESULT=true ;;
|
||||||
*)
|
*)
|
||||||
{
|
{
|
||||||
error "Unrecognized option '$1' passed to _execute_. Exiting."
|
error "Unrecognized option '$1' passed to _execute_. Exiting."
|
||||||
_safeExit_
|
_safeExit_
|
||||||
}
|
}
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
shift $((OPTIND - 1))
|
shift $((OPTIND - 1))
|
||||||
|
|
||||||
local cmd="${1:?_execute_ needs a command}"
|
local CMD="${1:?_execute_ needs a command}"
|
||||||
local message="${2:-$1}"
|
local EXECUTE_MESSAGE="${2:-$1}"
|
||||||
|
|
||||||
local saveVerbose=$VERBOSE
|
local SAVE_VERBOSE=${VERBOSE}
|
||||||
if "${localVerbose}"; then
|
if "${LOCAL_VERBOSE}"; then
|
||||||
VERBOSE=true
|
VERBOSE=true
|
||||||
fi
|
|
||||||
|
|
||||||
if "${DRYRUN}"; then
|
|
||||||
if "$quietResult"; then
|
|
||||||
VERBOSE=$saveVerbose
|
|
||||||
return 0
|
|
||||||
fi
|
fi
|
||||||
if [ -n "${2:-}" ]; then
|
|
||||||
dryrun "${1} (${2})" "$(caller)"
|
if "${DRYRUN}"; then
|
||||||
|
if "${QUIET_RESULT}"; then
|
||||||
|
VERBOSE=$SAVE_VERBOSE
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
if [ -n "${2:-}" ]; then
|
||||||
|
dryrun "${1} (${2})" "$(caller)"
|
||||||
|
else
|
||||||
|
dryrun "${1}" "$(caller)"
|
||||||
|
fi
|
||||||
|
elif ${VERBOSE}; then
|
||||||
|
if eval "${CMD}"; then
|
||||||
|
if "${ECHO_RESULT}"; then
|
||||||
|
echo "${EXECUTE_MESSAGE}"
|
||||||
|
elif "${SUCCESS_RESULT}"; then
|
||||||
|
success "${EXECUTE_MESSAGE}"
|
||||||
|
else
|
||||||
|
info "${EXECUTE_MESSAGE}"
|
||||||
|
fi
|
||||||
|
VERBOSE=${SAVE_VERBOSE}
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
if "${ECHO_RESULT}"; then
|
||||||
|
echo "warning: ${EXECUTE_MESSAGE}"
|
||||||
|
else
|
||||||
|
warning "${EXECUTE_MESSAGE}"
|
||||||
|
fi
|
||||||
|
VERBOSE=${SAVE_VERBOSE}
|
||||||
|
"${PASS_FAILURES}" && return 0 || return 1
|
||||||
|
fi
|
||||||
else
|
else
|
||||||
dryrun "${1}" "$(caller)"
|
if eval "${CMD}" &>/dev/null; then
|
||||||
|
if "${QUIET_RESULT}"; then
|
||||||
|
VERBOSE=${SAVE_VERBOSE}
|
||||||
|
return 0
|
||||||
|
elif "${ECHO_RESULT}"; then
|
||||||
|
echo "${EXECUTE_MESSAGE}"
|
||||||
|
elif "${SUCCESS_RESULT}"; then
|
||||||
|
success "${EXECUTE_MESSAGE}"
|
||||||
|
else
|
||||||
|
info "${EXECUTE_MESSAGE}"
|
||||||
|
fi
|
||||||
|
VERBOSE=${SAVE_VERBOSE}
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
if "${ECHO_RESULT}"; then
|
||||||
|
echo "error: ${EXECUTE_MESSAGE}"
|
||||||
|
else
|
||||||
|
warning "${EXECUTE_MESSAGE}"
|
||||||
|
fi
|
||||||
|
VERBOSE=${SAVE_VERBOSE}
|
||||||
|
"${PASS_FAILURES}" && return 0 || return 1
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
elif ${VERBOSE}; then
|
|
||||||
if eval "${cmd}"; then
|
|
||||||
if "$echoResult"; then
|
|
||||||
echo "${message}"
|
|
||||||
elif "${successResult}"; then
|
|
||||||
success "${message}"
|
|
||||||
else
|
|
||||||
info "${message}"
|
|
||||||
fi
|
|
||||||
VERBOSE=$saveVerbose
|
|
||||||
return 0
|
|
||||||
else
|
|
||||||
if "$echoResult"; then
|
|
||||||
echo "warning: ${message}"
|
|
||||||
else
|
|
||||||
warning "${message}"
|
|
||||||
fi
|
|
||||||
VERBOSE=$saveVerbose
|
|
||||||
"${passFailures}" && return 0 || return 1
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
if eval "${cmd}" &>/dev/null; then
|
|
||||||
if "$quietResult"; then
|
|
||||||
VERBOSE=$saveVerbose
|
|
||||||
return 0
|
|
||||||
elif "$echoResult"; then
|
|
||||||
echo "${message}"
|
|
||||||
elif "${successResult}"; then
|
|
||||||
success "${message}"
|
|
||||||
else
|
|
||||||
info "${message}"
|
|
||||||
fi
|
|
||||||
VERBOSE=$saveVerbose
|
|
||||||
return 0
|
|
||||||
else
|
|
||||||
if "$echoResult"; then
|
|
||||||
echo "error: ${message}"
|
|
||||||
else
|
|
||||||
warning "${message}"
|
|
||||||
fi
|
|
||||||
VERBOSE=$saveVerbose
|
|
||||||
"${passFailures}" && return 0 || return 1
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_findBaseDir_() {
|
_findBaseDir_() {
|
||||||
# DESC: Locates the real directory of the script being run. Similar to GNU readlink -n
|
# DESC: Locates the real directory of the script being run. Similar to GNU readlink -n
|
||||||
# ARGS: None
|
# ARGS: None
|
||||||
# OUTS: Echo result to STDOUT
|
# OUTS: Echo result to STDOUT
|
||||||
# USE : baseDir="$(_findBaseDir_)"
|
# USE : baseDir="$(_findBaseDir_)"
|
||||||
# cp "$(_findBaseDir_ "somefile.txt")" "other_file.txt"
|
# cp "$(_findBaseDir_ "somefile.txt")" "other_file.txt"
|
||||||
|
|
||||||
local SOURCE
|
local SOURCE
|
||||||
local DIR
|
local DIR
|
||||||
|
|
||||||
# Is file sourced?
|
# Is file sourced?
|
||||||
[[ $_ != "$0" ]] \
|
[[ $_ != "$0" ]] \
|
||||||
&& SOURCE="${BASH_SOURCE[1]}" \
|
&& SOURCE="${BASH_SOURCE[1]}" \
|
||||||
|| SOURCE="${BASH_SOURCE[0]}"
|
|| SOURCE="${BASH_SOURCE[0]}"
|
||||||
|
|
||||||
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
|
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
|
||||||
DIR="$(cd -P "$(dirname "$SOURCE")" && pwd)"
|
DIR="$(cd -P "$(dirname "$SOURCE")" && pwd)"
|
||||||
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
|
||||||
echo "$(cd -P "$(dirname "${SOURCE}")" && pwd)"
|
echo "$(cd -P "$(dirname "${SOURCE}")" && pwd)"
|
||||||
}
|
}
|
||||||
|
|
||||||
_checkBinary_() {
|
_checkBinary_() {
|
||||||
# DESC: Check if a binary exists in the search PATH
|
# DESC: Check if a binary exists in the search PATH
|
||||||
# ARGS: $1 (Required) - Name of the binary to check for existence
|
# ARGS: $1 (Required) - Name of the binary to check for existence
|
||||||
# OUTS: true/false
|
# OUTS: true/false
|
||||||
# USAGE: (_checkBinary_ ffmpeg ) && [SUCCESS] || [FAILURE]
|
# USAGE: (_checkBinary_ ffmpeg ) && [SUCCESS] || [FAILURE]
|
||||||
if [[ $# -lt 1 ]]; then
|
if [[ $# -lt 1 ]]; then
|
||||||
error 'Missing required argument to _checkBinary_()!'
|
error 'Missing required argument to _checkBinary_()!'
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if ! command -v "$1" >/dev/null 2>&1; then
|
if ! command -v "$1" >/dev/null 2>&1; then
|
||||||
debug "Did not find dependency: '$1'"
|
debug "Did not find dependency: '$1'"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
_haveFunction_() {
|
_haveFunction_() {
|
||||||
# DESC: Tests if a function exists.
|
# DESC: Tests if a function exists.
|
||||||
# ARGS: $1 (Required) - Function name
|
# ARGS: $1 (Required) - Function name
|
||||||
# OUTS: true/false
|
# OUTS: true/false
|
||||||
local f
|
local f
|
||||||
f="$1"
|
f="$1"
|
||||||
|
|
||||||
if declare -f "$f" &>/dev/null 2>&1; then
|
if declare -f "${f}" &>/dev/null 2>&1; then
|
||||||
return 0
|
return 0
|
||||||
else
|
else
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
_pauseScript_() {
|
_pauseScript_() {
|
||||||
# DESC: Pause a script at any point and continue after user input
|
# DESC: Pause a script at any point and continue after user input
|
||||||
# ARGS: $1 (Optional) - String for customized message
|
# ARGS: $1 (Optional) - String for customized message
|
||||||
# OUTS: None
|
# OUTS: None
|
||||||
|
|
||||||
local pauseMessage
|
local pauseMessage
|
||||||
pauseMessage="${1:-Paused}. Ready to continue?"
|
pauseMessage="${1:-Paused}. Ready to continue?"
|
||||||
|
|
||||||
if _seekConfirmation_ "${pauseMessage}"; then
|
if _seekConfirmation_ "${pauseMessage}"; then
|
||||||
info "Continuing..."
|
info "Continuing..."
|
||||||
else
|
else
|
||||||
notice "Exiting Script"
|
notice "Exiting Script"
|
||||||
_safeExit_
|
_safeExit_
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
_progressBar_() {
|
_progressBar_() {
|
||||||
# DESC: Prints a progress bar within a for/while loop
|
# DESC: Prints a progress bar within a for/while loop
|
||||||
# ARGS: $1 (Required) - The total number of items counted
|
# ARGS: $1 (Required) - The total number of items counted
|
||||||
# $2 (Optional) - The optional title of the progress bar
|
# $2 (Optional) - The optional title of the progress bar
|
||||||
# OUTS: None
|
# OUTS: None
|
||||||
# USAGE:
|
# USAGE:
|
||||||
# for number in $(seq 0 100); do
|
# for number in $(seq 0 100); do
|
||||||
# sleep 1
|
# sleep 1
|
||||||
# _progressBar_ "100" "Counting numbers"
|
# _progressBar_ "100" "Counting numbers"
|
||||||
# done
|
# done
|
||||||
|
|
||||||
($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 width bar_char perc num bar progressBarLine barTitle n
|
local width bar_char perc num bar progressBarLine barTitle n
|
||||||
|
|
||||||
n="${1:?_progressBar_ needs input}"
|
n="${1:?_progressBar_ needs input}"
|
||||||
((n = n - 1))
|
((n = n - 1))
|
||||||
barTitle="${2:-Running Process}"
|
barTitle="${2:-Running Process}"
|
||||||
width=30
|
width=30
|
||||||
bar_char="#"
|
bar_char="#"
|
||||||
|
|
||||||
# Reset the count
|
# Reset the count
|
||||||
[ -z "${progressBarProgress}" ] && progressBarProgress=0
|
[ -z "${progressBarProgress}" ] && progressBarProgress=0
|
||||||
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.
|
||||||
perc=$((progressBarProgress * 100 / $1))
|
perc=$((progressBarProgress * 100 / $1))
|
||||||
# Compute the number of blocks to represent the percentage.
|
# Compute the number of blocks to represent the percentage.
|
||||||
num=$((progressBarProgress * width / $1))
|
num=$((progressBarProgress * width / $1))
|
||||||
# Create the progress bar string.
|
# Create the progress bar string.
|
||||||
bar=""
|
bar=""
|
||||||
if [ ${num} -gt 0 ]; then
|
if [ ${num} -gt 0 ]; then
|
||||||
bar=$(printf "%0.s${bar_char}" $(seq 1 ${num}))
|
bar=$(printf "%0.s${bar_char}" $(seq 1 ${num}))
|
||||||
|
fi
|
||||||
|
# Print the progress bar.
|
||||||
|
progressBarLine=$(printf "%s [%-${width}s] (%d%%)" " ${barTitle}" "${bar}" "${perc}")
|
||||||
|
echo -ne "${progressBarLine}\r"
|
||||||
|
progressBarProgress=$((progressBarProgress + 1))
|
||||||
|
else
|
||||||
|
# Clear the progress bar when complete
|
||||||
|
# echo -ne "\033[0K\r"
|
||||||
|
tput el # Clear the line
|
||||||
|
|
||||||
|
unset progressBarProgress
|
||||||
fi
|
fi
|
||||||
# Print the progress bar.
|
|
||||||
progressBarLine=$(printf "%s [%-${width}s] (%d%%)" " ${barTitle}" "${bar}" "${perc}")
|
|
||||||
echo -ne "${progressBarLine}\r"
|
|
||||||
progressBarProgress=$((progressBarProgress + 1))
|
|
||||||
else
|
|
||||||
# Clear the progress bar when complete
|
|
||||||
# echo -ne "\033[0K\r"
|
|
||||||
tput el # Clear the line
|
|
||||||
|
|
||||||
unset progressBarProgress
|
tput cnorm
|
||||||
fi
|
|
||||||
|
|
||||||
tput cnorm
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_rootAvailable_() {
|
_rootAvailable_() {
|
||||||
# DESC: Validate we have superuser access as root (via sudo if requested)
|
# DESC: Validate we have superuser access as root (via sudo if requested)
|
||||||
# ARGS: $1 (optional): Set to any value to not attempt root access via sudo
|
# ARGS: $1 (optional): Set to any value to not attempt root access via sudo
|
||||||
# OUTS: None
|
# OUTS: None
|
||||||
# NOTE: https://github.com/ralish/bash-script-template
|
# NOTE: https://github.com/ralish/bash-script-template
|
||||||
|
|
||||||
local superuser
|
local superuser
|
||||||
if [[ ${EUID} -eq 0 ]]; then
|
if [[ ${EUID} -eq 0 ]]; then
|
||||||
superuser=true
|
superuser=true
|
||||||
elif [[ -z ${1:-} ]]; then
|
elif [[ -z ${1:-} ]]; then
|
||||||
if command -v sudo &>/dev/null; then
|
if command -v sudo &>/dev/null; then
|
||||||
debug 'Sudo: Updating cached credentials ...'
|
debug 'Sudo: Updating cached credentials ...'
|
||||||
if ! sudo -v; then
|
if ! sudo -v; then
|
||||||
warning "Sudo: Couldn't acquire credentials ..."
|
warning "Sudo: Couldn't acquire credentials ..."
|
||||||
else
|
else
|
||||||
local test_euid
|
local test_euid
|
||||||
test_euid="$(sudo -H -- "$BASH" -c 'printf "%s" "$EUID"')"
|
test_euid="$(sudo -H -- "$BASH" -c 'printf "%s" "$EUID"')"
|
||||||
if [[ ${test_euid} -eq 0 ]]; then
|
if [[ ${test_euid} -eq 0 ]]; then
|
||||||
superuser=true
|
superuser=true
|
||||||
|
fi
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ -z ${superuser:-} ]]; then
|
if [[ -z ${superuser:-} ]]; then
|
||||||
notice 'Unable to acquire superuser credentials.'
|
notice 'Unable to acquire superuser credentials.'
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
info 'Successfully acquired superuser credentials.'
|
info 'Successfully acquired superuser credentials.'
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
_runAsRoot_() {
|
_runAsRoot_() {
|
||||||
# DESC: Run the requested command as root (via sudo if requested)
|
# DESC: Run the requested command as root (via sudo if requested)
|
||||||
# ARGS: $1 (optional): Set to zero to not attempt execution via sudo
|
# ARGS: $1 (optional): Set to zero to not attempt execution via sudo
|
||||||
# $@ (required): Passed through for execution as root user
|
# $@ (required): Passed through for execution as root user
|
||||||
# OUTS: None
|
# OUTS: None
|
||||||
# NOTE: https://github.com/ralish/bash-script-template
|
# NOTE: https://github.com/ralish/bash-script-template
|
||||||
|
|
||||||
if [[ $# -eq 0 ]]; then
|
if [[ $# -eq 0 ]]; then
|
||||||
fatal 'Missing required argument to _runAsRoot_()!'
|
fatal 'Missing required argument to _runAsRoot_()!'
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ ${1:-} =~ ^0$ ]]; then
|
if [[ ${1:-} =~ ^0$ ]]; then
|
||||||
local skip_sudo=true
|
local skip_sudo=true
|
||||||
shift
|
shift
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ ${EUID} -eq 0 ]]; then
|
if [[ ${EUID} -eq 0 ]]; then
|
||||||
"$@"
|
"$@"
|
||||||
elif [[ -z ${skip_sudo:-} ]]; then
|
elif [[ -z ${skip_sudo:-} ]]; then
|
||||||
sudo -H -- "$@"
|
sudo -H -- "$@"
|
||||||
else
|
else
|
||||||
fatal "Unable to run requested command as root: $*"
|
fatal "Unable to run requested command as root: $*"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
_seekConfirmation_() {
|
_seekConfirmation_() {
|
||||||
# DESC: Seek user input for yes/no question
|
# DESC: Seek user input for yes/no question
|
||||||
# ARGS: $1 (Optional) - Question being asked
|
# ARGS: $1 (Optional) - Question being asked
|
||||||
# OUTS: true/false
|
# OUTS: true/false
|
||||||
# USAGE: _seekConfirmation_ "Do something?" && echo "okay" || echo "not okay"
|
# USAGE: _seekConfirmation_ "Do something?" && echo "okay" || echo "not okay"
|
||||||
# OR
|
# OR
|
||||||
# if _seekConfirmation_ "Answer this question"; then
|
# if _seekConfirmation_ "Answer this question"; then
|
||||||
# something
|
# something
|
||||||
# fi
|
# fi
|
||||||
|
|
||||||
input "${1:-}"
|
input "${1:-}"
|
||||||
if "${FORCE}"; then
|
if "${FORCE}"; then
|
||||||
debug "Forcing confirmation with '--force' flag set"
|
debug "Forcing confirmation with '--force' flag set"
|
||||||
echo -e ""
|
echo -e ""
|
||||||
return 0
|
return 0
|
||||||
else
|
else
|
||||||
while true; do
|
while true; do
|
||||||
read -r -p " (y/n) " yn
|
read -r -p " (y/n) " yn
|
||||||
case $yn in
|
case $yn in
|
||||||
[Yy]*) return 0 ;;
|
[Yy]*) return 0 ;;
|
||||||
[Nn]*) return 1 ;;
|
[Nn]*) return 1 ;;
|
||||||
*) input "Please answer yes or no." ;;
|
*) input "Please answer yes or no." ;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
_setPATH_() {
|
_setPATH_() {
|
||||||
# DESC: Add directories to $PATH so script can find executables
|
# DESC: Add directories to $PATH so script can find executables
|
||||||
# ARGS: $@ - One or more paths
|
# ARGS: $@ - One or more paths
|
||||||
# OUTS: $PATH
|
# OUTS: $PATH
|
||||||
# USAGE: _setPATH_ "/usr/local/bin" "${HOME}/bin" "$(npm bin)"
|
# USAGE: _setPATH_ "/usr/local/bin" "${HOME}/bin" "$(npm bin)"
|
||||||
local NEWPATH NEWPATHS USERPATH
|
local NEWPATH NEWPATHS USERPATH
|
||||||
|
|
||||||
for USERPATH in "$@"; do
|
for USERPATH in "$@"; do
|
||||||
NEWPATHS+=("$USERPATH")
|
NEWPATHS+=("$USERPATH")
|
||||||
done
|
done
|
||||||
|
|
||||||
for NEWPATH in "${NEWPATHS[@]}"; do
|
for NEWPATH in "${NEWPATHS[@]}"; do
|
||||||
if ! echo "$PATH" | grep -Eq "(^|:)${NEWPATH}($|:)"; then
|
if ! echo "$PATH" | grep -Eq "(^|:)${NEWPATH}($|:)"; then
|
||||||
PATH="${NEWPATH}:${PATH}"
|
PATH="${NEWPATH}:${PATH}"
|
||||||
debug "Added '${tan}${NEWPATH}${purple}' to PATH"
|
debug "Added '${tan}${NEWPATH}${purple}' to PATH"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
_safeExit_() {
|
_safeExit_() {
|
||||||
# DESC: Cleanup and exit from a script
|
# DESC: Cleanup and exit from a script
|
||||||
# ARGS: $1 (optional) - Exit code (defaults to 0)
|
# ARGS: $1 (optional) - Exit code (defaults to 0)
|
||||||
# OUTS: None
|
# OUTS: None
|
||||||
|
|
||||||
if [[ -d "${script_lock:-}" ]]; then
|
if [[ -d ${SCRIPT_LOCK:-} ]]; then
|
||||||
if command rm -rf "${script_lock}"; then
|
if command rm -rf "${SCRIPT_LOCK}"; then
|
||||||
debug "Removing script lock"
|
debug "Removing script lock"
|
||||||
else
|
else
|
||||||
warning "Script lock could not be removed. Try manually deleting ${tan}'${lock_dir}'${red}"
|
warning "Script lock could not be removed. Try manually deleting ${tan}'${LOCK_DIR}'${red}"
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ -n "${tmpDir:-}" && -d "${tmpDir:-}" ]]; then
|
if [[ -n ${TMP_DIR:-} && -d ${TMP_DIR:-} ]]; then
|
||||||
if [[ ${1:-} == 1 && -n "$(ls "${tmpDir}")" ]]; then
|
if [[ ${1:-} == 1 && -n "$(ls "${TMP_DIR}")" ]]; then
|
||||||
command rm -r "${tmpDir}"
|
command rm -r "${TMP_DIR}"
|
||||||
else
|
else
|
||||||
command rm -r "${tmpDir}"
|
command rm -r "${TMP_DIR}"
|
||||||
debug "Removing temp directory"
|
debug "Removing temp directory"
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
fi
|
|
||||||
|
|
||||||
trap - INT TERM EXIT
|
trap - INT TERM EXIT
|
||||||
exit ${1:-0}
|
exit ${1:-0}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,34 +1,34 @@
|
|||||||
_makeCSV_() {
|
_makeCSV_() {
|
||||||
# Creates a new CSV file if one does not already exist
|
# Creates a new CSV file if one does not already exist
|
||||||
# Takes passed arguments and writes them as a header line to the CSV
|
# Takes passed arguments and writes them as a header line to the CSV
|
||||||
# Usage '_makeCSV_ column1 column2 column3'
|
# Usage '_makeCSV_ column1 column2 column3'
|
||||||
|
|
||||||
# Set the location and name of the CSV File
|
# Set the location and name of the CSV File
|
||||||
if [ -z "${csvLocation}" ]; then
|
if [ -z "${csvLocation}" ]; then
|
||||||
csvLocation="${HOME}/Desktop"
|
csvLocation="${HOME}/Desktop"
|
||||||
fi
|
|
||||||
if [ -z "${csvName}" ]; then
|
|
||||||
csvName="$(LC_ALL=C date +%Y-%m-%d)-${FUNCNAME[1]}.csv"
|
|
||||||
fi
|
|
||||||
csvFile="${csvLocation}/${csvName}"
|
|
||||||
|
|
||||||
# Overwrite existing file? If not overwritten, new content is added
|
|
||||||
# to the bottom of the existing file
|
|
||||||
if [ -f "${csvFile}" ]; then
|
|
||||||
if _seekConfirmation_ "${csvFile} already exists. Overwrite?"; then
|
|
||||||
rm "${csvFile}"
|
|
||||||
fi
|
fi
|
||||||
fi
|
if [ -z "${csvName}" ]; then
|
||||||
_writeCSV_ "$@"
|
csvName="$(LC_ALL=C date +%Y-%m-%d)-${FUNCNAME[1]}.csv"
|
||||||
|
fi
|
||||||
|
csvFile="${csvLocation}/${csvName}"
|
||||||
|
|
||||||
|
# Overwrite existing file? If not overwritten, new content is added
|
||||||
|
# to the bottom of the existing file
|
||||||
|
if [ -f "${csvFile}" ]; then
|
||||||
|
if _seekConfirmation_ "${csvFile} already exists. Overwrite?"; then
|
||||||
|
rm "${csvFile}"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
_writeCSV_ "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
_writeCSV_() {
|
_writeCSV_() {
|
||||||
# Takes passed arguments and writes them as a comma separated line
|
# Takes passed arguments and writes them as a comma separated line
|
||||||
# Usage '_writeCSV_ column1 column2 column3'
|
# Usage '_writeCSV_ column1 column2 column3'
|
||||||
|
|
||||||
local csvInput=("$@")
|
local csvInput=("$@")
|
||||||
saveIFS=$IFS
|
saveIFS=$IFS
|
||||||
IFS=','
|
IFS=','
|
||||||
echo "${csvInput[*]}" >>"${csvFile}"
|
echo "${csvInput[*]}" >>"${csvFile}"
|
||||||
IFS=${saveIFS}
|
IFS=${saveIFS}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,381 +1,392 @@
|
|||||||
_monthToNumber_() {
|
_monthToNumber_() {
|
||||||
# DESC: Convert a month name to a number
|
# DESC: Convert a month name to a number
|
||||||
# ARGS: None
|
# ARGS: None
|
||||||
# OUTS: Prints the number of the month to stdout
|
# OUTS: Prints the number of the month to stdout
|
||||||
# USAGE: _monthToNumber_ "January"
|
# USAGE: _monthToNumber_ "January"
|
||||||
|
|
||||||
local mon="$(echo "$1" | tr '[:upper:]' '[:lower:]')"
|
local 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 ;;
|
||||||
march|mar|ma) echo 3 ;;
|
march | mar | ma) echo 3 ;;
|
||||||
april|apr|ap) echo 4 ;;
|
april | apr | ap) echo 4 ;;
|
||||||
may) echo 5 ;;
|
may) echo 5 ;;
|
||||||
june|jun|ju) echo 6 ;;
|
june | jun | ju) echo 6 ;;
|
||||||
july|jul) echo 7 ;;
|
july | jul) echo 7 ;;
|
||||||
august|aug|au) echo 8 ;;
|
august | aug | au) echo 8 ;;
|
||||||
september|sep|se) echo 9 ;;
|
september | sep | se) echo 9 ;;
|
||||||
october|oct) echo 10 ;;
|
october | oct) echo 10 ;;
|
||||||
november|nov|no) echo 11 ;;
|
november | nov | no) echo 11 ;;
|
||||||
december|dec|de) echo 12 ;;
|
december | dec | de) echo 12 ;;
|
||||||
*)
|
*)
|
||||||
warning "month_monthToNumber_: Bad monthname: $1"
|
warning "month_monthToNumber_: Bad monthname: $1"
|
||||||
return 1 ;;
|
return 1
|
||||||
esac
|
;;
|
||||||
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
_numberToMonth_() {
|
_numberToMonth_() {
|
||||||
# DESC: Convert a month number to its name
|
# DESC: Convert a month number to its name
|
||||||
# ARGS: None
|
# ARGS: None
|
||||||
# OUTS: Prints the name of the month to stdout
|
# OUTS: Prints the name of the month to stdout
|
||||||
# USAGE: _numberToMonth_ 1
|
# USAGE: _numberToMonth_ 1
|
||||||
|
|
||||||
local mon="$1"
|
local mon="$1"
|
||||||
case "$mon" in
|
case "$mon" in
|
||||||
1|01) echo January ;;
|
1 | 01) echo January ;;
|
||||||
2|02) echo February ;;
|
2 | 02) echo February ;;
|
||||||
3|03) echo March ;;
|
3 | 03) echo March ;;
|
||||||
4|04) echo April ;;
|
4 | 04) echo April ;;
|
||||||
5|05) echo May ;;
|
5 | 05) echo May ;;
|
||||||
6|06) echo June ;;
|
6 | 06) echo June ;;
|
||||||
7|07) echo July ;;
|
7 | 07) echo July ;;
|
||||||
8|08) echo August ;;
|
8 | 08) echo August ;;
|
||||||
9|09) echo September ;;
|
9 | 09) echo September ;;
|
||||||
10) echo October ;;
|
10) echo October ;;
|
||||||
11) echo November ;;
|
11) echo November ;;
|
||||||
12) echo December ;;
|
12) echo December ;;
|
||||||
*)
|
*)
|
||||||
warning "_numberToMonth_: Bad month number: $1"
|
warning "_numberToMonth_: Bad month number: $1"
|
||||||
return 1 ;;
|
return 1
|
||||||
esac
|
;;
|
||||||
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
_parseDate_() {
|
_parseDate_() {
|
||||||
# DESC: Takes a string as input and attempts to find a date within it
|
# DESC: Takes a string as input and attempts to find a date within it
|
||||||
# to parse into component parts (day, month, year)
|
# to parse into component parts (day, month, year)
|
||||||
# ARGS: $1 (required) - A string
|
# ARGS: $1 (required) - A string
|
||||||
# OUTS: Returns error if no date found
|
# OUTS: Returns error if no date found
|
||||||
# $_parseDate_found - The date found in the string
|
# $_parseDate_found - The date found in the string
|
||||||
# $_parseDate_year - The year
|
# $_parseDate_year - The year
|
||||||
# $_parseDate_month - The number month
|
# $_parseDate_month - The number month
|
||||||
# $_parseDate_monthName - The name of the month
|
# $_parseDate_monthName - The name of the month
|
||||||
# $_parseDate_day - The day
|
# $_parseDate_day - The day
|
||||||
# $_parseDate_hour - The hour (if avail)
|
# $_parseDate_hour - The hour (if avail)
|
||||||
# $_parseDate_minute - The minute (if avail)
|
# $_parseDate_minute - The minute (if avail)
|
||||||
# USAGE: if _parseDate_ "[STRING]"; then ...
|
# USAGE: if _parseDate_ "[STRING]"; then ...
|
||||||
# NOTE: This function only recognizes dates from the year 2000 to 2029
|
# NOTE: This function only recognizes dates from the year 2000 to 2029
|
||||||
# NOTE: Will recognize dates in the following formats separated by '-_ ./'
|
# NOTE: Will recognize dates in the following formats separated by '-_ ./'
|
||||||
# * YYYY-MM-DD * Month DD, YYYY * DD Month, YYYY
|
# * YYYY-MM-DD * Month DD, YYYY * DD Month, YYYY
|
||||||
# * Month, YYYY * Month, DD YY * MM-DD-YYYY
|
# * Month, YYYY * Month, DD YY * MM-DD-YYYY
|
||||||
# * MMDDYYYY * YYYYMMDD * DDMMYYYY
|
# * MMDDYYYY * YYYYMMDD * DDMMYYYY
|
||||||
# * YYYYMMDDHHMM * YYYYMMDDHH * DD-MM-YYYY
|
# * YYYYMMDDHHMM * YYYYMMDDHH * DD-MM-YYYY
|
||||||
# * DD MM YY * MM DD YY
|
# * DD MM YY * MM DD YY
|
||||||
# TODO: Impelemt the following date formats
|
# TODO: Impelemt the following date formats
|
||||||
# * MMDDYY * YYMMDD * mon-DD-YY
|
# * MMDDYY * YYMMDD * mon-DD-YY
|
||||||
|
|
||||||
[[ $# -eq 0 ]] && {
|
[[ $# -eq 0 ]] && {
|
||||||
error 'Missing required argument to _parseDate_()!'
|
error 'Missing required argument to _parseDate_()!'
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
local date="${1:-$(date +%F)}"
|
|
||||||
_parseDate_found="" _parseDate_year="" _parseDate_month="" _parseDate_monthName=""
|
|
||||||
_parseDate_day="" _parseDate_hour="" _parseDate_minute=""
|
|
||||||
|
|
||||||
shopt -s nocasematch #Use case-insensitive regex
|
|
||||||
|
|
||||||
debug "_parseDate_() input ${tan}$date${purple}"
|
|
||||||
|
|
||||||
# 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].*|$)"
|
|
||||||
if [[ "${date}" =~ $pat ]]; then
|
|
||||||
_parseDate_found="${BASH_REMATCH[2]}"
|
|
||||||
_parseDate_year=$(( 10#${BASH_REMATCH[3]} ))
|
|
||||||
_parseDate_month=$(( 10#${BASH_REMATCH[4]} ))
|
|
||||||
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
|
||||||
_parseDate_day=$(( 10#${BASH_REMATCH[5]} ))
|
|
||||||
debug "regex match: ${tan}YYYY-MM-DD${purple}"
|
|
||||||
|
|
||||||
# Month DD, YYYY
|
|
||||||
elif [[ "${date}" =~ ((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
|
|
||||||
_parseDate_found="${BASH_REMATCH[1]:-}"
|
|
||||||
_parseDate_month=$(_monthToNumber_ ${BASH_REMATCH[2]:-})
|
|
||||||
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month:-}")"
|
|
||||||
_parseDate_day=$(( 10#${BASH_REMATCH[3]:-} ))
|
|
||||||
_parseDate_year=$(( 10#${BASH_REMATCH[5]:-} ))
|
|
||||||
debug "regex match: ${tan}Month DD, YYYY${purple}"
|
|
||||||
|
|
||||||
# Month DD, YY
|
|
||||||
elif [[ "${date}" =~ ((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
|
|
||||||
_parseDate_found="${BASH_REMATCH[1]}"
|
|
||||||
_parseDate_month=$(_monthToNumber_ ${BASH_REMATCH[2]})
|
|
||||||
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
|
||||||
_parseDate_day=$(( 10#${BASH_REMATCH[3]} ))
|
|
||||||
_parseDate_year="20$(( 10#${BASH_REMATCH[5]} ))"
|
|
||||||
debug "regex match: ${tan}Month DD, YY${purple}"
|
|
||||||
|
|
||||||
# DD Month YYYY
|
|
||||||
elif [[ "${date}" =~ (.*[^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
|
|
||||||
_parseDate_found="${BASH_REMATCH[2]}"
|
|
||||||
_parseDate_day=$(( 10#"${BASH_REMATCH[3]}" ))
|
|
||||||
_parseDate_month="$(_monthToNumber_ "${BASH_REMATCH[4]}")"
|
|
||||||
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
|
||||||
_parseDate_year=$(( 10#"${BASH_REMATCH[5]}" ))
|
|
||||||
debug "regex match: ${tan}DD Month, YYYY${purple}"
|
|
||||||
|
|
||||||
# MM-DD-YYYY or DD-MM-YYYY
|
|
||||||
elif [[ "${date}" =~ (.*[^0-9]|^)(([ 0-9]{1,2})[-\.\/_ ]+([ 0-9]{1,2})[-\.\/_ ]+(20[0-2][0-9]))([^0-9].*|$) ]]; then
|
|
||||||
|
|
||||||
if [[ $(( 10#${BASH_REMATCH[3]} )) -lt 13 && \
|
|
||||||
$(( 10#${BASH_REMATCH[4]} )) -gt 12 && \
|
|
||||||
$(( 10#${BASH_REMATCH[4]} )) -lt 32
|
|
||||||
]]; then
|
|
||||||
_parseDate_found="${BASH_REMATCH[2]}"
|
|
||||||
_parseDate_year=$(( 10#${BASH_REMATCH[5]} ))
|
|
||||||
_parseDate_month=$(( 10#${BASH_REMATCH[3]} ))
|
|
||||||
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
|
||||||
_parseDate_day=$(( 10#${BASH_REMATCH[4]} ))
|
|
||||||
debug "regex match: ${tan}MM-DD-YYYY${purple}"
|
|
||||||
elif [[ $(( 10#${BASH_REMATCH[3]} )) -gt 12 && \
|
|
||||||
$(( 10#${BASH_REMATCH[3]} )) -lt 32 && \
|
|
||||||
$(( 10#${BASH_REMATCH[4]} )) -lt 13
|
|
||||||
]]; then
|
|
||||||
_parseDate_found="${BASH_REMATCH[2]}"
|
|
||||||
_parseDate_year=$(( 10#${BASH_REMATCH[5]} ))
|
|
||||||
_parseDate_month=$(( 10#${BASH_REMATCH[4]} ))
|
|
||||||
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
|
||||||
_parseDate_day=$(( 10#${BASH_REMATCH[3]} ))
|
|
||||||
debug "regex match: ${tan}DD-MM-YYYY${purple}"
|
|
||||||
elif [[ $(( 10#${BASH_REMATCH[3]} )) -lt 32 && \
|
|
||||||
$(( 10#${BASH_REMATCH[4]} )) -lt 13
|
|
||||||
]]; then
|
|
||||||
_parseDate_found="${BASH_REMATCH[2]}"
|
|
||||||
_parseDate_year=$(( 10#${BASH_REMATCH[5]} ))
|
|
||||||
_parseDate_month=$(( 10#${BASH_REMATCH[3]} ))
|
|
||||||
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
|
||||||
_parseDate_day=$(( 10#${BASH_REMATCH[4]} ))
|
|
||||||
debug "regex match: ${tan}MM-DD-YYYY${purple}"
|
|
||||||
else
|
|
||||||
shopt -u nocasematch
|
|
||||||
return 1
|
return 1
|
||||||
fi
|
}
|
||||||
|
|
||||||
elif [[ "${date}" =~ (.*[^0-9]|^)(([0-9]{1,2})[-\.\/_ ]+([0-9]{1,2})[-\.\/_ ]+([0-9]{2}))([^0-9].*|$) ]]; then
|
local date="${1:-$(date +%F)}"
|
||||||
|
_parseDate_found="" _parseDate_year="" _parseDate_month="" _parseDate_monthName=""
|
||||||
|
_parseDate_day="" _parseDate_hour="" _parseDate_minute=""
|
||||||
|
|
||||||
if [[ $(( 10#${BASH_REMATCH[3]} )) -lt 13 && \
|
shopt -s nocasematch #Use case-insensitive regex
|
||||||
$(( 10#${BASH_REMATCH[4]} )) -gt 12 && \
|
|
||||||
$(( 10#${BASH_REMATCH[4]} )) -lt 32
|
debug "_parseDate_() input ${tan}$date${purple}"
|
||||||
]]; then
|
|
||||||
|
# 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].*|$)"
|
||||||
|
if [[ ${date} =~ $pat ]]; then
|
||||||
|
_parseDate_found="${BASH_REMATCH[2]}"
|
||||||
|
_parseDate_year=$((10#${BASH_REMATCH[3]}))
|
||||||
|
_parseDate_month=$((10#${BASH_REMATCH[4]}))
|
||||||
|
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
||||||
|
_parseDate_day=$((10#${BASH_REMATCH[5]}))
|
||||||
|
debug "regex match: ${tan}YYYY-MM-DD${purple}"
|
||||||
|
|
||||||
|
# Month DD, YYYY
|
||||||
|
elif [[ ${date} =~ ((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
|
||||||
|
_parseDate_found="${BASH_REMATCH[1]:-}"
|
||||||
|
_parseDate_month=$(_monthToNumber_ ${BASH_REMATCH[2]:-})
|
||||||
|
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month:-}")"
|
||||||
|
_parseDate_day=$((10#${BASH_REMATCH[3]:-}))
|
||||||
|
_parseDate_year=$((10#${BASH_REMATCH[5]:-}))
|
||||||
|
debug "regex match: ${tan}Month DD, YYYY${purple}"
|
||||||
|
|
||||||
|
# Month DD, YY
|
||||||
|
elif [[ ${date} =~ ((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
|
||||||
|
_parseDate_found="${BASH_REMATCH[1]}"
|
||||||
|
_parseDate_month=$(_monthToNumber_ ${BASH_REMATCH[2]})
|
||||||
|
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
||||||
|
_parseDate_day=$((10#${BASH_REMATCH[3]}))
|
||||||
|
_parseDate_year="20$((10#${BASH_REMATCH[5]}))"
|
||||||
|
debug "regex match: ${tan}Month DD, YY${purple}"
|
||||||
|
|
||||||
|
# DD Month YYYY
|
||||||
|
elif [[ ${date} =~ (.*[^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
|
||||||
|
_parseDate_found="${BASH_REMATCH[2]}"
|
||||||
|
_parseDate_day=$((10#"${BASH_REMATCH[3]}"))
|
||||||
|
_parseDate_month="$(_monthToNumber_ "${BASH_REMATCH[4]}")"
|
||||||
|
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
||||||
|
_parseDate_year=$((10#"${BASH_REMATCH[5]}"))
|
||||||
|
debug "regex match: ${tan}DD Month, YYYY${purple}"
|
||||||
|
|
||||||
|
# MM-DD-YYYY or DD-MM-YYYY
|
||||||
|
elif [[ ${date} =~ (.*[^0-9]|^)(([ 0-9]{1,2})[-\.\/_ ]+([ 0-9]{1,2})[-\.\/_ ]+(20[0-2][0-9]))([^0-9].*|$) ]]; then
|
||||||
|
|
||||||
|
if [[ $((10#${BASH_REMATCH[3]})) -lt 13 &&
|
||||||
|
$((10#${BASH_REMATCH[4]})) -gt 12 &&
|
||||||
|
$((10#${BASH_REMATCH[4]})) -lt 32 ]] \
|
||||||
|
; then
|
||||||
_parseDate_found="${BASH_REMATCH[2]}"
|
_parseDate_found="${BASH_REMATCH[2]}"
|
||||||
_parseDate_year="20$(( 10#${BASH_REMATCH[5]} ))"
|
_parseDate_year=$((10#${BASH_REMATCH[5]}))
|
||||||
_parseDate_month=$(( 10#${BASH_REMATCH[3]} ))
|
_parseDate_month=$((10#${BASH_REMATCH[3]}))
|
||||||
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
||||||
_parseDate_day=$(( 10#${BASH_REMATCH[4]} ))
|
_parseDate_day=$((10#${BASH_REMATCH[4]}))
|
||||||
debug "regex match: ${tan}MM-DD-YYYY${purple}"
|
debug "regex match: ${tan}MM-DD-YYYY${purple}"
|
||||||
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 ]] \
|
||||||
]]; then
|
; then
|
||||||
_parseDate_found="${BASH_REMATCH[2]}"
|
_parseDate_found="${BASH_REMATCH[2]}"
|
||||||
_parseDate_year="20$(( 10#${BASH_REMATCH[5]} ))"
|
_parseDate_year=$((10#${BASH_REMATCH[5]}))
|
||||||
_parseDate_month=$(( 10#${BASH_REMATCH[4]} ))
|
_parseDate_month=$((10#${BASH_REMATCH[4]}))
|
||||||
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
||||||
_parseDate_day=$(( 10#${BASH_REMATCH[3]} ))
|
_parseDate_day=$((10#${BASH_REMATCH[3]}))
|
||||||
debug "regex match: ${tan}DD-MM-YYYY${purple}"
|
debug "regex match: ${tan}DD-MM-YYYY${purple}"
|
||||||
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
|
||||||
_parseDate_found="${BASH_REMATCH[2]}"
|
_parseDate_found="${BASH_REMATCH[2]}"
|
||||||
_parseDate_year="20$(( 10#${BASH_REMATCH[5]} ))"
|
_parseDate_year=$((10#${BASH_REMATCH[5]}))
|
||||||
_parseDate_month=$(( 10#${BASH_REMATCH[3]} ))
|
_parseDate_month=$((10#${BASH_REMATCH[3]}))
|
||||||
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
||||||
_parseDate_day=$(( 10#${BASH_REMATCH[4]} ))
|
_parseDate_day=$((10#${BASH_REMATCH[4]}))
|
||||||
debug "regex match: ${tan}MM-DD-YYYY${purple}"
|
debug "regex match: ${tan}MM-DD-YYYY${purple}"
|
||||||
else
|
else
|
||||||
shopt -u nocasematch
|
shopt -u nocasematch
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Month, YYYY
|
elif [[ ${date} =~ (.*[^0-9]|^)(([0-9]{1,2})[-\.\/_ ]+([0-9]{1,2})[-\.\/_ ]+([0-9]{2}))([^0-9].*|$) ]]; then
|
||||||
elif [[ "${date}" =~ ((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
|
|
||||||
_parseDate_found="${BASH_REMATCH[1]}"
|
|
||||||
_parseDate_day="1"
|
|
||||||
_parseDate_month="$(_monthToNumber_ "${BASH_REMATCH[2]}")"
|
|
||||||
_parseDate_monthName="$(_numberToMonth_ $_parseDate_month)"
|
|
||||||
_parseDate_year="$(( 10#${BASH_REMATCH[3]} ))"
|
|
||||||
debug "regex match: ${tan}Month, YYYY${purple}"
|
|
||||||
|
|
||||||
# YYYYMMDDHHMM
|
if [[ $((10#${BASH_REMATCH[3]})) -lt 13 &&
|
||||||
elif [[ "${date}" =~ (.*[^0-9]|^)((20[0-2][0-9])([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2}))([^0-9].*|$) ]]; then
|
$((10#${BASH_REMATCH[4]})) -gt 12 &&
|
||||||
_parseDate_found="${BASH_REMATCH[2]}"
|
$((10#${BASH_REMATCH[4]})) -lt 32 ]] \
|
||||||
_parseDate_day="$(( 10#${BASH_REMATCH[5]} ))"
|
; then
|
||||||
_parseDate_month="$(( 10#${BASH_REMATCH[4]} ))"
|
_parseDate_found="${BASH_REMATCH[2]}"
|
||||||
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
_parseDate_year="20$((10#${BASH_REMATCH[5]}))"
|
||||||
_parseDate_year="$(( 10#${BASH_REMATCH[3]} ))"
|
_parseDate_month=$((10#${BASH_REMATCH[3]}))
|
||||||
_parseDate_hour="$(( 10#${BASH_REMATCH[6]} ))"
|
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
||||||
_parseDate_minute="$(( 10#${BASH_REMATCH[7]} ))"
|
_parseDate_day=$((10#${BASH_REMATCH[4]}))
|
||||||
debug "regex match: ${tan}YYYYMMDDHHMM${purple}"
|
debug "regex match: ${tan}MM-DD-YYYY${purple}"
|
||||||
|
elif [[ $((10#${BASH_REMATCH[3]})) -gt 12 &&
|
||||||
|
$((10#${BASH_REMATCH[3]})) -lt 32 &&
|
||||||
|
$((10#${BASH_REMATCH[4]})) -lt 13 ]] \
|
||||||
|
; then
|
||||||
|
_parseDate_found="${BASH_REMATCH[2]}"
|
||||||
|
_parseDate_year="20$((10#${BASH_REMATCH[5]}))"
|
||||||
|
_parseDate_month=$((10#${BASH_REMATCH[4]}))
|
||||||
|
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
||||||
|
_parseDate_day=$((10#${BASH_REMATCH[3]}))
|
||||||
|
debug "regex match: ${tan}DD-MM-YYYY${purple}"
|
||||||
|
elif [[ $((10#${BASH_REMATCH[3]})) -lt 32 &&
|
||||||
|
$((10#${BASH_REMATCH[4]})) -lt 13 ]] \
|
||||||
|
; then
|
||||||
|
_parseDate_found="${BASH_REMATCH[2]}"
|
||||||
|
_parseDate_year="20$((10#${BASH_REMATCH[5]}))"
|
||||||
|
_parseDate_month=$((10#${BASH_REMATCH[3]}))
|
||||||
|
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
||||||
|
_parseDate_day=$((10#${BASH_REMATCH[4]}))
|
||||||
|
debug "regex match: ${tan}MM-DD-YYYY${purple}"
|
||||||
|
else
|
||||||
|
shopt -u nocasematch
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
# YYYYMMDDHH 1 2 3 4 5 6
|
# Month, YYYY
|
||||||
elif [[ "${date}" =~ (.*[^0-9]|^)((20[0-2][0-9])([0-9]{2})([0-9]{2})([0-9]{2}))([^0-9].*|$) ]]; then
|
elif [[ ${date} =~ ((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
|
||||||
_parseDate_found="${BASH_REMATCH[2]}"
|
_parseDate_found="${BASH_REMATCH[1]}"
|
||||||
_parseDate_day="$(( 10#${BASH_REMATCH[5]} ))"
|
_parseDate_day="1"
|
||||||
_parseDate_month="$(( 10#${BASH_REMATCH[4]} ))"
|
_parseDate_month="$(_monthToNumber_ "${BASH_REMATCH[2]}")"
|
||||||
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
_parseDate_monthName="$(_numberToMonth_ $_parseDate_month)"
|
||||||
_parseDate_year="$(( 10#${BASH_REMATCH[3]} ))"
|
_parseDate_year="$((10#${BASH_REMATCH[3]}))"
|
||||||
_parseDate_hour="${BASH_REMATCH[6]}"
|
debug "regex match: ${tan}Month, YYYY${purple}"
|
||||||
_parseDate_minute="00"
|
|
||||||
debug "regex match: ${tan}YYYYMMDDHHMM${purple}"
|
|
||||||
|
|
||||||
# MMDDYYYY or YYYYMMDD or DDMMYYYY
|
# YYYYMMDDHHMM
|
||||||
# 1 2 3 4 5 6
|
elif [[ ${date} =~ (.*[^0-9]|^)((20[0-2][0-9])([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2}))([^0-9].*|$) ]]; then
|
||||||
elif [[ "${date}" =~ (.*[^0-9]|^)(([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2}))([^0-9].*|$) ]]; then
|
_parseDate_found="${BASH_REMATCH[2]}"
|
||||||
|
_parseDate_day="$((10#${BASH_REMATCH[5]}))"
|
||||||
|
_parseDate_month="$((10#${BASH_REMATCH[4]}))"
|
||||||
|
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
||||||
|
_parseDate_year="$((10#${BASH_REMATCH[3]}))"
|
||||||
|
_parseDate_hour="$((10#${BASH_REMATCH[6]}))"
|
||||||
|
_parseDate_minute="$((10#${BASH_REMATCH[7]}))"
|
||||||
|
debug "regex match: ${tan}YYYYMMDDHHMM${purple}"
|
||||||
|
|
||||||
|
# YYYYMMDDHH 1 2 3 4 5 6
|
||||||
|
elif [[ ${date} =~ (.*[^0-9]|^)((20[0-2][0-9])([0-9]{2})([0-9]{2})([0-9]{2}))([^0-9].*|$) ]]; then
|
||||||
|
_parseDate_found="${BASH_REMATCH[2]}"
|
||||||
|
_parseDate_day="$((10#${BASH_REMATCH[5]}))"
|
||||||
|
_parseDate_month="$((10#${BASH_REMATCH[4]}))"
|
||||||
|
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
||||||
|
_parseDate_year="$((10#${BASH_REMATCH[3]}))"
|
||||||
|
_parseDate_hour="${BASH_REMATCH[6]}"
|
||||||
|
_parseDate_minute="00"
|
||||||
|
debug "regex match: ${tan}YYYYMMDDHHMM${purple}"
|
||||||
|
|
||||||
|
# MMDDYYYY or YYYYMMDD or DDMMYYYY
|
||||||
|
# 1 2 3 4 5 6
|
||||||
|
elif [[ ${date} =~ (.*[^0-9]|^)(([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2}))([^0-9].*|$) ]]; then
|
||||||
|
|
||||||
# MMDDYYYY
|
# MMDDYYYY
|
||||||
if [[ $(( 10#${BASH_REMATCH[5]} )) -eq 20 && \
|
if [[ $((10#${BASH_REMATCH[5]})) -eq 20 &&
|
||||||
$(( 10#${BASH_REMATCH[3]} )) -lt 13 && \
|
$((10#${BASH_REMATCH[3]})) -lt 13 &&
|
||||||
$(( 10#${BASH_REMATCH[4]} )) -lt 32
|
$((10#${BASH_REMATCH[4]})) -lt 32 ]] \
|
||||||
]]; then
|
; then
|
||||||
_parseDate_found="${BASH_REMATCH[2]}"
|
_parseDate_found="${BASH_REMATCH[2]}"
|
||||||
_parseDate_day="$(( 10#${BASH_REMATCH[4]} ))"
|
_parseDate_day="$((10#${BASH_REMATCH[4]}))"
|
||||||
_parseDate_month="$(( 10#${BASH_REMATCH[3]} ))"
|
_parseDate_month="$((10#${BASH_REMATCH[3]}))"
|
||||||
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
||||||
_parseDate_year="${BASH_REMATCH[5]}${BASH_REMATCH[6]}"
|
_parseDate_year="${BASH_REMATCH[5]}${BASH_REMATCH[6]}"
|
||||||
debug "regex match: ${tan}MMDDYYYY${purple}"
|
debug "regex match: ${tan}MMDDYYYY${purple}"
|
||||||
# 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 &&
|
||||||
$(( 10#${BASH_REMATCH[3]} )) -lt 32 && \
|
$((10#${BASH_REMATCH[3]})) -lt 32 &&
|
||||||
$(( 10#${BASH_REMATCH[4]} )) -lt 13
|
$((10#${BASH_REMATCH[4]})) -lt 13 ]] \
|
||||||
]]; then
|
; then
|
||||||
_parseDate_found="${BASH_REMATCH[2]}"
|
_parseDate_found="${BASH_REMATCH[2]}"
|
||||||
_parseDate_day="$(( 10#${BASH_REMATCH[3]} ))"
|
_parseDate_day="$((10#${BASH_REMATCH[3]}))"
|
||||||
_parseDate_month="$(( 10#${BASH_REMATCH[4]} ))"
|
_parseDate_month="$((10#${BASH_REMATCH[4]}))"
|
||||||
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
||||||
_parseDate_year="${BASH_REMATCH[5]}${BASH_REMATCH[6]}"
|
_parseDate_year="${BASH_REMATCH[5]}${BASH_REMATCH[6]}"
|
||||||
debug "regex match: ${tan}DDMMYYYY${purple}"
|
debug "regex match: ${tan}DDMMYYYY${purple}"
|
||||||
# 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 &&
|
||||||
&& $(( 10#${BASH_REMATCH[6]} )) -lt 32 \
|
$((10#${BASH_REMATCH[6]})) -lt 32 &&
|
||||||
&& $(( 10#${BASH_REMATCH[5]} )) -lt 13 \
|
$((10#${BASH_REMATCH[5]})) -lt 13 ]] \
|
||||||
]]; then
|
; then
|
||||||
_parseDate_found="${BASH_REMATCH[2]}"
|
_parseDate_found="${BASH_REMATCH[2]}"
|
||||||
_parseDate_day="$(( 10#${BASH_REMATCH[6]} ))"
|
_parseDate_day="$((10#${BASH_REMATCH[6]}))"
|
||||||
_parseDate_month="$(( 10#${BASH_REMATCH[5]} ))"
|
_parseDate_month="$((10#${BASH_REMATCH[5]}))"
|
||||||
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
||||||
_parseDate_year="${BASH_REMATCH[3]}${BASH_REMATCH[4]}"
|
_parseDate_year="${BASH_REMATCH[3]}${BASH_REMATCH[4]}"
|
||||||
debug "regex match: ${tan}YYYYMMDD${purple}"
|
debug "regex match: ${tan}YYYYMMDD${purple}"
|
||||||
# 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 &&
|
||||||
&& $(( 10#${BASH_REMATCH[5]} )) -lt 32 \
|
$((10#${BASH_REMATCH[5]})) -lt 32 &&
|
||||||
&& $(( 10#${BASH_REMATCH[6]} )) -lt 13 \
|
$((10#${BASH_REMATCH[6]})) -lt 13 ]] \
|
||||||
]]; then
|
; then
|
||||||
_parseDate_found="${BASH_REMATCH[2]}"
|
_parseDate_found="${BASH_REMATCH[2]}"
|
||||||
_parseDate_day="$(( 10#${BASH_REMATCH[5]} ))"
|
_parseDate_day="$((10#${BASH_REMATCH[5]}))"
|
||||||
_parseDate_month="$(( 10#${BASH_REMATCH[6]} ))"
|
_parseDate_month="$((10#${BASH_REMATCH[6]}))"
|
||||||
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
||||||
_parseDate_year="${BASH_REMATCH[3]}${BASH_REMATCH[4]}"
|
_parseDate_year="${BASH_REMATCH[3]}${BASH_REMATCH[4]}"
|
||||||
debug "regex match: ${tan}YYYYMMDD${purple}"
|
debug "regex match: ${tan}YYYYMMDD${purple}"
|
||||||
# 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 &&
|
||||||
&& $(( 10#${BASH_REMATCH[5]} )) -lt 13 \
|
$((10#${BASH_REMATCH[5]})) -lt 13 ]] \
|
||||||
]]; then
|
; then
|
||||||
_parseDate_found="${BASH_REMATCH[2]}"
|
_parseDate_found="${BASH_REMATCH[2]}"
|
||||||
_parseDate_day="$(( 10#${BASH_REMATCH[6]} ))"
|
_parseDate_day="$((10#${BASH_REMATCH[6]}))"
|
||||||
_parseDate_month="$(( 10#${BASH_REMATCH[5]} ))"
|
_parseDate_month="$((10#${BASH_REMATCH[5]}))"
|
||||||
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
||||||
_parseDate_year="${BASH_REMATCH[3]}${BASH_REMATCH[4]}"
|
_parseDate_year="${BASH_REMATCH[3]}${BASH_REMATCH[4]}"
|
||||||
debug "regex match: ${tan}YYYYMMDD${purple}"
|
debug "regex match: ${tan}YYYYMMDD${purple}"
|
||||||
else
|
else
|
||||||
shopt -u nocasematch
|
shopt -u nocasematch
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# # MMDD or DDYY
|
# # MMDD or DDYY
|
||||||
# elif [[ "$date" =~ .*(([0-9]{2})([0-9]{2})).* ]]; then
|
# elif [[ "$date" =~ .*(([0-9]{2})([0-9]{2})).* ]]; then
|
||||||
# debug "regex match: ${tan}MMDD or DDMM${purple}"
|
# debug "regex match: ${tan}MMDD or DDMM${purple}"
|
||||||
# _parseDate_found="${BASH_REMATCH[1]}"
|
# _parseDate_found="${BASH_REMATCH[1]}"
|
||||||
|
|
||||||
|
# # Figure out if days are months or vice versa
|
||||||
|
# if [[ $(( 10#${BASH_REMATCH[2]} )) -gt 12 \
|
||||||
|
# && $(( 10#${BASH_REMATCH[2]} )) -lt 32 \
|
||||||
|
# && $(( 10#${BASH_REMATCH[3]} )) -lt 13 \
|
||||||
|
# ]]; then
|
||||||
|
# _parseDate_day="$(( 10#${BASH_REMATCH[2]} ))"
|
||||||
|
# _parseDate_month="$(( 10#${BASH_REMATCH[3]} ))"
|
||||||
|
# _parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
||||||
|
# _parseDate_year="$(date +%Y )"
|
||||||
|
# elif [[ $(( 10#${BASH_REMATCH[2]} )) -lt 13 \
|
||||||
|
# && $(( 10#${BASH_REMATCH[3]} )) -lt 32 \
|
||||||
|
# ]]; then
|
||||||
|
# _parseDate_day="$(( 10#${BASH_REMATCH[3]} ))"
|
||||||
|
# _parseDate_month="$(( 10#${BASH_REMATCH[2]} ))"
|
||||||
|
# _parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
||||||
|
# _parseDate_year="$(date +%Y )"
|
||||||
|
# else
|
||||||
|
# shopt -u nocasematch
|
||||||
|
# return 1
|
||||||
|
# fi
|
||||||
|
else
|
||||||
|
shopt -u nocasematch
|
||||||
|
return 1
|
||||||
|
|
||||||
|
fi
|
||||||
|
|
||||||
|
[[ -z ${_parseDate_year:-} ]] && {
|
||||||
|
shopt -u nocasematch
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
((_parseDate_month >= 1 && _parseDate_month <= 12)) || {
|
||||||
|
shopt -u nocasematch
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
((_parseDate_day >= 1 && _parseDate_day <= 31)) || {
|
||||||
|
shopt -u nocasematch
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
debug "${tan}\$_parseDate_found: ${_parseDate_found}${purple}"
|
||||||
|
debug "${tan}\$_parseDate_year: ${_parseDate_year}${purple}"
|
||||||
|
debug "${tan}\$_parseDate_month: ${_parseDate_month}${purple}"
|
||||||
|
debug "${tan}\$_parseDate_monthName: ${_parseDate_monthName}${purple}"
|
||||||
|
debug "${tan}\$_parseDate_day: ${_parseDate_day}${purple}"
|
||||||
|
[[ -z ${_parseDate_hour:-} ]] || debug "${tan}\$_parseDate_hour: ${_parseDate_hour}${purple}"
|
||||||
|
[[ -z ${_parseDate_minute:-} ]] || debug "${tan}\$_parseDate_minute: ${_parseDate_minute}${purple}"
|
||||||
|
|
||||||
# # Figure out if days are months or vice versa
|
|
||||||
# if [[ $(( 10#${BASH_REMATCH[2]} )) -gt 12 \
|
|
||||||
# && $(( 10#${BASH_REMATCH[2]} )) -lt 32 \
|
|
||||||
# && $(( 10#${BASH_REMATCH[3]} )) -lt 13 \
|
|
||||||
# ]]; then
|
|
||||||
# _parseDate_day="$(( 10#${BASH_REMATCH[2]} ))"
|
|
||||||
# _parseDate_month="$(( 10#${BASH_REMATCH[3]} ))"
|
|
||||||
# _parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
|
||||||
# _parseDate_year="$(date +%Y )"
|
|
||||||
# elif [[ $(( 10#${BASH_REMATCH[2]} )) -lt 13 \
|
|
||||||
# && $(( 10#${BASH_REMATCH[3]} )) -lt 32 \
|
|
||||||
# ]]; then
|
|
||||||
# _parseDate_day="$(( 10#${BASH_REMATCH[3]} ))"
|
|
||||||
# _parseDate_month="$(( 10#${BASH_REMATCH[2]} ))"
|
|
||||||
# _parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
|
||||||
# _parseDate_year="$(date +%Y )"
|
|
||||||
# else
|
|
||||||
# shopt -u nocasematch
|
|
||||||
# return 1
|
|
||||||
# fi
|
|
||||||
else
|
|
||||||
shopt -u nocasematch
|
shopt -u nocasematch
|
||||||
return 1
|
|
||||||
|
|
||||||
fi
|
# Output results for BATS tests
|
||||||
|
if [ "${automated_test_in_progress:-}" ]; then
|
||||||
[[ -z ${_parseDate_year:-} ]] && { shopt -u nocasematch; return 1 ; }
|
echo "_parseDate_found: ${_parseDate_found}"
|
||||||
(( _parseDate_month >= 1 && _parseDate_month <= 12 )) || { shopt -u nocasematch; return 1 ; }
|
echo "_parseDate_year: ${_parseDate_year}"
|
||||||
(( _parseDate_day >= 1 && _parseDate_day <= 31 )) || { shopt -u nocasematch; return 1 ; }
|
echo "_parseDate_month: ${_parseDate_month}"
|
||||||
|
echo "_parseDate_monthName: ${_parseDate_monthName}"
|
||||||
debug "${tan}\$_parseDate_found: ${_parseDate_found}${purple}"
|
echo "_parseDate_day: ${_parseDate_day}"
|
||||||
debug "${tan}\$_parseDate_year: ${_parseDate_year}${purple}"
|
echo "_parseDate_hour: ${_parseDate_hour}"
|
||||||
debug "${tan}\$_parseDate_month: ${_parseDate_month}${purple}"
|
echo "_parseDate_minute: ${_parseDate_minute}"
|
||||||
debug "${tan}\$_parseDate_monthName: ${_parseDate_monthName}${purple}"
|
fi
|
||||||
debug "${tan}\$_parseDate_day: ${_parseDate_day}${purple}"
|
|
||||||
[[ -z ${_parseDate_hour:-} ]] || debug "${tan}\$_parseDate_hour: ${_parseDate_hour}${purple}"
|
|
||||||
[[ -z ${_parseDate_minute:-} ]] || debug "${tan}\$_parseDate_minute: ${_parseDate_minute}${purple}"
|
|
||||||
|
|
||||||
shopt -u nocasematch
|
|
||||||
|
|
||||||
# Output results for BATS tests
|
|
||||||
if [ "${automated_test_in_progress:-}" ]; then
|
|
||||||
echo "_parseDate_found: ${_parseDate_found}"
|
|
||||||
echo "_parseDate_year: ${_parseDate_year}"
|
|
||||||
echo "_parseDate_month: ${_parseDate_month}"
|
|
||||||
echo "_parseDate_monthName: ${_parseDate_monthName}"
|
|
||||||
echo "_parseDate_day: ${_parseDate_day}"
|
|
||||||
echo "_parseDate_hour: ${_parseDate_hour}"
|
|
||||||
echo "_parseDate_minute: ${_parseDate_minute}"
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_formatDate_() {
|
_formatDate_() {
|
||||||
# DESC: Reformats dates into user specified formats
|
# DESC: Reformats dates into user specified formats
|
||||||
# ARGS: $1 (Required) - Date to be formatted
|
# ARGS: $1 (Required) - Date to be formatted
|
||||||
# $2 (Optional) - Format in any format accepted by bash's date command. Examples listed below.
|
# $2 (Optional) - Format in any format accepted by bash's date command. Examples listed below.
|
||||||
# %F - YYYY-MM-DD
|
# %F - YYYY-MM-DD
|
||||||
# %D - MM/DD/YY
|
# %D - MM/DD/YY
|
||||||
# %a - Name of weekday in short (like Sun, Mon, Tue, Wed, Thu, Fri, Sat)
|
# %a - Name of weekday in short (like Sun, Mon, Tue, Wed, Thu, Fri, Sat)
|
||||||
# %A - Name of weekday in full (like Sunday, Monday, Tuesday)
|
# %A - Name of weekday in full (like Sunday, Monday, Tuesday)
|
||||||
# '+%m %d, %Y' - 12 27, 2019
|
# '+%m %d, %Y' - 12 27, 2019
|
||||||
# OUTS: Echo result to STDOUT
|
# OUTS: Echo result to STDOUT
|
||||||
# USAGE: _formatDate_ "Jan 10, 2019" "%D"
|
# USAGE: _formatDate_ "Jan 10, 2019" "%D"
|
||||||
# NOTE: Defaults to YYYY-MM-DD or $(date +%F)
|
# NOTE: Defaults to YYYY-MM-DD or $(date +%F)
|
||||||
|
|
||||||
[[ $# -eq 0 ]] && {
|
[[ $# -eq 0 ]] && {
|
||||||
error 'Missing required argument to _formatDate_()'
|
error 'Missing required argument to _formatDate_()'
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
local d="${1}"
|
local d="${1}"
|
||||||
local format="${2:-%F}"
|
local format="${2:-%F}"
|
||||||
format="${format//+/}"
|
format="${format//+/}"
|
||||||
|
|
||||||
if command -v gdate >/dev/null 2>&1; then
|
if command -v gdate >/dev/null 2>&1; then
|
||||||
gdate -d "${d}" "+${format}"
|
gdate -d "${d}" "+${format}"
|
||||||
else
|
else
|
||||||
date -d "${d}" "+${format}"
|
date -d "${d}" "+${format}"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -1,39 +1,39 @@
|
|||||||
# Functions for use on computers running MacOS
|
# Functions for use on computers running MacOS
|
||||||
|
|
||||||
_haveScriptableFinder_() {
|
_haveScriptableFinder_() {
|
||||||
# DESC: Determine whether we can script the Finder or not
|
# DESC: Determine whether we can script the Finder or not
|
||||||
# ARGS: None
|
# ARGS: None
|
||||||
# OUTS: true/false
|
# OUTS: true/false
|
||||||
|
|
||||||
local finder_pid
|
local finder_pid
|
||||||
finder_pid="$(pgrep -f /System/Library/CoreServices/Finder.app | head -n 1)"
|
finder_pid="$(pgrep -f /System/Library/CoreServices/Finder.app | head -n 1)"
|
||||||
|
|
||||||
if [[ (${finder_pid} -gt 1) && ("${STY-}" == "") ]]; then
|
if [[ (${finder_pid} -gt 1) && (${STY-} == "") ]]; then
|
||||||
return 0
|
return 0
|
||||||
else
|
else
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
_guiInput_() {
|
_guiInput_() {
|
||||||
# DESC: Ask for user input using a Mac dialog box
|
# DESC: Ask for user input using a Mac dialog box
|
||||||
# ARGS: $1 (Optional) - Text in dialogue box (Default: Password)
|
# ARGS: $1 (Optional) - Text in dialogue box (Default: Password)
|
||||||
# OUTS: None
|
# OUTS: None
|
||||||
# NOTE: https://github.com/herrbischoff/awesome-osx-command-line/blob/master/functions.md
|
# NOTE: https://github.com/herrbischoff/awesome-osx-command-line/blob/master/functions.md
|
||||||
if _haveScriptableFinder_; then
|
if _haveScriptableFinder_; then
|
||||||
guiPrompt="${1:-Password:}"
|
guiPrompt="${1:-Password:}"
|
||||||
guiInput=$(
|
guiInput=$(
|
||||||
osascript &>/dev/null <<EOF
|
osascript &>/dev/null <<EOF
|
||||||
tell application "System Events"
|
tell application "System Events"
|
||||||
activate
|
activate
|
||||||
text returned of (display dialog "${guiPrompt}" default answer "" with hidden answer)
|
text returned of (display dialog "${guiPrompt}" default answer "" with hidden answer)
|
||||||
end tell
|
end tell
|
||||||
EOF
|
EOF
|
||||||
)
|
)
|
||||||
echo -n "${guiInput}"
|
echo -n "${guiInput}"
|
||||||
else
|
else
|
||||||
error "No GUI input without macOS"
|
error "No GUI input without macOS"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,71 +1,73 @@
|
|||||||
|
|
||||||
_fromSeconds_() {
|
_fromSeconds_() {
|
||||||
# DESC: Convert seconds to HH:MM:SS
|
# DESC: Convert seconds to HH:MM:SS
|
||||||
# ARGS: $1 (Required) - Time in seconds
|
# ARGS: $1 (Required) - Time in seconds
|
||||||
# OUTS: Print HH:MM:SS to STDOUT
|
# OUTS: Print HH:MM:SS to STDOUT
|
||||||
# USAGE: _convertSecs_ "SECONDS"
|
# USAGE: _convertSecs_ "SECONDS"
|
||||||
# To compute the time it takes a script to run:
|
# To compute the time it takes a script to run:
|
||||||
# STARTTIME=$(date +"%s")
|
# STARTTIME=$(date +"%s")
|
||||||
# ENDTIME=$(date +"%s")
|
# ENDTIME=$(date +"%s")
|
||||||
# TOTALTIME=$(($ENDTIME-$STARTTIME)) # human readable time
|
# TOTALTIME=$(($ENDTIME-$STARTTIME)) # human readable time
|
||||||
# _convertSecs_ "$TOTALTIME"
|
# _convertSecs_ "$TOTALTIME"
|
||||||
|
|
||||||
((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
|
||||||
}
|
}
|
||||||
|
|
||||||
_toSeconds_() {
|
_toSeconds_() {
|
||||||
# DESC: Converts HH:MM:SS to seconds
|
# DESC: Converts HH:MM:SS to seconds
|
||||||
# ARGS: $1 (Required) - Time in HH:MM:SS
|
# ARGS: $1 (Required) - Time in HH:MM:SS
|
||||||
# OUTS: Print seconds to STDOUT
|
# OUTS: Print seconds to STDOUT
|
||||||
# USAGE: _toSeconds_ "01:00:00"
|
# USAGE: _toSeconds_ "01:00:00"
|
||||||
# NOTE: Acceptable Input Formats
|
# NOTE: Acceptable Input Formats
|
||||||
# 24 12 09
|
# 24 12 09
|
||||||
# 12,12,09
|
# 12,12,09
|
||||||
# 12;12;09
|
# 12;12;09
|
||||||
# 12:12:09
|
# 12:12:09
|
||||||
# 12-12-09
|
# 12-12-09
|
||||||
# 12H12M09S
|
# 12H12M09S
|
||||||
# 12h12m09s
|
# 12h12m09s
|
||||||
|
|
||||||
local saveIFS
|
local saveIFS
|
||||||
|
local h
|
||||||
|
local m
|
||||||
|
local s
|
||||||
|
|
||||||
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"
|
||||||
m="$2"
|
m="$2"
|
||||||
s="$3"
|
s="$3"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo $(( 10#$h * 3600 + 10#$m * 60 + 10#$s ))
|
echo $((10#$h * 3600 + 10#$m * 60 + 10#$s))
|
||||||
}
|
}
|
||||||
|
|
||||||
_countdown_() {
|
_countdown_() {
|
||||||
# DESC: Sleep for a specified amount of time
|
# DESC: Sleep for a specified amount of time
|
||||||
# ARGS: $1 (Optional) - Total seconds to sleep for(Default is 10)
|
# ARGS: $1 (Optional) - Total seconds to sleep for(Default is 10)
|
||||||
# $2 (Optional) - Increment to count down
|
# $2 (Optional) - Increment to count down
|
||||||
# $3 (Optional) - Message to print at each increment (default is ...)
|
# $3 (Optional) - Message to print at each increment (default is ...)
|
||||||
# OUTS: None
|
# OUTS: None
|
||||||
# USAGE: _countdown_ 10 1 "Waiting for cache to invalidate"
|
# USAGE: _countdown_ 10 1 "Waiting for cache to invalidate"
|
||||||
|
|
||||||
local i ii t
|
local i ii t
|
||||||
local n=${1:-10}
|
local n=${1:-10}
|
||||||
local stime=${2:-1}
|
local stime=${2:-1}
|
||||||
local message="${3:-...}"
|
local message="${3:-...}"
|
||||||
((t = n + 1))
|
((t = n + 1))
|
||||||
|
|
||||||
for ((i = 1; i <= n; i++)); do
|
for ((i = 1; i <= n; i++)); do
|
||||||
((ii = t - i))
|
((ii = t - i))
|
||||||
if declare -f "info" &>/dev/null 2>&1; then
|
if declare -f "info" &>/dev/null 2>&1; then
|
||||||
info "${message} ${ii}"
|
info "${message} ${ii}"
|
||||||
else
|
else
|
||||||
echo "${message} ${ii}"
|
echo "${message} ${ii}"
|
||||||
fi
|
fi
|
||||||
sleep $stime
|
sleep $stime
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,148 +1,148 @@
|
|||||||
_haveInternet_() {
|
_haveInternet_() {
|
||||||
# DESC: Tests to see if there is an active Internet connection
|
# DESC: Tests to see if there is an active Internet connection
|
||||||
# ARGS: None
|
# ARGS: None
|
||||||
# OUTS: None
|
# OUTS: None
|
||||||
# USAGE: _haveInternet_ && [SOMETHING]
|
# USAGE: _haveInternet_ && [SOMETHING]
|
||||||
# NOTE: https://stackoverflow.com/questions/929368/
|
# NOTE: https://stackoverflow.com/questions/929368/
|
||||||
|
|
||||||
if command -v fping &>/dev/null; then
|
if command -v fping &>/dev/null; then
|
||||||
fping 1.1.1.1 &>/dev/null \
|
fping 1.1.1.1 &>/dev/null \
|
||||||
&& return 0 \
|
&& return 0 \
|
||||||
|| return 1
|
|| return 1
|
||||||
elif ping -t 2 -c 1 1 1.1.1.1 &>/dev/null; then
|
elif ping -t 2 -c 1 1 1.1.1.1 &>/dev/null; then
|
||||||
return 0
|
return 0
|
||||||
elif command -v route &>/dev/null; then
|
elif command -v route &>/dev/null; then
|
||||||
local GATEWAY="$(route -n get default | grep gateway)"
|
local GATEWAY="$(route -n get default | grep gateway)"
|
||||||
ping -t 2 -c 1 "$(echo "${GATEWAY}" | cut -d ':' -f 2)" &>/dev/null \
|
ping -t 2 -c 1 "$(echo "${GATEWAY}" | cut -d ':' -f 2)" &>/dev/null \
|
||||||
&& return 0 \
|
&& return 0 \
|
||||||
|| return 1
|
|| return 1
|
||||||
elif command -v ip &>/dev/null; then
|
elif command -v ip &>/dev/null; then
|
||||||
ping -t 2 -c 1 "$(ip r | grep default | cut -d ' ' -f 3)" &>/dev/null \
|
ping -t 2 -c 1 "$(ip r | grep default | cut -d ' ' -f 3)" &>/dev/null \
|
||||||
&& return 0 \
|
&& return 0 \
|
||||||
|| return 1
|
|| return 1
|
||||||
else
|
else
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
_httpStatus_() {
|
_httpStatus_() {
|
||||||
# DESC: Report the HTTP status of a specified URL
|
# DESC: Report the HTTP status of a specified URL
|
||||||
# ARGS: $1 (Required) - URL (will work fine without https:// prefix)
|
# ARGS: $1 (Required) - URL (will work fine without https:// prefix)
|
||||||
# $2 (Optional) - Seconds to wait until timeout (Default is 3)
|
# $2 (Optional) - Seconds to wait until timeout (Default is 3)
|
||||||
# $3 (Optional) - either '--code' or '--status' (default)
|
# $3 (Optional) - either '--code' or '--status' (default)
|
||||||
# $4 (optional) - CURL opts separated by spaces (Use -L to follow redirects)
|
# $4 (optional) - CURL opts separated by spaces (Use -L to follow redirects)
|
||||||
# OUTS: Prints output to STDOUT
|
# OUTS: Prints output to STDOUT
|
||||||
# USAGE: _httpStatus_ URL [timeout] [--code or --status] [curl opts]
|
# USAGE: _httpStatus_ URL [timeout] [--code or --status] [curl opts]
|
||||||
# NOTE: https://gist.github.com/rsvp/1171304
|
# NOTE: https://gist.github.com/rsvp/1171304
|
||||||
#
|
#
|
||||||
# Example: $ _httpStatus_ bit.ly
|
# Example: $ _httpStatus_ bit.ly
|
||||||
# 301 Redirection: Moved Permanently
|
# 301 Redirection: Moved Permanently
|
||||||
#
|
#
|
||||||
# Example: $ _httpStatus_ www.google.com 100 --code
|
# Example: $ _httpStatus_ www.google.com 100 --code
|
||||||
local code
|
local code
|
||||||
local status
|
local status
|
||||||
|
|
||||||
local saveIFS=${IFS}
|
local saveIFS=${IFS}
|
||||||
IFS=$' \n\t'
|
IFS=$' \n\t'
|
||||||
|
|
||||||
local url=${1:?_httpStatus_ needs an url}
|
local url=${1:?_httpStatus_ needs an url}
|
||||||
local timeout=${2:-'3'}
|
local timeout=${2:-'3'}
|
||||||
local flag=${3:-'--status'}
|
local flag=${3:-'--status'}
|
||||||
local arg4=${4:-''}
|
local arg4=${4:-''}
|
||||||
local arg5=${5:-''}
|
local arg5=${5:-''}
|
||||||
local arg6=${6:-''}
|
local arg6=${6:-''}
|
||||||
local arg7=${7:-''}
|
local arg7=${7:-''}
|
||||||
local curlops="${arg4} ${arg5} ${arg6} ${arg7}"
|
local curlops="${arg4} ${arg5} ${arg6} ${arg7}"
|
||||||
|
|
||||||
# __________ get the CODE which is numeric:
|
# __________ get the CODE which is numeric:
|
||||||
code=$(echo "$(curl --write-out %{http_code} --silent --connect-timeout "${timeout}" \
|
code=$(echo "$(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
|
||||||
000) status="Not responding within ${timeout} seconds" ;;
|
000) status="Not responding within ${timeout} seconds" ;;
|
||||||
100) status="Informational: Continue" ;;
|
100) status="Informational: Continue" ;;
|
||||||
101) status="Informational: Switching Protocols" ;;
|
101) status="Informational: Switching Protocols" ;;
|
||||||
200) status="Successful: OK within ${timeout} seconds" ;;
|
200) status="Successful: OK within ${timeout} seconds" ;;
|
||||||
201) status="Successful: Created" ;;
|
201) status="Successful: Created" ;;
|
||||||
202) status="Successful: Accepted" ;;
|
202) status="Successful: Accepted" ;;
|
||||||
203) status="Successful: Non-Authoritative Information" ;;
|
203) status="Successful: Non-Authoritative Information" ;;
|
||||||
204) status="Successful: No Content" ;;
|
204) status="Successful: No Content" ;;
|
||||||
205) status="Successful: Reset Content" ;;
|
205) status="Successful: Reset Content" ;;
|
||||||
206) status="Successful: Partial Content" ;;
|
206) status="Successful: Partial Content" ;;
|
||||||
300) status="Redirection: Multiple Choices" ;;
|
300) status="Redirection: Multiple Choices" ;;
|
||||||
301) status="Redirection: Moved Permanently" ;;
|
301) status="Redirection: Moved Permanently" ;;
|
||||||
302) status="Redirection: Found residing temporarily under different URI" ;;
|
302) status="Redirection: Found residing temporarily under different URI" ;;
|
||||||
303) status="Redirection: See Other" ;;
|
303) status="Redirection: See Other" ;;
|
||||||
304) status="Redirection: Not Modified" ;;
|
304) status="Redirection: Not Modified" ;;
|
||||||
305) status="Redirection: Use Proxy" ;;
|
305) status="Redirection: Use Proxy" ;;
|
||||||
306) status="Redirection: status not defined" ;;
|
306) status="Redirection: status not defined" ;;
|
||||||
307) status="Redirection: Temporary Redirect" ;;
|
307) status="Redirection: Temporary Redirect" ;;
|
||||||
400) status="Client Error: Bad Request" ;;
|
400) status="Client Error: Bad Request" ;;
|
||||||
401) status="Client Error: Unauthorized" ;;
|
401) status="Client Error: Unauthorized" ;;
|
||||||
402) status="Client Error: Payment Required" ;;
|
402) status="Client Error: Payment Required" ;;
|
||||||
403) status="Client Error: Forbidden" ;;
|
403) status="Client Error: Forbidden" ;;
|
||||||
404) status="Client Error: Not Found" ;;
|
404) status="Client Error: Not Found" ;;
|
||||||
405) status="Client Error: Method Not Allowed" ;;
|
405) status="Client Error: Method Not Allowed" ;;
|
||||||
406) status="Client Error: Not Acceptable" ;;
|
406) status="Client Error: Not Acceptable" ;;
|
||||||
407) status="Client Error: Proxy Authentication Required" ;;
|
407) status="Client Error: Proxy Authentication Required" ;;
|
||||||
408) status="Client Error: Request Timeout within ${timeout} seconds" ;;
|
408) status="Client Error: Request Timeout within ${timeout} seconds" ;;
|
||||||
409) status="Client Error: Conflict" ;;
|
409) status="Client Error: Conflict" ;;
|
||||||
410) status="Client Error: Gone" ;;
|
410) status="Client Error: Gone" ;;
|
||||||
411) status="Client Error: Length Required" ;;
|
411) status="Client Error: Length Required" ;;
|
||||||
412) status="Client Error: Precondition Failed" ;;
|
412) status="Client Error: Precondition Failed" ;;
|
||||||
413) status="Client Error: Request Entity Too Large" ;;
|
413) status="Client Error: Request Entity Too Large" ;;
|
||||||
414) status="Client Error: Request-URI Too Long" ;;
|
414) status="Client Error: Request-URI Too Long" ;;
|
||||||
415) status="Client Error: Unsupported Media Type" ;;
|
415) status="Client Error: Unsupported Media Type" ;;
|
||||||
416) status="Client Error: Requested Range Not Satisfiable" ;;
|
416) status="Client Error: Requested Range Not Satisfiable" ;;
|
||||||
417) status="Client Error: Expectation Failed" ;;
|
417) status="Client Error: Expectation Failed" ;;
|
||||||
500) status="Server Error: Internal Server Error" ;;
|
500) status="Server Error: Internal Server Error" ;;
|
||||||
501) status="Server Error: Not Implemented" ;;
|
501) status="Server Error: Not Implemented" ;;
|
||||||
502) status="Server Error: Bad Gateway" ;;
|
502) status="Server Error: Bad Gateway" ;;
|
||||||
503) status="Server Error: Service Unavailable" ;;
|
503) status="Server Error: Service Unavailable" ;;
|
||||||
504) status="Server Error: Gateway Timeout within ${timeout} seconds" ;;
|
504) status="Server Error: Gateway Timeout within ${timeout} seconds" ;;
|
||||||
505) status="Server Error: HTTP Version Not Supported" ;;
|
505) status="Server Error: HTTP Version Not Supported" ;;
|
||||||
*) die "httpstatus: status not defined." ;;
|
*) die "httpstatus: status not defined." ;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
case ${flag} in
|
case ${flag} in
|
||||||
--status) echo "${code} ${status}" ;;
|
--status) echo "${code} ${status}" ;;
|
||||||
-s) echo "${code} ${status}" ;;
|
-s) echo "${code} ${status}" ;;
|
||||||
--code) echo "${code}" ;;
|
--code) echo "${code}" ;;
|
||||||
-c) echo "${code}" ;;
|
-c) echo "${code}" ;;
|
||||||
*) echo " httpstatus: bad flag" && _safeExit_ ;;
|
*) echo " httpstatus: bad flag" && _safeExit_ ;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
IFS="${saveIFS}"
|
IFS="${saveIFS}"
|
||||||
}
|
}
|
||||||
|
|
||||||
_pushover_() {
|
_pushover_() {
|
||||||
# DESC: Sends a notification via Pushover
|
# DESC: Sends a notification via Pushover
|
||||||
# ARGS: $1 (Required) - Title of notification
|
# ARGS: $1 (Required) - Title of notification
|
||||||
# $2 (Required) - Body of notification
|
# $2 (Required) - Body of notification
|
||||||
# OUTS: None
|
# OUTS: None
|
||||||
# USAGE: _pushover_ "Title Goes Here" "Message Goes Here"
|
# USAGE: _pushover_ "Title Goes Here" "Message Goes Here"
|
||||||
# NOTE: The variables for the two API Keys must have valid values
|
# NOTE: The variables for the two API Keys must have valid values
|
||||||
# Credit: http://ryonsherman.blogspot.com/2012/10/shell-script-to-send-pushover.html
|
# Credit: http://ryonsherman.blogspot.com/2012/10/shell-script-to-send-pushover.html
|
||||||
|
|
||||||
local PUSHOVERURL
|
local PUSHOVERURL
|
||||||
local API_KEY
|
local API_KEY
|
||||||
local USER_KEY
|
local USER_KEY
|
||||||
local DEVICE
|
local DEVICE
|
||||||
local TITLE
|
local TITLE
|
||||||
local MESSAGE
|
local MESSAGE
|
||||||
|
|
||||||
PUSHOVERURL="https://api.pushover.net/1/messages.json"
|
PUSHOVERURL="https://api.pushover.net/1/messages.json"
|
||||||
API_KEY="${PUSHOVER_API_KEY}"
|
API_KEY="${PUSHOVER_API_KEY}"
|
||||||
USER_KEY="${PUSHOVER_USER_KEY}"
|
USER_KEY="${PUSHOVER_USER_KEY}"
|
||||||
DEVICE=""
|
DEVICE=""
|
||||||
TITLE="${1}"
|
TITLE="${1}"
|
||||||
MESSAGE="${2}"
|
MESSAGE="${2}"
|
||||||
curl \
|
curl \
|
||||||
-F "token=${API_KEY}" \
|
-F "token=${API_KEY}" \
|
||||||
-F "user=${USER_KEY}" \
|
-F "user=${USER_KEY}" \
|
||||||
-F "device=${DEVICE}" \
|
-F "device=${DEVICE}" \
|
||||||
-F "title=${TITLE}" \
|
-F "title=${TITLE}" \
|
||||||
-F "message=${MESSAGE}" \
|
-F "message=${MESSAGE}" \
|
||||||
"${PUSHOVERURL}" >/dev/null 2>&1
|
"${PUSHOVERURL}" >/dev/null 2>&1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,288 +2,292 @@
|
|||||||
# Some were adapted from https://github.com/jmcantrell/bashful
|
# Some were adapted from https://github.com/jmcantrell/bashful
|
||||||
|
|
||||||
_cleanString_() {
|
_cleanString_() {
|
||||||
# DESC: Cleans a string of text
|
# DESC: Cleans a string of text
|
||||||
# ARGS: $1 (Required) - String to be cleaned
|
# ARGS: $1 (Required) - String to be cleaned
|
||||||
# $2 (optional) - Specific characters to be cleaned (separated by commas,
|
# $2 (optional) - Specific characters to be removed (separated by commas,
|
||||||
# escape regex special chars)
|
# escape regex special chars)
|
||||||
# OPTS: -l Forces all text to lowercase
|
# OPTS: -l Forces all text to lowercase
|
||||||
# -u Forces all text to uppercase
|
# -u Forces all text to uppercase
|
||||||
# -a Removes all non-alphanumeric characters except for spaces and dashes
|
# -a Removes all non-alphanumeric characters except for spaces and dashes
|
||||||
# -p Replace one character with another (separated by commas) (escape regex characters)
|
# -p Replace one character with another (separated by commas) (escape regex characters)
|
||||||
# -s In combination with -a, replaces characters with a space
|
# -s In combination with -a, replaces characters with a space
|
||||||
# OUTS: Prints result to STDOUT
|
# OUTS: Prints result to STDOUT
|
||||||
# USAGE: _cleanString_ [OPT] [STRING] [CHARS TO REPLACE]
|
# USAGE: _cleanString_ [OPT] [STRING] [CHARS TO REMOVE]
|
||||||
# _cleanString_ -p " ,-" [STRING] [CHARS TO REPLACE]
|
# _cleanString_ -p " ,-" [STRING] [CHARS TO REMOVE]
|
||||||
# NOTES: Always cleaned:
|
# NOTES: Always cleaned:
|
||||||
# - leading white space
|
# - leading white space
|
||||||
# - trailing white space
|
# - trailing white space
|
||||||
# - multiple spaces become a single space
|
# - multiple spaces become a single space
|
||||||
# - remove spaces before and aftrer -_
|
# - remove spaces before and aftrer -_
|
||||||
|
|
||||||
local opt
|
local opt
|
||||||
local lc=false
|
local lc=false
|
||||||
local uc=false
|
local uc=false
|
||||||
local alphanumeric=false
|
local alphanumeric=false
|
||||||
local replace=false
|
local replace=false
|
||||||
local us=false
|
local us=false
|
||||||
|
|
||||||
local OPTIND=1
|
local OPTIND=1
|
||||||
while getopts ":lLuUaAsSpP" opt; do
|
while getopts ":lLuUaAsSpP" opt; do
|
||||||
case $opt in
|
case $opt in
|
||||||
l | L) lc=true ;;
|
l | L) lc=true ;;
|
||||||
u | U) uc=true ;;
|
u | U) uc=true ;;
|
||||||
a | A) alphanumeric=true ;;
|
a | A) alphanumeric=true ;;
|
||||||
s | S) us=true ;;
|
s | S) us=true ;;
|
||||||
p | P)
|
p | P)
|
||||||
shift
|
shift
|
||||||
local pairs=()
|
local pairs=()
|
||||||
IFS=',' read -r -a pairs <<<"$1"
|
IFS=',' read -r -a pairs <<<"$1"
|
||||||
replace=true ;;
|
replace=true
|
||||||
*)
|
;;
|
||||||
{
|
*)
|
||||||
error "Unrecognized option '$1' passed to _execute. Exiting."
|
{
|
||||||
return 1
|
error "Unrecognized option '$1' passed to _execute. Exiting."
|
||||||
}
|
return 1
|
||||||
;;
|
}
|
||||||
esac
|
;;
|
||||||
done
|
esac
|
||||||
shift $((OPTIND - 1))
|
done
|
||||||
|
shift $((OPTIND - 1))
|
||||||
|
|
||||||
[[ $# -lt 1 ]] && fatal 'Missing required argument to _cleanString_()!'
|
[[ $# -lt 1 ]] && fatal 'Missing required argument to _cleanString_()!'
|
||||||
|
|
||||||
local string="${1}"
|
local string="${1}"
|
||||||
local userChars="${2:-}"
|
local userChars="${2:-}"
|
||||||
|
|
||||||
local arrayToClean=()
|
local arrayToClean=()
|
||||||
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="$(echo "${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="$(echo "${string}" | sed "s/$i//g")"
|
||||||
done
|
done
|
||||||
|
|
||||||
("${lc}") \
|
("${lc}") \
|
||||||
&& string="$(echo "${string}" | tr '[:upper:]' '[:lower:]')"
|
&& string="$(echo "${string}" | tr '[:upper:]' '[:lower:]')"
|
||||||
|
|
||||||
("${uc}") \
|
("${uc}") \
|
||||||
&& string="$(echo "${string}" | tr '[:lower:]' '[:upper:]')"
|
&& string="$(echo "${string}" | tr '[:lower:]' '[:upper:]')"
|
||||||
|
|
||||||
if "${alphanumeric}" && "${us}"; then
|
if "${alphanumeric}" && "${us}"; then
|
||||||
string="$(echo "${string}" | tr -c '[:alnum:] -_' ' ')"
|
string="$(echo "${string}" | tr -c '[:alnum:]_ -' ' ')"
|
||||||
elif "${alphanumeric}"; then
|
elif "${alphanumeric}"; then
|
||||||
string="$(echo "${string}" | sed "s/[^a-zA-Z0-9 -_]//g")"
|
string="$(echo "${string}" | sed "s/[^a-zA-Z0-9_ \-]//g")"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if "${replace}"; then
|
if "${replace}"; then
|
||||||
string="$(echo "${string}" | sed "s/${pairs[0]}/${pairs[1]}/g")"
|
string="$(echo "${string}" | sed "s/${pairs[0]}/${pairs[1]}/g")"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# trim trailing/leading white space and duplicate dashes
|
# trim trailing/leading white space and duplicate dashes
|
||||||
string="$(echo "${string}" | tr -s '-')"
|
string="$(echo "${string}" | tr -s '-' | tr -s '_')"
|
||||||
string="$(echo "${string}" | sed -E 's/([-_]) /\1/g' | sed -E 's/ ([-_])/\1/g')"
|
string="$(echo "${string}" | sed -E 's/([_\-]) /\1/g' | sed -E 's/ ([_\-])/\1/g')"
|
||||||
string="$(echo "${string}" | awk '{$1=$1};1')"
|
string="$(echo "${string}" | awk '{$1=$1};1')"
|
||||||
|
|
||||||
printf "%s\n" "${string}"
|
printf "%s\n" "${string}"
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_stopWords_() {
|
_stopWords_() {
|
||||||
# DESC: Removes common stopwords from a string
|
# DESC: Removes common stopwords from a string
|
||||||
# ARGS: $1 (Required) - String to parse
|
# ARGS: $1 (Required) - String to parse
|
||||||
# $2 (Optional) - Additional stopwords (comma separated)
|
# $2 (Optional) - Additional stopwords (comma separated)
|
||||||
# OUTS: Prints cleaned string to STDOUT
|
# OUTS: Prints cleaned string to STDOUT
|
||||||
# USAGE: cleanName="$(_stopWords_ "[STRING]" "[MORE,STOP,WORDS]")"
|
# USAGE: cleanName="$(_stopWords_ "[STRING]" "[MORE,STOP,WORDS]")"
|
||||||
# NOTE: Requires a stopwords file in sed format (expected at: ~/.sed/stopwords.sed)
|
# NOTE: Requires a stopwords file in sed format (expected at: ~/.sed/stopwords.sed)
|
||||||
|
|
||||||
[[ $# -lt 1 ]] && {
|
[[ $# -lt 1 ]] && {
|
||||||
warning 'Missing required argument to _stripCommonWords_!'
|
warning 'Missing required argument to _stripCommonWords_!'
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
[ "$(command -v gsed)" ] || {
|
if command -v gsed &>/dev/null; then
|
||||||
error "Can not continue without gsed. Use '${YELLOW}brew install gnu-sed${reset}'"
|
local SED_COMMAND="gsed"
|
||||||
return 1
|
elif sed --version | grep GNU &>/dev/null; then
|
||||||
}
|
local SED_COMMAND="sed"
|
||||||
|
else
|
||||||
|
error "Can not continue without gnu sed. Use '${YELLOW}brew install gnu-sed${reset} on a Mac or install with your package manager'"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
local string="${1}"
|
local string="${1}"
|
||||||
|
|
||||||
local sedFile="${HOME}/.sed/stopwords.sed"
|
local sedFile="${HOME}/.sed/stopwords.sed"
|
||||||
if [ -f "${sedFile}" ]; then
|
if [ -f "${sedFile}" ]; then
|
||||||
string="$(echo "${string}" | gsed -f "${sedFile}")"
|
string="$(echo "${string}" | ${SED_COMMAND} -f "${sedFile}")"
|
||||||
else
|
else
|
||||||
debug "Missing sedfile in _stopWords_()"
|
debug "Missing sedfile in _stopWords_()"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
declare -a localStopWords=()
|
declare -a localStopWords=()
|
||||||
IFS=',' read -r -a localStopWords <<<"${2:-}"
|
IFS=',' read -r -a localStopWords <<<"${2:-}"
|
||||||
|
|
||||||
if [[ ${#localStopWords[@]} -gt 0 ]]; then
|
if [[ ${#localStopWords[@]} -gt 0 ]]; then
|
||||||
for w in "${localStopWords[@]}"; do
|
for w in "${localStopWords[@]}"; do
|
||||||
string="$(echo "$string" | gsed -E "s/$w//gI")"
|
string="$(echo "${string}" | ${SED_COMMAND} -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' | _ltrim_ | _rtrim_)"
|
string="$(echo "${string}" | ${SED_COMMAND} -E 's/[ ]{2,}/ /g' | _ltrim_ | _rtrim_)"
|
||||||
|
|
||||||
echo "${string}"
|
echo "${string}"
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_escape_() {
|
_escape_() {
|
||||||
# DESC: Escapes a string by adding \ before special chars
|
# DESC: Escapes a string by adding \ before special chars
|
||||||
# ARGS: $@ (Required) - String to be escaped
|
# ARGS: $@ (Required) - String to be escaped
|
||||||
# OUTS: Prints output to STDOUT
|
# OUTS: Prints output to STDOUT
|
||||||
# USAGE: _escape_ "Some text here"
|
# USAGE: _escape_ "Some text here"
|
||||||
|
|
||||||
# shellcheck disable=2001
|
# shellcheck disable=2001
|
||||||
echo "${@}" | sed 's/[]\.|$[ (){}?+*^]/\\&/g'
|
echo "${@}" | sed 's/[]\.|$[ (){}?+*^]/\\&/g'
|
||||||
}
|
}
|
||||||
|
|
||||||
_htmlDecode_() {
|
_htmlDecode_() {
|
||||||
# DESC: Decode HTML characters with sed
|
# DESC: Decode HTML characters with sed
|
||||||
# ARGS: $1 (Required) - String to be decoded
|
# ARGS: $1 (Required) - String to be decoded
|
||||||
# OUTS: Prints output to STDOUT
|
# OUTS: Prints output to STDOUT
|
||||||
# USAGE: _htmlDecode_ <string>
|
# USAGE: _htmlDecode_ <string>
|
||||||
# NOTE: Must have a sed file containing replacements
|
# NOTE: Must have a sed file containing replacements
|
||||||
|
|
||||||
[[ $# -lt 1 ]] && {
|
[[ $# -lt 1 ]] && {
|
||||||
error 'Missing required argument to _htmlDecode_()!'
|
error 'Missing required argument to _htmlDecode_()!'
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
local sedFile
|
local sedFile
|
||||||
sedFile="${HOME}/.sed/htmlDecode.sed"
|
sedFile="${HOME}/.sed/htmlDecode.sed"
|
||||||
|
|
||||||
[ -f "${sedFile}" ] \
|
[ -f "${sedFile}" ] \
|
||||||
&& { echo "${1}" | sed -f "${sedFile}"; } \
|
&& { echo "${1}" | sed -f "${sedFile}"; } \
|
||||||
|| return 1
|
|| return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
_htmlEncode_() {
|
_htmlEncode_() {
|
||||||
# DESC: Encode HTML characters with sed
|
# DESC: Encode HTML characters with sed
|
||||||
# ARGS: $1 (Required) - String to be encoded
|
# ARGS: $1 (Required) - String to be encoded
|
||||||
# OUTS: Prints output to STDOUT
|
# OUTS: Prints output to STDOUT
|
||||||
# USAGE: _htmlEncode_ <string>
|
# USAGE: _htmlEncode_ <string>
|
||||||
# NOTE: Must have a sed file containing replacements
|
# NOTE: Must have a sed file containing replacements
|
||||||
|
|
||||||
[[ $# -lt 1 ]] && {
|
[[ $# -lt 1 ]] && {
|
||||||
error 'Missing required argument to _htmlEncode_()!'
|
error 'Missing required argument to _htmlEncode_()!'
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
local sedFile
|
local sedFile
|
||||||
sedFile="${HOME}/.sed/htmlEncode.sed"
|
sedFile="${HOME}/.sed/htmlEncode.sed"
|
||||||
|
|
||||||
[ -f "${sedFile}" ] \
|
[ -f "${sedFile}" ] \
|
||||||
&& { echo "${1}" | sed -f "${sedFile}"; } \
|
&& { echo "${1}" | sed -f "${sedFile}"; } \
|
||||||
|| return 1
|
|| return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
_lower_() {
|
_lower_() {
|
||||||
# DESC: Convert a string to lowercase
|
# DESC: Convert a string to lowercase
|
||||||
# ARGS: None
|
# ARGS: None
|
||||||
# OUTS: None
|
# OUTS: None
|
||||||
# USAGE: text=$(_lower_ <<<"$1")
|
# USAGE: text=$(_lower_ <<<"$1")
|
||||||
# echo "STRING" | _lower_
|
# echo "STRING" | _lower_
|
||||||
tr '[:upper:]' '[:lower:]'
|
tr '[:upper:]' '[:lower:]'
|
||||||
}
|
}
|
||||||
|
|
||||||
_upper_() {
|
_upper_() {
|
||||||
# DESC: Convert a string to uppercase
|
# DESC: Convert a string to uppercase
|
||||||
# ARGS: None
|
# ARGS: None
|
||||||
# OUTS: None
|
# OUTS: None
|
||||||
# USAGE: text=$(_upper_ <<<"$1")
|
# USAGE: text=$(_upper_ <<<"$1")
|
||||||
# echo "STRING" | _upper_
|
# echo "STRING" | _upper_
|
||||||
tr '[:lower:]' '[:upper:]'
|
tr '[:lower:]' '[:upper:]'
|
||||||
}
|
}
|
||||||
|
|
||||||
_ltrim_() {
|
_ltrim_() {
|
||||||
# DESC: Removes all leading whitespace (from the left)
|
# DESC: Removes all leading whitespace (from the left)
|
||||||
# ARGS: None
|
# ARGS: None
|
||||||
# OUTS: None
|
# OUTS: None
|
||||||
# USAGE: text=$(_ltrim_ <<<"$1")
|
# USAGE: text=$(_ltrim_ <<<"$1")
|
||||||
# echo "STRING" | _ltrim_
|
# echo "STRING" | _ltrim_
|
||||||
local char=${1:-[:space:]}
|
local char=${1:-[:space:]}
|
||||||
sed "s%^[${char//%/\\%}]*%%"
|
sed "s%^[${char//%/\\%}]*%%"
|
||||||
}
|
}
|
||||||
|
|
||||||
_regex_() {
|
_regex_() {
|
||||||
# DESC: Use regex to validate and parse strings
|
# DESC: Use regex to validate and parse strings
|
||||||
# ARGS: $1 (Required) - Input String
|
# ARGS: $1 (Required) - Input String
|
||||||
# $2 (Required) - Regex pattern
|
# $2 (Required) - Regex pattern
|
||||||
# OUTS: Prints string matching regex
|
# OUTS: Prints string matching regex
|
||||||
# Returns error if no part of string did not match regex
|
# Returns error if no part of string did not match regex
|
||||||
# USAGE: _regex_ "#FFFFFF" '^(#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3}))$'
|
# USAGE: _regex_ "#FFFFFF" '^(#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3}))$'
|
||||||
# NOTE: This example only prints the first matching group. When using multiple capture
|
# NOTE: This example only prints the first matching group. When using multiple capture
|
||||||
# groups some modification is needed.
|
# groups some modification is needed.
|
||||||
# https://github.com/dylanaraps/pure-bash-bible
|
# https://github.com/dylanaraps/pure-bash-bible
|
||||||
if [[ $1 =~ $2 ]]; then
|
if [[ $1 =~ $2 ]]; then
|
||||||
printf '%s\n' "${BASH_REMATCH[1]}"
|
printf '%s\n' "${BASH_REMATCH[1]}"
|
||||||
return 0
|
return 0
|
||||||
else
|
else
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
_rtrim_() {
|
_rtrim_() {
|
||||||
# DESC: Removes all leading whitespace (from the right)
|
# DESC: Removes all leading whitespace (from the right)
|
||||||
# ARGS: None
|
# ARGS: None
|
||||||
# OUTS: None
|
# OUTS: None
|
||||||
# USAGE: text=$(_rtrim_ <<<"$1")
|
# USAGE: text=$(_rtrim_ <<<"$1")
|
||||||
# echo "STRING" | _rtrim_
|
# echo "STRING" | _rtrim_
|
||||||
local char=${1:-[:space:]}
|
local char=${1:-[:space:]}
|
||||||
sed "s%[${char//%/\\%}]*$%%"
|
sed "s%[${char//%/\\%}]*$%%"
|
||||||
}
|
}
|
||||||
|
|
||||||
_trim_() {
|
_trim_() {
|
||||||
# DESC: Removes all leading/trailing whitespace
|
# DESC: Removes all leading/trailing whitespace
|
||||||
# ARGS: None
|
# ARGS: None
|
||||||
# OUTS: None
|
# OUTS: None
|
||||||
# USAGE: text=$(_trim_ <<<"$1")
|
# USAGE: text=$(_trim_ <<<"$1")
|
||||||
# echo "STRING" | _trim_
|
# echo "STRING" | _trim_
|
||||||
awk '{$1=$1;print}'
|
awk '{$1=$1;print}'
|
||||||
}
|
}
|
||||||
|
|
||||||
_urlEncode_() {
|
_urlEncode_() {
|
||||||
# DESC: URL encode a string
|
# DESC: URL encode a string
|
||||||
# ARGS: $1 (Required) - String to be encoded
|
# ARGS: $1 (Required) - String to be encoded
|
||||||
# OUTS: Prints output to STDOUT
|
# OUTS: Prints output to STDOUT
|
||||||
# USAGE: _urlEncode_ <string>
|
# USAGE: _urlEncode_ <string>
|
||||||
# NOTE: https://gist.github.com/cdown/1163649
|
# NOTE: https://gist.github.com/cdown/1163649
|
||||||
|
|
||||||
[[ $# -lt 1 ]] && {
|
[[ $# -lt 1 ]] && {
|
||||||
error 'Missing required argument to _urlEncode_()!'
|
error 'Missing required argument to _urlEncode_()!'
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
local LANG=C
|
local LANG=C
|
||||||
local i
|
local i
|
||||||
|
|
||||||
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 "${1:i:1}"
|
||||||
else
|
else
|
||||||
printf '%%%02X' "'${1:$i:1}"
|
printf '%%%02X' "'${1:i:1}"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
_urlDecode_() {
|
_urlDecode_() {
|
||||||
# DESC: Decode a URL encoded string
|
# DESC: Decode a URL encoded string
|
||||||
# ARGS: $1 (Required) - String to be decoded
|
# ARGS: $1 (Required) - String to be decoded
|
||||||
# OUTS: Prints output to STDOUT
|
# OUTS: Prints output to STDOUT
|
||||||
# USAGE: _urlDecode_ <string>
|
# USAGE: _urlDecode_ <string>
|
||||||
|
|
||||||
[[ $# -lt 1 ]] && {
|
[[ $# -lt 1 ]] && {
|
||||||
error 'Missing required argument to _urlDecode_()!'
|
error 'Missing required argument to _urlDecode_()!'
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
local url_encoded="${1//+/ }"
|
local url_encoded="${1//+/ }"
|
||||||
printf '%b' "${url_encoded//%/\\x}"
|
printf '%b' "${url_encoded//%/\\x}"
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user