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,4 +1,3 @@
|
||||
|
||||
# Colors
|
||||
if tput setaf 1 &>/dev/null; then
|
||||
bold=$(tput bold)
|
||||
@@ -41,15 +40,15 @@ _alert_() {
|
||||
local message="${2}"
|
||||
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_)"
|
||||
elif [[ -n "${line}" && "${FUNCNAME[2]}" != "_trapCleanup_" ]]; then
|
||||
elif [[ -n ${line} && ${FUNCNAME[2]} != "_trapCleanup_" ]]; then
|
||||
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_)"
|
||||
fi
|
||||
|
||||
if [[ "${alertType}" =~ ^(error|fatal) ]]; then
|
||||
if [[ ${alertType} =~ ^(error|fatal) ]]; then
|
||||
color="${bold}${red}"
|
||||
elif [ "${alertType}" = "warning" ]; then
|
||||
color="${red}"
|
||||
@@ -59,7 +58,7 @@ _alert_() {
|
||||
color="${purple}"
|
||||
elif [ "${alertType}" = "header" ]; then
|
||||
color="${bold}${tan}"
|
||||
elif [[ "${alertType}" =~ ^(input|notice) ]]; then
|
||||
elif [[ ${alertType} =~ ^(input|notice) ]]; then
|
||||
color="${bold}"
|
||||
elif [ "${alertType}" = "dryrun" ]; then
|
||||
color="${blue}"
|
||||
@@ -70,7 +69,7 @@ _alert_() {
|
||||
_writeToScreen_() {
|
||||
|
||||
("${QUIET}") && return 0 # Print to console when script is not 'quiet'
|
||||
[[ ${VERBOSE} == false && "${alertType}" =~ ^(debug|verbose) ]] && return 0
|
||||
[[ ${VERBOSE} == false && ${alertType} =~ ^(debug|verbose) ]] && return 0
|
||||
|
||||
if ! [[ -t 1 ]]; then # Don't use colors on non-recognized terminals
|
||||
color=""
|
||||
@@ -82,11 +81,13 @@ _alert_() {
|
||||
_writeToScreen_
|
||||
|
||||
_writeToLog_() {
|
||||
[[ "${alertType}" == "input" ]] && return 0
|
||||
[[ "${LOGLEVEL}" =~ (off|OFF|Off) ]] && return 0
|
||||
[ -z "${LOGFILE:-}" ] && fatal "\$LOGFILE must be set"
|
||||
[[ ${alertType} == "input" ]] && return 0
|
||||
[[ ${LOGLEVEL} =~ (off|OFF|Off) ]] && return 0
|
||||
if [ -z "${LOGFILE:-}" ]; then
|
||||
LOGFILE="$(pwd)/$(basename "$0").log"
|
||||
fi
|
||||
[ ! -d "$(dirname "${LOGFILE}")" ] && mkdir -p "$(dirname "${LOGFILE}")"
|
||||
[[ ! -f "${LOGFILE}" ]] && touch "${LOGFILE}"
|
||||
[[ ! -f ${LOGFILE} ]] && touch "${LOGFILE}"
|
||||
|
||||
# Don't use colors in logs
|
||||
if command -v gsed &>/dev/null; then
|
||||
@@ -97,43 +98,43 @@ _alert_() {
|
||||
echo -e "$(date +"%b %d %R:%S") $(printf "[%7s]" "${alertType}") [$(/bin/hostname)] ${cleanmessage}" >>"${LOGFILE}"
|
||||
}
|
||||
|
||||
# Write specified log level data to LOGFILE
|
||||
case "${LOGLEVEL:-ERROR}" in
|
||||
ALL|all|All)
|
||||
# Write specified log level data to logfile
|
||||
case "${LOGLEVEL:-ERROR}" in
|
||||
ALL | all | All)
|
||||
_writeToLog_
|
||||
;;
|
||||
DEBUG|debug|Debug)
|
||||
DEBUG | debug | Debug)
|
||||
_writeToLog_
|
||||
;;
|
||||
INFO|info|Info)
|
||||
if [[ "${alertType}" =~ ^(die|error|fatal|warning|info|notice|success) ]]; then
|
||||
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
|
||||
WARN | warn | Warn)
|
||||
if [[ ${alertType} =~ ^(die|error|fatal|warning) ]]; then
|
||||
_writeToLog_
|
||||
fi
|
||||
;;
|
||||
ERROR|error|Error)
|
||||
if [[ "${alertType}" =~ ^(die|error|fatal) ]]; then
|
||||
ERROR | error | Error)
|
||||
if [[ ${alertType} =~ ^(die|error|fatal) ]]; then
|
||||
_writeToLog_
|
||||
fi
|
||||
;;
|
||||
FATAL|fatal|Fatal)
|
||||
if [[ "${alertType}" =~ ^(die|fatal) ]]; then
|
||||
FATAL | fatal | Fatal)
|
||||
if [[ ${alertType} =~ ^(die|fatal) ]]; then
|
||||
_writeToLog_
|
||||
fi
|
||||
;;
|
||||
OFF|off)
|
||||
OFF | off)
|
||||
return 0
|
||||
;;
|
||||
*)
|
||||
if [[ "${alertType}" =~ ^(die|error|fatal) ]]; then
|
||||
if [[ ${alertType} =~ ^(die|error|fatal) ]]; then
|
||||
_writeToLog_
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
esac
|
||||
|
||||
} # /_alert_
|
||||
|
||||
@@ -145,8 +146,14 @@ success() { _alert_ success "${1}" "${2:-}"; }
|
||||
dryrun() { _alert_ dryrun "${1}" "${2:-}"; }
|
||||
input() { _alert_ input "${1}" "${2:-}"; }
|
||||
header() { _alert_ header "== ${1} ==" "${2:-}"; }
|
||||
die() { _alert_ fatal "${1}" "${2:-}"; _safeExit_ "1" ; }
|
||||
fatal() { _alert_ fatal "${1}" "${2:-}"; _safeExit_ "1" ; }
|
||||
die() {
|
||||
_alert_ fatal "${1}" "${2:-}"
|
||||
_safeExit_ "1"
|
||||
}
|
||||
fatal() {
|
||||
_alert_ fatal "${1}" "${2:-}"
|
||||
_safeExit_ "1"
|
||||
}
|
||||
debug() { _alert_ debug "${1}" "${2:-}"; }
|
||||
verbose() { _alert_ debug "${1}" "${2:-}"; }
|
||||
|
||||
@@ -158,8 +165,8 @@ _functionStack_() {
|
||||
local _i
|
||||
funcStackResponse=()
|
||||
for ((_i = 1; _i < ${#BASH_SOURCE[@]}; _i++)); do
|
||||
case "${FUNCNAME[$_i]}" in "_alert_" | "_trapCleanup_" | fatal | error | warning | verbose | debug | die) continue ;; esac
|
||||
funcStackResponse+=("${FUNCNAME[$_i]}:$(basename ${BASH_SOURCE[$_i]}):${BASH_LINENO[$_i - 1]}")
|
||||
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]}")
|
||||
done
|
||||
printf "( "
|
||||
printf %s "${funcStackResponse[0]}"
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
_inArray_() {
|
||||
# DESC: Determine if a value is in an array
|
||||
# ARGS: $1 (Required) - Value to search for
|
||||
@@ -11,7 +10,7 @@ _inArray_() {
|
||||
local value="$1"
|
||||
shift
|
||||
for arrayItem in "$@"; do
|
||||
[[ "${arrayItem}" == "${value}" ]] && return 0
|
||||
[[ ${arrayItem} == "${value}" ]] && return 0
|
||||
done
|
||||
return 1
|
||||
}
|
||||
@@ -45,7 +44,7 @@ _setdiff_() {
|
||||
[[ $# -lt 2 ]] && fatal 'Missing required argument to _setdiff_()!'
|
||||
|
||||
local debug skip a b
|
||||
if [[ "$1" == 1 ]]; then
|
||||
if [[ $1 == 1 ]]; then
|
||||
debug=1
|
||||
shift
|
||||
fi
|
||||
@@ -60,7 +59,7 @@ _setdiff_() {
|
||||
for a in "${setdiffA[@]}"; do
|
||||
skip=
|
||||
for b in "${setdiffB[@]}"; do
|
||||
[[ "$a" == "$b" ]] && skip=1 && break
|
||||
[[ $a == "$b" ]] && skip=1 && break
|
||||
done
|
||||
[[ "$skip" ]] || setdiffC=("${setdiffC[@]}" "$a")
|
||||
done
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
_execute_() {
|
||||
# DESC: Executes commands with safety and logging options
|
||||
# ARGS: $1 (Required) - The command to be executed. Quotation marks MUST be escaped.
|
||||
@@ -16,21 +15,21 @@ _execute_() {
|
||||
# If $VERBOSE=true the command's native output is printed to
|
||||
# stderr and stdin. This can be forced with `_execute_ -v`
|
||||
|
||||
local localVerbose=false
|
||||
local passFailures=false
|
||||
local echoResult=false
|
||||
local successResult=false
|
||||
local quietResult=false
|
||||
local LOCAL_VERBOSE=false
|
||||
local PASS_FAILURES=false
|
||||
local ECHO_RESULT=false
|
||||
local SUCCESS_RESULT=false
|
||||
local QUIET_RESULT=false
|
||||
local opt
|
||||
|
||||
local OPTIND=1
|
||||
while getopts ":vVpPeEsSqQ" opt; do
|
||||
case $opt in
|
||||
v | V) localVerbose=true ;;
|
||||
p | P) passFailures=true ;;
|
||||
e | E) echoResult=true ;;
|
||||
s | S) successResult=true ;;
|
||||
q | Q) quietResult=true ;;
|
||||
v | V) LOCAL_VERBOSE=true ;;
|
||||
p | P) PASS_FAILURES=true ;;
|
||||
e | E) ECHO_RESULT=true ;;
|
||||
s | S) SUCCESS_RESULT=true ;;
|
||||
q | Q) QUIET_RESULT=true ;;
|
||||
*)
|
||||
{
|
||||
error "Unrecognized option '$1' passed to _execute_. Exiting."
|
||||
@@ -41,17 +40,17 @@ _execute_() {
|
||||
done
|
||||
shift $((OPTIND - 1))
|
||||
|
||||
local cmd="${1:?_execute_ needs a command}"
|
||||
local message="${2:-$1}"
|
||||
local CMD="${1:?_execute_ needs a command}"
|
||||
local EXECUTE_MESSAGE="${2:-$1}"
|
||||
|
||||
local saveVerbose=$VERBOSE
|
||||
if "${localVerbose}"; then
|
||||
local SAVE_VERBOSE=${VERBOSE}
|
||||
if "${LOCAL_VERBOSE}"; then
|
||||
VERBOSE=true
|
||||
fi
|
||||
|
||||
if "${DRYRUN}"; then
|
||||
if "$quietResult"; then
|
||||
VERBOSE=$saveVerbose
|
||||
if "${QUIET_RESULT}"; then
|
||||
VERBOSE=$SAVE_VERBOSE
|
||||
return 0
|
||||
fi
|
||||
if [ -n "${2:-}" ]; then
|
||||
@@ -60,47 +59,47 @@ _execute_() {
|
||||
dryrun "${1}" "$(caller)"
|
||||
fi
|
||||
elif ${VERBOSE}; then
|
||||
if eval "${cmd}"; then
|
||||
if "$echoResult"; then
|
||||
echo "${message}"
|
||||
elif "${successResult}"; then
|
||||
success "${message}"
|
||||
if eval "${CMD}"; then
|
||||
if "${ECHO_RESULT}"; then
|
||||
echo "${EXECUTE_MESSAGE}"
|
||||
elif "${SUCCESS_RESULT}"; then
|
||||
success "${EXECUTE_MESSAGE}"
|
||||
else
|
||||
info "${message}"
|
||||
info "${EXECUTE_MESSAGE}"
|
||||
fi
|
||||
VERBOSE=$saveVerbose
|
||||
VERBOSE=${SAVE_VERBOSE}
|
||||
return 0
|
||||
else
|
||||
if "$echoResult"; then
|
||||
echo "warning: ${message}"
|
||||
if "${ECHO_RESULT}"; then
|
||||
echo "warning: ${EXECUTE_MESSAGE}"
|
||||
else
|
||||
warning "${message}"
|
||||
warning "${EXECUTE_MESSAGE}"
|
||||
fi
|
||||
VERBOSE=$saveVerbose
|
||||
"${passFailures}" && return 0 || return 1
|
||||
VERBOSE=${SAVE_VERBOSE}
|
||||
"${PASS_FAILURES}" && return 0 || return 1
|
||||
fi
|
||||
else
|
||||
if eval "${cmd}" &>/dev/null; then
|
||||
if "$quietResult"; then
|
||||
VERBOSE=$saveVerbose
|
||||
if eval "${CMD}" &>/dev/null; then
|
||||
if "${QUIET_RESULT}"; then
|
||||
VERBOSE=${SAVE_VERBOSE}
|
||||
return 0
|
||||
elif "$echoResult"; then
|
||||
echo "${message}"
|
||||
elif "${successResult}"; then
|
||||
success "${message}"
|
||||
elif "${ECHO_RESULT}"; then
|
||||
echo "${EXECUTE_MESSAGE}"
|
||||
elif "${SUCCESS_RESULT}"; then
|
||||
success "${EXECUTE_MESSAGE}"
|
||||
else
|
||||
info "${message}"
|
||||
info "${EXECUTE_MESSAGE}"
|
||||
fi
|
||||
VERBOSE=$saveVerbose
|
||||
VERBOSE=${SAVE_VERBOSE}
|
||||
return 0
|
||||
else
|
||||
if "$echoResult"; then
|
||||
echo "error: ${message}"
|
||||
if "${ECHO_RESULT}"; then
|
||||
echo "error: ${EXECUTE_MESSAGE}"
|
||||
else
|
||||
warning "${message}"
|
||||
warning "${EXECUTE_MESSAGE}"
|
||||
fi
|
||||
VERBOSE=$saveVerbose
|
||||
"${passFailures}" && return 0 || return 1
|
||||
VERBOSE=${SAVE_VERBOSE}
|
||||
"${PASS_FAILURES}" && return 0 || return 1
|
||||
fi
|
||||
fi
|
||||
}
|
||||
@@ -152,7 +151,7 @@ _haveFunction_() {
|
||||
local f
|
||||
f="$1"
|
||||
|
||||
if declare -f "$f" &>/dev/null 2>&1; then
|
||||
if declare -f "${f}" &>/dev/null 2>&1; then
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
@@ -339,19 +338,19 @@ _safeExit_() {
|
||||
# ARGS: $1 (optional) - Exit code (defaults to 0)
|
||||
# OUTS: None
|
||||
|
||||
if [[ -d "${script_lock:-}" ]]; then
|
||||
if command rm -rf "${script_lock}"; then
|
||||
if [[ -d ${SCRIPT_LOCK:-} ]]; then
|
||||
if command rm -rf "${SCRIPT_LOCK}"; then
|
||||
debug "Removing script lock"
|
||||
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
|
||||
|
||||
if [[ -n "${tmpDir:-}" && -d "${tmpDir:-}" ]]; then
|
||||
if [[ ${1:-} == 1 && -n "$(ls "${tmpDir}")" ]]; then
|
||||
command rm -r "${tmpDir}"
|
||||
if [[ -n ${TMP_DIR:-} && -d ${TMP_DIR:-} ]]; then
|
||||
if [[ ${1:-} == 1 && -n "$(ls "${TMP_DIR}")" ]]; then
|
||||
command rm -r "${TMP_DIR}"
|
||||
else
|
||||
command rm -r "${tmpDir}"
|
||||
command rm -r "${TMP_DIR}"
|
||||
debug "Removing temp directory"
|
||||
fi
|
||||
fi
|
||||
|
||||
@@ -6,21 +6,22 @@ _monthToNumber_() {
|
||||
|
||||
local mon="$(echo "$1" | tr '[:upper:]' '[:lower:]')"
|
||||
case "$mon" in
|
||||
january|jan|ja) echo 1 ;;
|
||||
february|feb|fe) echo 2 ;;
|
||||
march|mar|ma) echo 3 ;;
|
||||
april|apr|ap) echo 4 ;;
|
||||
january | jan | ja) echo 1 ;;
|
||||
february | feb | fe) echo 2 ;;
|
||||
march | mar | ma) echo 3 ;;
|
||||
april | apr | ap) echo 4 ;;
|
||||
may) echo 5 ;;
|
||||
june|jun|ju) echo 6 ;;
|
||||
july|jul) echo 7 ;;
|
||||
august|aug|au) echo 8 ;;
|
||||
september|sep|se) echo 9 ;;
|
||||
october|oct) echo 10 ;;
|
||||
november|nov|no) echo 11 ;;
|
||||
december|dec|de) echo 12 ;;
|
||||
june | jun | ju) echo 6 ;;
|
||||
july | jul) echo 7 ;;
|
||||
august | aug | au) echo 8 ;;
|
||||
september | sep | se) echo 9 ;;
|
||||
october | oct) echo 10 ;;
|
||||
november | nov | no) echo 11 ;;
|
||||
december | dec | de) echo 12 ;;
|
||||
*)
|
||||
warning "month_monthToNumber_: Bad monthname: $1"
|
||||
return 1 ;;
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
@@ -32,21 +33,22 @@ _numberToMonth_() {
|
||||
|
||||
local mon="$1"
|
||||
case "$mon" in
|
||||
1|01) echo January ;;
|
||||
2|02) echo February ;;
|
||||
3|03) echo March ;;
|
||||
4|04) echo April ;;
|
||||
5|05) echo May ;;
|
||||
6|06) echo June ;;
|
||||
7|07) echo July ;;
|
||||
8|08) echo August ;;
|
||||
9|09) echo September ;;
|
||||
1 | 01) echo January ;;
|
||||
2 | 02) echo February ;;
|
||||
3 | 03) echo March ;;
|
||||
4 | 04) echo April ;;
|
||||
5 | 05) echo May ;;
|
||||
6 | 06) echo June ;;
|
||||
7 | 07) echo July ;;
|
||||
8 | 08) echo August ;;
|
||||
9 | 09) echo September ;;
|
||||
10) echo October ;;
|
||||
11) echo November ;;
|
||||
12) echo December ;;
|
||||
*)
|
||||
warning "_numberToMonth_: Bad month number: $1"
|
||||
return 1 ;;
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
@@ -88,108 +90,108 @@ _parseDate_() {
|
||||
|
||||
# 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
|
||||
if [[ ${date} =~ $pat ]]; then
|
||||
_parseDate_found="${BASH_REMATCH[2]}"
|
||||
_parseDate_year=$(( 10#${BASH_REMATCH[3]} ))
|
||||
_parseDate_month=$(( 10#${BASH_REMATCH[4]} ))
|
||||
_parseDate_year=$((10#${BASH_REMATCH[3]}))
|
||||
_parseDate_month=$((10#${BASH_REMATCH[4]}))
|
||||
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
||||
_parseDate_day=$(( 10#${BASH_REMATCH[5]} ))
|
||||
_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
|
||||
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]:-} ))
|
||||
_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
|
||||
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]} ))"
|
||||
_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
|
||||
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_day=$((10#"${BASH_REMATCH[3]}"))
|
||||
_parseDate_month="$(_monthToNumber_ "${BASH_REMATCH[4]}")"
|
||||
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
||||
_parseDate_year=$(( 10#"${BASH_REMATCH[5]}" ))
|
||||
_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
|
||||
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
|
||||
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_year=$((10#${BASH_REMATCH[5]}))
|
||||
_parseDate_month=$((10#${BASH_REMATCH[3]}))
|
||||
_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}"
|
||||
elif [[ $(( 10#${BASH_REMATCH[3]} )) -gt 12 && \
|
||||
$(( 10#${BASH_REMATCH[3]} )) -lt 32 && \
|
||||
$(( 10#${BASH_REMATCH[4]} )) -lt 13
|
||||
]]; then
|
||||
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_year=$((10#${BASH_REMATCH[5]}))
|
||||
_parseDate_month=$((10#${BASH_REMATCH[4]}))
|
||||
_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}"
|
||||
elif [[ $(( 10#${BASH_REMATCH[3]} )) -lt 32 && \
|
||||
$(( 10#${BASH_REMATCH[4]} )) -lt 13
|
||||
]]; then
|
||||
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_year=$((10#${BASH_REMATCH[5]}))
|
||||
_parseDate_month=$((10#${BASH_REMATCH[3]}))
|
||||
_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}"
|
||||
else
|
||||
shopt -u nocasematch
|
||||
return 1
|
||||
fi
|
||||
|
||||
elif [[ "${date}" =~ (.*[^0-9]|^)(([0-9]{1,2})[-\.\/_ ]+([0-9]{1,2})[-\.\/_ ]+([0-9]{2}))([^0-9].*|$) ]]; then
|
||||
elif [[ ${date} =~ (.*[^0-9]|^)(([0-9]{1,2})[-\.\/_ ]+([0-9]{1,2})[-\.\/_ ]+([0-9]{2}))([^0-9].*|$) ]]; then
|
||||
|
||||
if [[ $(( 10#${BASH_REMATCH[3]} )) -lt 13 && \
|
||||
$(( 10#${BASH_REMATCH[4]} )) -gt 12 && \
|
||||
$(( 10#${BASH_REMATCH[4]} )) -lt 32
|
||||
]]; 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="20$(( 10#${BASH_REMATCH[5]} ))"
|
||||
_parseDate_month=$(( 10#${BASH_REMATCH[3]} ))
|
||||
_parseDate_year="20$((10#${BASH_REMATCH[5]}))"
|
||||
_parseDate_month=$((10#${BASH_REMATCH[3]}))
|
||||
_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}"
|
||||
elif [[ $(( 10#${BASH_REMATCH[3]} )) -gt 12 && \
|
||||
$(( 10#${BASH_REMATCH[3]} )) -lt 32 && \
|
||||
$(( 10#${BASH_REMATCH[4]} )) -lt 13
|
||||
]]; then
|
||||
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_year="20$((10#${BASH_REMATCH[5]}))"
|
||||
_parseDate_month=$((10#${BASH_REMATCH[4]}))
|
||||
_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}"
|
||||
elif [[ $(( 10#${BASH_REMATCH[3]} )) -lt 32 && \
|
||||
$(( 10#${BASH_REMATCH[4]} )) -lt 13
|
||||
]]; then
|
||||
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_year="20$((10#${BASH_REMATCH[5]}))"
|
||||
_parseDate_month=$((10#${BASH_REMATCH[3]}))
|
||||
_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}"
|
||||
else
|
||||
shopt -u nocasematch
|
||||
@@ -197,95 +199,95 @@ _parseDate_() {
|
||||
fi
|
||||
|
||||
# Month, 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),?[-\./_ ]+(20[0-2][0-9]))([^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]} ))"
|
||||
_parseDate_year="$((10#${BASH_REMATCH[3]}))"
|
||||
debug "regex match: ${tan}Month, YYYY${purple}"
|
||||
|
||||
# YYYYMMDDHHMM
|
||||
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]|^)((20[0-2][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_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]} ))"
|
||||
_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
|
||||
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_day="$((10#${BASH_REMATCH[5]}))"
|
||||
_parseDate_month="$((10#${BASH_REMATCH[4]}))"
|
||||
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
||||
_parseDate_year="$(( 10#${BASH_REMATCH[3]} ))"
|
||||
_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
|
||||
elif [[ ${date} =~ (.*[^0-9]|^)(([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2}))([^0-9].*|$) ]]; then
|
||||
|
||||
# MMDDYYYY
|
||||
if [[ $(( 10#${BASH_REMATCH[5]} )) -eq 20 && \
|
||||
$(( 10#${BASH_REMATCH[3]} )) -lt 13 && \
|
||||
$(( 10#${BASH_REMATCH[4]} )) -lt 32
|
||||
]]; then
|
||||
if [[ $((10#${BASH_REMATCH[5]})) -eq 20 &&
|
||||
$((10#${BASH_REMATCH[3]})) -lt 13 &&
|
||||
$((10#${BASH_REMATCH[4]})) -lt 32 ]] \
|
||||
; then
|
||||
_parseDate_found="${BASH_REMATCH[2]}"
|
||||
_parseDate_day="$(( 10#${BASH_REMATCH[4]} ))"
|
||||
_parseDate_month="$(( 10#${BASH_REMATCH[3]} ))"
|
||||
_parseDate_day="$((10#${BASH_REMATCH[4]}))"
|
||||
_parseDate_month="$((10#${BASH_REMATCH[3]}))"
|
||||
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
||||
_parseDate_year="${BASH_REMATCH[5]}${BASH_REMATCH[6]}"
|
||||
debug "regex match: ${tan}MMDDYYYY${purple}"
|
||||
# DDMMYYYY
|
||||
elif [[ $(( 10#${BASH_REMATCH[5]} )) -eq 20 && \
|
||||
$(( 10#${BASH_REMATCH[3]} )) -gt 12 && \
|
||||
$(( 10#${BASH_REMATCH[3]} )) -lt 32 && \
|
||||
$(( 10#${BASH_REMATCH[4]} )) -lt 13
|
||||
]]; then
|
||||
elif [[ $((10#${BASH_REMATCH[5]})) -eq 20 &&
|
||||
$((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_day="$(( 10#${BASH_REMATCH[3]} ))"
|
||||
_parseDate_month="$(( 10#${BASH_REMATCH[4]} ))"
|
||||
_parseDate_day="$((10#${BASH_REMATCH[3]}))"
|
||||
_parseDate_month="$((10#${BASH_REMATCH[4]}))"
|
||||
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
||||
_parseDate_year="${BASH_REMATCH[5]}${BASH_REMATCH[6]}"
|
||||
debug "regex match: ${tan}DDMMYYYY${purple}"
|
||||
# YYYYMMDD
|
||||
elif [[ $(( 10#${BASH_REMATCH[3]} )) -eq 20 \
|
||||
&& $(( 10#${BASH_REMATCH[6]} )) -gt 12 \
|
||||
&& $(( 10#${BASH_REMATCH[6]} )) -lt 32 \
|
||||
&& $(( 10#${BASH_REMATCH[5]} )) -lt 13 \
|
||||
]]; then
|
||||
elif [[ $((10#${BASH_REMATCH[3]})) -eq 20 &&
|
||||
$((10#${BASH_REMATCH[6]})) -gt 12 &&
|
||||
$((10#${BASH_REMATCH[6]})) -lt 32 &&
|
||||
$((10#${BASH_REMATCH[5]})) -lt 13 ]] \
|
||||
; then
|
||||
_parseDate_found="${BASH_REMATCH[2]}"
|
||||
_parseDate_day="$(( 10#${BASH_REMATCH[6]} ))"
|
||||
_parseDate_month="$(( 10#${BASH_REMATCH[5]} ))"
|
||||
_parseDate_day="$((10#${BASH_REMATCH[6]}))"
|
||||
_parseDate_month="$((10#${BASH_REMATCH[5]}))"
|
||||
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
||||
_parseDate_year="${BASH_REMATCH[3]}${BASH_REMATCH[4]}"
|
||||
debug "regex match: ${tan}YYYYMMDD${purple}"
|
||||
# YYYYDDMM
|
||||
elif [[ $(( 10#${BASH_REMATCH[3]} )) -eq 20 \
|
||||
&& $(( 10#${BASH_REMATCH[5]} )) -gt 12 \
|
||||
&& $(( 10#${BASH_REMATCH[5]} )) -lt 32 \
|
||||
&& $(( 10#${BASH_REMATCH[6]} )) -lt 13 \
|
||||
]]; then
|
||||
elif [[ $((10#${BASH_REMATCH[3]})) -eq 20 &&
|
||||
$((10#${BASH_REMATCH[5]})) -gt 12 &&
|
||||
$((10#${BASH_REMATCH[5]})) -lt 32 &&
|
||||
$((10#${BASH_REMATCH[6]})) -lt 13 ]] \
|
||||
; then
|
||||
_parseDate_found="${BASH_REMATCH[2]}"
|
||||
_parseDate_day="$(( 10#${BASH_REMATCH[5]} ))"
|
||||
_parseDate_month="$(( 10#${BASH_REMATCH[6]} ))"
|
||||
_parseDate_day="$((10#${BASH_REMATCH[5]}))"
|
||||
_parseDate_month="$((10#${BASH_REMATCH[6]}))"
|
||||
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
||||
_parseDate_year="${BASH_REMATCH[3]}${BASH_REMATCH[4]}"
|
||||
debug "regex match: ${tan}YYYYMMDD${purple}"
|
||||
# Assume YYYMMDD
|
||||
elif [[ $(( 10#${BASH_REMATCH[3]} )) -eq 20 \
|
||||
&& $(( 10#${BASH_REMATCH[6]} )) -lt 32 \
|
||||
&& $(( 10#${BASH_REMATCH[5]} )) -lt 13 \
|
||||
]]; then
|
||||
elif [[ $((10#${BASH_REMATCH[3]})) -eq 20 &&
|
||||
$((10#${BASH_REMATCH[6]})) -lt 32 &&
|
||||
$((10#${BASH_REMATCH[5]})) -lt 13 ]] \
|
||||
; then
|
||||
_parseDate_found="${BASH_REMATCH[2]}"
|
||||
_parseDate_day="$(( 10#${BASH_REMATCH[6]} ))"
|
||||
_parseDate_month="$(( 10#${BASH_REMATCH[5]} ))"
|
||||
_parseDate_day="$((10#${BASH_REMATCH[6]}))"
|
||||
_parseDate_month="$((10#${BASH_REMATCH[5]}))"
|
||||
_parseDate_monthName="$(_numberToMonth_ "${_parseDate_month}")"
|
||||
_parseDate_year="${BASH_REMATCH[3]}${BASH_REMATCH[4]}"
|
||||
debug "regex match: ${tan}YYYYMMDD${purple}"
|
||||
@@ -325,9 +327,18 @@ _parseDate_() {
|
||||
|
||||
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 ; }
|
||||
[[ -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}"
|
||||
|
||||
@@ -156,8 +156,7 @@ _parseFilename_() {
|
||||
|
||||
local fileToParse="${1}"
|
||||
|
||||
|
||||
[[ -f "${fileToParse}" ]] || {
|
||||
[[ -f ${fileToParse} ]] || {
|
||||
error "Can't locate a file to parse at: ${fileToParse}"
|
||||
return 1
|
||||
}
|
||||
@@ -188,7 +187,7 @@ _parseFilename_() {
|
||||
fi
|
||||
fn=${fn%.$ext}
|
||||
done
|
||||
if [[ "${exts}" == "${PARSE_BASE}" ]]; then
|
||||
if [[ ${exts} == "${PARSE_BASE}" ]]; then
|
||||
PARSE_EXT="" && debug "\${PARSE_EXT}: ${PARSE_EXT}"
|
||||
else
|
||||
PARSE_EXT="${exts}" && debug "\${PARSE_EXT}: ${PARSE_EXT}"
|
||||
@@ -273,7 +272,7 @@ _extract_() {
|
||||
|
||||
[[ $# -lt 1 ]] && fatal 'Missing required argument to _extract_()!'
|
||||
|
||||
[[ "${2:-}" == "v" ]] && vv="v"
|
||||
[[ ${2:-} == "v" ]] && vv="v"
|
||||
|
||||
if [ -f "$1" ]; then
|
||||
case "$1" in
|
||||
@@ -358,7 +357,7 @@ _makeSymlink_() {
|
||||
|
||||
if ! command -v realpath >/dev/null 2>&1; then
|
||||
error "We must have 'realpath' installed and available in \$PATH to run."
|
||||
if [[ "$OSTYPE" == "darwin"* ]]; then
|
||||
if [[ $OSTYPE == "darwin"* ]]; then
|
||||
notice "Install coreutils using homebrew and rerun this script."
|
||||
info "\t$ brew install coreutils"
|
||||
fi
|
||||
@@ -407,7 +406,7 @@ _makeSymlink_() {
|
||||
elif [ -h "${d}" ]; then
|
||||
o="$(realpath "${d}")"
|
||||
|
||||
[[ "${o}" == "${s}" ]] && {
|
||||
[[ ${o} == "${s}" ]] && {
|
||||
if [ "${DRYRUN}" == true ]; then
|
||||
dryrun "Symlink already exists: ${s} → ${d}"
|
||||
else
|
||||
@@ -416,11 +415,11 @@ _makeSymlink_() {
|
||||
return 0
|
||||
}
|
||||
|
||||
if [[ "${backupOriginal}" == true ]]; then
|
||||
if [[ ${backupOriginal} == true ]]; then
|
||||
_backupFile_ "${d}" "${b:-backup}"
|
||||
fi
|
||||
if [[ "${DRYRUN}" == false ]]; then
|
||||
if [[ "${useSudo}" == true ]]; then
|
||||
if [[ ${DRYRUN} == false ]]; then
|
||||
if [[ ${useSudo} == true ]]; then
|
||||
command rm -rf "${d}"
|
||||
else
|
||||
command rm -rf "${d}"
|
||||
@@ -428,11 +427,11 @@ _makeSymlink_() {
|
||||
fi
|
||||
_execute_ "ln -fs \"${s}\" \"${d}\"" "symlink ${s} → ${d}"
|
||||
elif [ -e "${d}" ]; then
|
||||
if [[ "${backupOriginal}" == true ]]; then
|
||||
if [[ ${backupOriginal} == true ]]; then
|
||||
_backupFile_ "${d}" "${b:-backup}"
|
||||
fi
|
||||
if [[ "${DRYRUN}" == false ]]; then
|
||||
if [[ "${useSudo}" == true ]]; then
|
||||
if [[ ${DRYRUN} == false ]]; then
|
||||
if [[ ${useSudo} == true ]]; then
|
||||
sudo command rm -rf "${d}"
|
||||
else
|
||||
command rm -rf "${d}"
|
||||
@@ -510,13 +509,13 @@ _sourceFile_() {
|
||||
|
||||
local c="$1"
|
||||
|
||||
[ ! -f "$c" ] \
|
||||
[ ! -f "${c}" ] \
|
||||
&& {
|
||||
fatal "Attempted to source '$c'. Not found"
|
||||
return 1
|
||||
}
|
||||
|
||||
source "$c"
|
||||
source "${c}"
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -537,7 +536,7 @@ _uniqueFileName_() {
|
||||
|
||||
if ! command -v realpath >/dev/null 2>&1; then
|
||||
error "We must have 'realpath' installed and available in \$PATH to run."
|
||||
if [[ "$OSTYPE" == "darwin"* ]]; then
|
||||
if [[ $OSTYPE == "darwin"* ]]; then
|
||||
notice "Install coreutils using homebrew and rerun this script."
|
||||
info "\t$ brew install coreutils"
|
||||
fi
|
||||
@@ -553,7 +552,7 @@ _uniqueFileName_() {
|
||||
filename="$(basename "${fullfile}")"
|
||||
|
||||
# Extract extensions only when they exist
|
||||
if [[ "${filename}" =~ \.[a-zA-Z]{2,4}$ ]]; then
|
||||
if [[ ${filename} =~ \.[a-zA-Z]{2,4}$ ]]; then
|
||||
extension=".${filename##*.}"
|
||||
filename="${filename%.*}"
|
||||
fi
|
||||
|
||||
@@ -8,7 +8,7 @@ _haveScriptableFinder_() {
|
||||
local finder_pid
|
||||
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
|
||||
else
|
||||
return 1
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
_fromSeconds_() {
|
||||
# DESC: Convert seconds to HH:MM:SS
|
||||
# ARGS: $1 (Required) - Time in seconds
|
||||
@@ -31,10 +30,13 @@ _toSeconds_() {
|
||||
# 12h12m09s
|
||||
|
||||
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"
|
||||
IFS=":,;-_, HhMmSs" read -r h m s <<< "$1"
|
||||
IFS=":,;-_, HhMmSs" read -r h m s <<<"$1"
|
||||
IFS="$saveIFS"
|
||||
else
|
||||
h="$1"
|
||||
@@ -42,7 +44,7 @@ _toSeconds_() {
|
||||
s="$3"
|
||||
fi
|
||||
|
||||
echo $(( 10#$h * 3600 + 10#$m * 60 + 10#$s ))
|
||||
echo $((10#$h * 3600 + 10#$m * 60 + 10#$s))
|
||||
}
|
||||
|
||||
_countdown_() {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
_cleanString_() {
|
||||
# DESC: Cleans a string of text
|
||||
# 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)
|
||||
# OPTS: -l Forces all text to lowercase
|
||||
# -u Forces all text to uppercase
|
||||
@@ -12,8 +12,8 @@ _cleanString_() {
|
||||
# -p Replace one character with another (separated by commas) (escape regex characters)
|
||||
# -s In combination with -a, replaces characters with a space
|
||||
# OUTS: Prints result to STDOUT
|
||||
# USAGE: _cleanString_ [OPT] [STRING] [CHARS TO REPLACE]
|
||||
# _cleanString_ -p " ,-" [STRING] [CHARS TO REPLACE]
|
||||
# USAGE: _cleanString_ [OPT] [STRING] [CHARS TO REMOVE]
|
||||
# _cleanString_ -p " ,-" [STRING] [CHARS TO REMOVE]
|
||||
# NOTES: Always cleaned:
|
||||
# - leading white space
|
||||
# - trailing white space
|
||||
@@ -38,7 +38,8 @@ _cleanString_() {
|
||||
shift
|
||||
local pairs=()
|
||||
IFS=',' read -r -a pairs <<<"$1"
|
||||
replace=true ;;
|
||||
replace=true
|
||||
;;
|
||||
*)
|
||||
{
|
||||
error "Unrecognized option '$1' passed to _execute. Exiting."
|
||||
@@ -73,9 +74,9 @@ _cleanString_() {
|
||||
&& string="$(echo "${string}" | tr '[:lower:]' '[:upper:]')"
|
||||
|
||||
if "${alphanumeric}" && "${us}"; then
|
||||
string="$(echo "${string}" | tr -c '[:alnum:] -_' ' ')"
|
||||
string="$(echo "${string}" | tr -c '[:alnum:]_ -' ' ')"
|
||||
elif "${alphanumeric}"; then
|
||||
string="$(echo "${string}" | sed "s/[^a-zA-Z0-9 -_]//g")"
|
||||
string="$(echo "${string}" | sed "s/[^a-zA-Z0-9_ \-]//g")"
|
||||
fi
|
||||
|
||||
if "${replace}"; then
|
||||
@@ -83,8 +84,8 @@ _cleanString_() {
|
||||
fi
|
||||
|
||||
# trim trailing/leading white space and duplicate dashes
|
||||
string="$(echo "${string}" | tr -s '-')"
|
||||
string="$(echo "${string}" | sed -E 's/([-_]) /\1/g' | sed -E 's/ ([-_])/\1/g')"
|
||||
string="$(echo "${string}" | tr -s '-' | tr -s '_')"
|
||||
string="$(echo "${string}" | sed -E 's/([_\-]) /\1/g' | sed -E 's/ ([_\-])/\1/g')"
|
||||
string="$(echo "${string}" | awk '{$1=$1};1')"
|
||||
|
||||
printf "%s\n" "${string}"
|
||||
@@ -104,16 +105,19 @@ _stopWords_() {
|
||||
return 1
|
||||
}
|
||||
|
||||
[ "$(command -v gsed)" ] || {
|
||||
error "Can not continue without gsed. Use '${YELLOW}brew install gnu-sed${reset}'"
|
||||
if command -v gsed &>/dev/null; then
|
||||
local SED_COMMAND="gsed"
|
||||
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 sedFile="${HOME}/.sed/stopwords.sed"
|
||||
if [ -f "${sedFile}" ]; then
|
||||
string="$(echo "${string}" | gsed -f "${sedFile}")"
|
||||
string="$(echo "${string}" | ${SED_COMMAND} -f "${sedFile}")"
|
||||
else
|
||||
debug "Missing sedfile in _stopWords_()"
|
||||
fi
|
||||
@@ -123,12 +127,12 @@ _stopWords_() {
|
||||
|
||||
if [[ ${#localStopWords[@]} -gt 0 ]]; then
|
||||
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
|
||||
fi
|
||||
|
||||
# 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}"
|
||||
|
||||
@@ -265,10 +269,10 @@ _urlEncode_() {
|
||||
local i
|
||||
|
||||
for ((i = 0; i < ${#1}; i++)); do
|
||||
if [[ ${1:$i:1} =~ ^[a-zA-Z0-9\.\~_-]$ ]]; then
|
||||
printf "${1:$i:1}"
|
||||
if [[ ${1:i:1} =~ ^[a-zA-Z0-9\.\~_-]$ ]]; then
|
||||
printf "${1:i:1}"
|
||||
else
|
||||
printf '%%%02X' "'${1:$i:1}"
|
||||
printf '%%%02X' "'${1:i:1}"
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user