default flags now true/false and added safeExit.

This commit is contained in:
Nathaniel Landau
2015-06-21 12:24:46 -04:00
parent 2498352fd5
commit 3bce738c34

View File

@@ -5,7 +5,7 @@
# #
version="1.0.0" # Sets version variable version="1.0.0" # Sets version variable
# #
scriptTemplateVersion="1.4.0" # Version of scriptTemplate.sh that this script is based on scriptTemplateVersion="1.5.0" # Version of scriptTemplate.sh that this script is based on
# v1.1.0 - Added 'debug' option # v1.1.0 - Added 'debug' option
# v1.1.1 - Moved all shared variables to Utils # v1.1.1 - Moved all shared variables to Utils
# - Added $PASS variable when -p is passed # - Added $PASS variable when -p is passed
@@ -14,6 +14,10 @@ scriptTemplateVersion="1.4.0" # Version of scriptTemplate.sh that this script is
# v1.3.0 - Can now pass CLI without an option to $args # v1.3.0 - Can now pass CLI without an option to $args
# v1.4.0 - checkDependencies now checks gems and mac apps via # v1.4.0 - checkDependencies now checks gems and mac apps via
# Homebrew cask # Homebrew cask
# v1.5.0 - Now has preferred IFS setting
# - Preset flags now respect true/false
# - Moved 'safeExit' function into template where it should
# have been all along.
# #
# HISTORY: # HISTORY:
# #
@@ -47,23 +51,38 @@ fi
# ----------------------------------- # -----------------------------------
function trapCleanup() { function trapCleanup() {
echo "" echo ""
# Delete temp files, if any
if is_dir "${tmpDir}"; then if is_dir "${tmpDir}"; then
rm -r "${tmpDir}" rm -r "${tmpDir}"
fi fi
die "Exit trapped." # Edit this if you like. die "Exit trapped." # Edit this if you like.
} }
# safeExit
# -----------------------------------
# Non destructive exit for when script exits naturally.
# Usage: Add this function at the end of every script.
# -----------------------------------
function safeExit() {
# Delete temp files, if any
if is_dir "${tmpDir}"; then
rm -r "${tmpDir}"
fi
trap - INT TERM EXIT
exit
}
# Set Flags # Set Flags
# ----------------------------------- # -----------------------------------
# Flags which can be overridden by user input. # Flags which can be overridden by user input.
# Default values are below # Default values are below
# ----------------------------------- # -----------------------------------
quiet=0 quiet=false
printLog=0 printLog=false
verbose=0 verbose=false
force=0 force=false
strict=0 strict=false
debug=0 debug=false
args=() args=()
# Set Temp Directory # Set Temp Directory
@@ -115,9 +134,9 @@ echo -n
usage() { usage() {
echo -n "${scriptName} [OPTION]... [FILE]... echo -n "${scriptName} [OPTION]... [FILE]...
This is my script template. This is a script template. Edit this description to print help to users.
Options: ${bold}Options:${reset}
-u, --username Username for script -u, --username Username for script
-p, --password User password -p, --password User password
--force Skip all user interaction. Implied 'Yes' to all actions. --force Skip all user interaction. Implied 'Yes' to all actions.
@@ -179,12 +198,12 @@ while [[ $1 = -?* ]]; do
-u|--username) shift; username=${1} ;; -u|--username) shift; username=${1} ;;
-p|--password) shift; echo "Enter Pass: "; stty -echo; read PASS; stty echo; -p|--password) shift; echo "Enter Pass: "; stty -echo; read PASS; stty echo;
echo ;; echo ;;
-v|--verbose) verbose=1 ;; -v|--verbose) verbose=true ;;
-l|--log) printLog=1 ;; -l|--log) printLog=true ;;
-q|--quiet) quiet=1 ;; -q|--quiet) quiet=true ;;
-s|--strict) strict=1;; -s|--strict) strict=true;;
-d|--debug) debug=1;; -d|--debug) debug=true;;
--force) force=1 ;; --force) force=true ;;
--endopts) shift; break ;; --endopts) shift; break ;;
*) die "invalid option: '$1'." ;; *) die "invalid option: '$1'." ;;
esac esac
@@ -210,25 +229,24 @@ args+=("$@")
# Trap bad exits with your cleanup function # Trap bad exits with your cleanup function
trap trapCleanup EXIT INT TERM trap trapCleanup EXIT INT TERM
# Set IFS to preferred implementation
IFS=$'\n\t'
# Exit on error. Append '||true' when you run the script if you expect an error. # Exit on error. Append '||true' when you run the script if you expect an error.
set -o errexit set -o errexit
# Run in debug mode, if set # Run in debug mode, if set
if [ "${debug}" == "1" ]; then if ${debug}; then set -x ; fi
set -x
fi
# Exit on empty variable # Exit on empty variable
if [ "${strict}" == "1" ]; then if ${strict}; then set -o nounset ; fi
set -o nounset
fi
# Bash will remember & return the highest exitcode in a chain of pipes. # Bash will remember & return the highest exitcode in a chain of pipes.
# This way you can catch the error in case mysqldump fails in `mysqldump |gzip`, for example. # This way you can catch the error in case mysqldump fails in `mysqldump |gzip`, for example.
set -o pipefail set -o pipefail
# Invoke the checkDependenices function to test for Bash packages # Invoke the checkDependenices function to test for Bash packages. Uncomment if needed.
checkDependencies # checkDependencies
# Run your script # Run your script
mainScript mainScript