diff --git a/simpleScriptTemplate.sh b/simpleScriptTemplate.sh index 30b04d2..231fd45 100644 --- a/simpleScriptTemplate.sh +++ b/simpleScriptTemplate.sh @@ -16,11 +16,6 @@ function mainScript() { } function trapCleanup() { - # trapCleanup Function - # ----------------------------------- - # Any actions that should be taken if the script is prematurely - # exited. Always call this function at the top of your script. - # ----------------------------------- echo "" # Delete temp files, if any if [ -d "${tmpDir}" ] ; then @@ -30,11 +25,6 @@ function trapCleanup() { } function safeExit() { - # safeExit - # ----------------------------------- - # Non destructive exit for when script exits naturally. - # Usage: Add this function at the end of every script. - # ----------------------------------- # Delete temp files, if any if [ -d "${tmpDir}" ] ; then rm -r "${tmpDir}" @@ -67,10 +57,6 @@ blue=$(tput setaf 38) underline=$(tput sgr 0 1) # Set Temp Directory -# ----------------------------------- -# Create temp directory with three random numbers and the process ID -# in the name. This directory is removed automatically at exit. -# ----------------------------------- tmpDir="/tmp/${scriptName}.$RANDOM.$RANDOM.$RANDOM.$$" (umask 077 && mkdir "${tmpDir}") || { die "Could not create temporary directory! Exiting." @@ -79,17 +65,11 @@ tmpDir="/tmp/${scriptName}.$RANDOM.$RANDOM.$RANDOM.$$" # Logging # ----------------------------------- # Log is only used when the '-l' flag is set. -# -# To never save a logfile change variable to '/dev/null' -# Save to Desktop use: $HOME/Desktop/${scriptBasename}.log -# Save to standard user log location use: $HOME/Library/Logs/${scriptBasename}.log -# ----------------------------------- logFile="${HOME}/Library/Logs/${scriptBasename}.log" # Options and Usage # ----------------------------------- -# Print usage usage() { echo -n "${scriptName} [OPTION]... [FILE]... @@ -148,7 +128,7 @@ unset options # Print help if no arguments were passed. # Uncomment to force arguments when invoking the script # ------------------------------------- -# [[ $# -eq 0 ]] && set -- "--help" +[[ $# -eq 0 ]] && set -- "--help" # Read the options and set stuff while [[ $1 = -?* ]]; do @@ -174,12 +154,8 @@ done args+=("$@") -# Logging and Colors +# Logging & Feedback # ----------------------------------------------------- -# Here we set the colors for our script feedback. -# Example usage: success "sometext" -#------------------------------------------------------ - function _alert() { if [ "${1}" = "error" ]; then local color="${bold}${red}"; fi if [ "${1}" = "warning" ]; then local color="${red}"; fi @@ -214,6 +190,40 @@ function input() { local _message="${*}"; echo -n "$(_alert input)"; } function header() { local _message="== ${*} == "; echo -e "$(_alert header)"; } function verbose() { if ${verbose}; then debug "$@"; fi } +# SEEKING CONFIRMATION +# ------------------------------------------------------ +function seek_confirmation() { + # echo "" + input "$@" + if "${force}"; then + notice "Forcing confirmation with '--force' flag set" + else + read -p " (y/n) " -n 1 + echo "" + fi +} +function is_confirmed() { + if "${force}"; then + return 0 + else + if [[ "${REPLY}" =~ ^[Yy]$ ]]; then + return 0 + fi + return 1 + fi +} +function is_not_confirmed() { + if "${force}"; then + return 1 + else + if [[ "${REPLY}" =~ ^[Nn]$ ]]; then + return 0 + fi + return 1 + fi +} + + # Trap bad exits with your cleanup function trap trapCleanup EXIT INT TERM