diff --git a/scriptTemplate.sh b/scriptTemplate.sh index 64ee046..7cc93dc 100755 --- a/scriptTemplate.sh +++ b/scriptTemplate.sh @@ -16,22 +16,22 @@ _mainScript_() { } # end _mainScript_ # ################################## Flags and defaults - # Script specific +# Script specific - # Common - LOGFILE="${HOME}/logs/$(basename "$0").log" - QUIET=false - LOGLEVEL=ERROR - VERBOSE=false - FORCE=false - DRYRUN=false - declare -a ARGS=() - NOW=$(LC_ALL=C date +"%m-%d-%Y %r") # Returns: 06-14-2015 10:34:40 PM - DATESTAMP=$(LC_ALL=C date +%Y-%m-%d) # Returns: 2015-06-14 - HOURSTAMP=$(LC_ALL=C date +%r) # Returns: 10:34:40 PM - TIMESTAMP=$(LC_ALL=C date +%Y%m%d_%H%M%S) # Returns: 20150614_223440 - LONGDATE=$(LC_ALL=C date +"%a, %d %b %Y %H:%M:%S %z") # Returns: Sun, 10 Jan 2016 20:47:53 -0500 - GMTDATE=$(LC_ALL=C date -u -R | sed 's/\+0000/GMT/') # Returns: Wed, 13 Jan 2016 15:55:29 GMT +# Common +LOGFILE="${HOME}/logs/$(basename "$0").log" +QUIET=false +LOGLEVEL=ERROR +VERBOSE=false +FORCE=false +DRYRUN=false +declare -a ARGS=() +NOW=$(LC_ALL=C date +"%m-%d-%Y %r") # Returns: 06-14-2015 10:34:40 PM +DATESTAMP=$(LC_ALL=C date +%Y-%m-%d) # Returns: 2015-06-14 +HOURSTAMP=$(LC_ALL=C date +%r) # Returns: 10:34:40 PM +TIMESTAMP=$(LC_ALL=C date +%Y%m%d_%H%M%S) # Returns: 20150614_223440 +LONGDATE=$(LC_ALL=C date +"%a, %d %b %Y %H:%M:%S %z") # Returns: Sun, 10 Jan 2016 20:47:53 -0500 +GMTDATE=$(LC_ALL=C date -u -R | sed 's/\+0000/GMT/') # Returns: Wed, 13 Jan 2016 15:55:29 GMT # ################################## Custom utility functions @@ -99,13 +99,13 @@ _alert_() { local function_name color local alertType="${1}" 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_)" - 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 @@ -123,7 +123,7 @@ _alert_() { color="${bold}${tan}" elif [ ${alertType} == "notice" ]; then color="${bold}" - elif [ ${alertType} == "input" ]; then + elif [ ${alertType} == "input" ]; then color="${bold}${underline}" elif [ "${alertType}" = "dryrun" ]; then color="${blue}" @@ -231,7 +231,7 @@ _safeExit_() { # ARGS: $1 (optional) - Exit code (defaults to 0) # OUTS: None - if [[ -d "${SCRIPT_LOCK:-}" ]]; then + if [[ -d ${SCRIPT_LOCK:-} ]]; then if command rm -rf "${SCRIPT_LOCK}"; then debug "Removing script lock" else @@ -239,7 +239,7 @@ _safeExit_() { fi fi - if [[ -n "${TMP_DIR:-}" && -d "${TMP_DIR:-}" ]]; then + if [[ -n ${TMP_DIR:-} && -d ${TMP_DIR:-} ]]; then if [[ ${1:-} == 1 && -n "$(ls "${TMP_DIR}")" ]]; then # Do something here to save TMP_DIR on a non-zero script exit for debugging command rm -r "${TMP_DIR}" @@ -273,7 +273,7 @@ _trapCleanup_() { funcstack="'$(echo "$funcstack" | sed -E 's/ / < /g')'" - if [[ "${script##*/}" == "${sourced##*/}" ]]; then + if [[ ${script##*/} == "${sourced##*/}" ]]; then fatal "${7:-} command: '${command}' (line: ${line}) [func: $(_functionStack_)]" else fatal "${7:-} command: '${command}' (func: ${funcstack} called at line ${linecallfunc} of '${script##*/}') (line: $line of '${sourced##*/}') " @@ -335,7 +335,7 @@ _functionStack_() { funcStackResponse=() for ((_i = 1; _i < ${#BASH_SOURCE[@]}; _i++)); do 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 printf "( " printf %s "${funcStackResponse[0]}" @@ -435,19 +435,46 @@ EOF # ################################## INITIALIZE AND RUN THE SCRIPT # (Comment or uncomment the lines below to customize script behavior) -trap '_trapCleanup_ ${LINENO} ${BASH_LINENO} "${BASH_COMMAND}" "${FUNCNAME[*]}" "${0}" "${BASH_SOURCE[0]}"' \ - EXIT INT TERM SIGINT SIGQUIT -set -o errtrace # Trap errors in subshells and functions -set -o errexit # Exit on error. Append '||true' if you expect an error -set -o pipefail # Use last non-zero exit code in a pipeline -# shopt -s nullglob globstar # Make `for f in *.txt` work when `*.txt` matches zero files -IFS=$' \n\t' # Set IFS to preferred implementation -# set -o xtrace # Run in debug mode -_setColors_ # Initialize color constants -set -o nounset # Disallow expansion of unset variables -# [[ $# -eq 0 ]] && _parseOptions_ "-h" # Force arguments when invoking the script -_parseOptions_ "$@" # Parse arguments passed to script -# _makeTempDir_ "$(basename "$0")" # Create a temp directory '$TMP_DIR' -# _acquireScriptLock_ # Acquire script lock -_mainScript_ # Run the main logic script -_safeExit_ # Exit cleanly +trap '_trapCleanup_ ${LINENO} ${BASH_LINENO} "${BASH_COMMAND}" "${FUNCNAME[*]}" "${0}" "${BASH_SOURCE[0]}"' EXIT INT TERM SIGINT SIGQUIT + +# Trap errors in subshells and functions +set -o errtrace + +# Exit on error. Append '||true' if you expect an error +set -o errexit + +# Use last non-zero exit code in a pipeline +set -o pipefail + +# Make `for f in *.txt` work when `*.txt` matches zero files +# shopt -s nullglob globstar + +# Set IFS to preferred implementation +IFS=$' \n\t' + +# Run in debug mode +# set -o xtrace + +# Initialize color constants +_setColors_ + +# Disallow expansion of unset variables +set -o nounset + +# Force arguments when invoking the script +# [[ $# -eq 0 ]] && _parseOptions_ "-h" + +# Parse arguments passed to script +_parseOptions_ "$@" + +# Create a temp directory '$TMP_DIR' +# _makeTempDir_ "$(basename "$0")" + +# Acquire script lock +# _acquireScriptLock_ + +# Run the main logic script +_mainScript_ + +# Exit cleanly +_safeExit_