mirror of
https://github.com/natelandau/shell-scripting-templates.git
synced 2025-11-09 13:43:46 -05:00
Improve readability
This commit is contained in:
@@ -16,22 +16,22 @@ _mainScript_() {
|
|||||||
} # end _mainScript_
|
} # end _mainScript_
|
||||||
|
|
||||||
# ################################## Flags and defaults
|
# ################################## Flags and defaults
|
||||||
# Script specific
|
# Script specific
|
||||||
|
|
||||||
# Common
|
# Common
|
||||||
LOGFILE="${HOME}/logs/$(basename "$0").log"
|
LOGFILE="${HOME}/logs/$(basename "$0").log"
|
||||||
QUIET=false
|
QUIET=false
|
||||||
LOGLEVEL=ERROR
|
LOGLEVEL=ERROR
|
||||||
VERBOSE=false
|
VERBOSE=false
|
||||||
FORCE=false
|
FORCE=false
|
||||||
DRYRUN=false
|
DRYRUN=false
|
||||||
declare -a ARGS=()
|
declare -a ARGS=()
|
||||||
NOW=$(LC_ALL=C date +"%m-%d-%Y %r") # Returns: 06-14-2015 10:34:40 PM
|
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
|
DATESTAMP=$(LC_ALL=C date +%Y-%m-%d) # Returns: 2015-06-14
|
||||||
HOURSTAMP=$(LC_ALL=C date +%r) # Returns: 10:34:40 PM
|
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
|
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
|
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
|
GMTDATE=$(LC_ALL=C date -u -R | sed 's/\+0000/GMT/') # Returns: Wed, 13 Jan 2016 15:55:29 GMT
|
||||||
|
|
||||||
# ################################## Custom utility functions
|
# ################################## Custom utility functions
|
||||||
|
|
||||||
@@ -99,13 +99,13 @@ _alert_() {
|
|||||||
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
|
fi
|
||||||
|
|
||||||
@@ -123,7 +123,7 @@ _alert_() {
|
|||||||
color="${bold}${tan}"
|
color="${bold}${tan}"
|
||||||
elif [ ${alertType} == "notice" ]; then
|
elif [ ${alertType} == "notice" ]; then
|
||||||
color="${bold}"
|
color="${bold}"
|
||||||
elif [ ${alertType} == "input" ]; then
|
elif [ ${alertType} == "input" ]; then
|
||||||
color="${bold}${underline}"
|
color="${bold}${underline}"
|
||||||
elif [ "${alertType}" = "dryrun" ]; then
|
elif [ "${alertType}" = "dryrun" ]; then
|
||||||
color="${blue}"
|
color="${blue}"
|
||||||
@@ -231,7 +231,7 @@ _safeExit_() {
|
|||||||
# 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
|
||||||
@@ -239,7 +239,7 @@ _safeExit_() {
|
|||||||
fi
|
fi
|
||||||
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
|
if [[ ${1:-} == 1 && -n "$(ls "${TMP_DIR}")" ]]; then
|
||||||
# Do something here to save TMP_DIR on a non-zero script exit for debugging
|
# Do something here to save TMP_DIR on a non-zero script exit for debugging
|
||||||
command rm -r "${TMP_DIR}"
|
command rm -r "${TMP_DIR}"
|
||||||
@@ -273,7 +273,7 @@ _trapCleanup_() {
|
|||||||
|
|
||||||
funcstack="'$(echo "$funcstack" | sed -E 's/ / < /g')'"
|
funcstack="'$(echo "$funcstack" | sed -E 's/ / < /g')'"
|
||||||
|
|
||||||
if [[ "${script##*/}" == "${sourced##*/}" ]]; then
|
if [[ ${script##*/} == "${sourced##*/}" ]]; then
|
||||||
fatal "${7:-} command: '${command}' (line: ${line}) [func: $(_functionStack_)]"
|
fatal "${7:-} command: '${command}' (line: ${line}) [func: $(_functionStack_)]"
|
||||||
else
|
else
|
||||||
fatal "${7:-} command: '${command}' (func: ${funcstack} called at line ${linecallfunc} of '${script##*/}') (line: $line of '${sourced##*/}') "
|
fatal "${7:-} command: '${command}' (func: ${funcstack} called at line ${linecallfunc} of '${script##*/}') (line: $line of '${sourced##*/}') "
|
||||||
@@ -335,7 +335,7 @@ _functionStack_() {
|
|||||||
funcStackResponse=()
|
funcStackResponse=()
|
||||||
for ((_i = 1; _i < ${#BASH_SOURCE[@]}; _i++)); do
|
for ((_i = 1; _i < ${#BASH_SOURCE[@]}; _i++)); do
|
||||||
case "${FUNCNAME[$_i]}" in "_alert_" | "_trapCleanup_" | fatal | error | warning | notice | info | verbose | debug | dryrun | header | success | 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]}"
|
||||||
@@ -435,19 +435,46 @@ EOF
|
|||||||
# ################################## INITIALIZE AND RUN THE SCRIPT
|
# ################################## INITIALIZE AND RUN THE SCRIPT
|
||||||
# (Comment or uncomment the lines below to customize script behavior)
|
# (Comment or uncomment the lines below to customize script behavior)
|
||||||
|
|
||||||
trap '_trapCleanup_ ${LINENO} ${BASH_LINENO} "${BASH_COMMAND}" "${FUNCNAME[*]}" "${0}" "${BASH_SOURCE[0]}"' \
|
trap '_trapCleanup_ ${LINENO} ${BASH_LINENO} "${BASH_COMMAND}" "${FUNCNAME[*]}" "${0}" "${BASH_SOURCE[0]}"' EXIT INT TERM SIGINT SIGQUIT
|
||||||
EXIT INT TERM SIGINT SIGQUIT
|
|
||||||
set -o errtrace # Trap errors in subshells and functions
|
# Trap errors in subshells and functions
|
||||||
set -o errexit # Exit on error. Append '||true' if you expect an error
|
set -o errtrace
|
||||||
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
|
# Exit on error. Append '||true' if you expect an error
|
||||||
IFS=$' \n\t' # Set IFS to preferred implementation
|
set -o errexit
|
||||||
# set -o xtrace # Run in debug mode
|
|
||||||
_setColors_ # Initialize color constants
|
# Use last non-zero exit code in a pipeline
|
||||||
set -o nounset # Disallow expansion of unset variables
|
set -o pipefail
|
||||||
# [[ $# -eq 0 ]] && _parseOptions_ "-h" # Force arguments when invoking the script
|
|
||||||
_parseOptions_ "$@" # Parse arguments passed to script
|
# Make `for f in *.txt` work when `*.txt` matches zero files
|
||||||
# _makeTempDir_ "$(basename "$0")" # Create a temp directory '$TMP_DIR'
|
# shopt -s nullglob globstar
|
||||||
# _acquireScriptLock_ # Acquire script lock
|
|
||||||
_mainScript_ # Run the main logic script
|
# Set IFS to preferred implementation
|
||||||
_safeExit_ # Exit cleanly
|
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_
|
||||||
|
|||||||
Reference in New Issue
Block a user