Refactored to be based on my script template

This commit is contained in:
Nathaniel Landau
2015-02-07 14:23:17 -05:00
parent f8e603229f
commit dad1c674c2
6 changed files with 1763 additions and 464 deletions

View File

@@ -1,11 +1,106 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# ##################################################
# My Generic BASH script template
#
version="1.0.0" # Sets version variable for this script
#
scriptTemplateVersion="1.0.1" # Version of scriptTemplate.sh
# that this script is based on
#
# A Bash script boilerplate. Allows for common functions, logging, tmp
# file creation, CL option passing, and more.
#
# For logging levels use the following functions:
# - header: Prints a script header
# - input: Ask for user input
# - success: Print script success
# - info: Print information to the user
# - notice: Notify the user of something
# - warning: Warn the user of something
# - error: Print a non-fatal error
# - die: A fatal error. Will exit the script
# - debug: Debug information
# - verbose: Debug info only printed when 'verbose' flag is set to 'true'.
#
# HISTORY:
#
# * 2015-02-07 - v1.0.0 - First Creation
#
# ##################################################
# Source Scripting Utilities
# -----------------------------------
# If these can't be found, update the path to the file
# -----------------------------------
if [ -f "../lib/utils.sh" ]; then if [ -f "../lib/utils.sh" ]; then
source "../lib/utils.sh" source "../lib/utils.sh"
else else
echo "You must have utils.sh to run. Exiting." echo "Please find the file util.sh and add a reference to it in this script. Exiting."
exit exit 1
fi fi
# trapCleanup Function
# -----------------------------------
# Any actions that should be taken if the script is prematurely
# exited. Always call this function at the top of your script.
# -----------------------------------
function trapCleanup() {
echo ""
if is_dir "${tmpDir}"; then
rm -r "${tmpDir}"
fi
die "Exit trapped." # Edit this if you like.
}
# Set Flags
# -----------------------------------
# Flags which can be overridden by user input.
# Default values are below
# -----------------------------------
quiet=0
printLog=0
verbose=0
force=0
strict=0
# Set Local Variables
# -----------------------------------
# A set of variables used by many scripts
# -----------------------------------
# Set Script name and location variables
scriptName=`basename ${0}` # Full name
scriptBasename="$(basename ${scriptName} .sh)" # Strips '.sh' from name
scriptPath="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Set time stamp
now=$(date +"%m-%d-%Y %r")
# Set hostname
thisHost=$(hostname)
# 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."
}
# 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"
function mainScript() {
############## Begin Script Here ###################
header "Beginning ${scriptName}"
# Set Variables # Set Variables
LISTINSTALLED="brew cask list" LISTINSTALLED="brew cask list"
@@ -72,6 +167,123 @@ hasHomebrew
hasCasks hasCasks
brewMaintenance brewMaintenance
doInstall doInstall
brewCleanup
# Cleanup Homebrew
brew cask cleanup header "Completed ${scriptName}"
############## End Script Here ###################
}
############## Begin Options and Usage ###################
# Print usage
usage() {
echo -n "${scriptName} [OPTION]... [FILE]...
This is my script template.
Options:
-u, --username Username for script
-p, --password User password
-f, --force Skip all user interaction. Implied 'Yes' to all actions
-q, --quiet Quiet (no output)
-l, --log Print log to file
-s, --strict Exit script with null variables. i.e 'set -o nounset'
-v, --verbose Output more information. (Items echoed to 'verbose')
-h, --help Display this help and exit
--version Output version information and exit
"
}
# Iterate over options breaking -ab into -a -b when needed and --foo=bar into
# --foo bar
optstring=h
unset options
while (($#)); do
case $1 in
# If option is of type -ab
-[!-]?*)
# Loop over each character starting with the second
for ((i=1; i < ${#1}; i++)); do
c=${1:i:1}
# Add current char to options
options+=("-$c")
# If option takes a required argument, and it's not the last char make
# the rest of the string its argument
if [[ $optstring = *"$c:"* && ${1:i+1} ]]; then
options+=("${1:i+1}")
break
fi
done
;;
# If option is of type --foo=bar
--?*=*) options+=("${1%%=*}" "${1#*=}") ;;
# add --endopts for --
--) options+=(--endopts) ;;
# Otherwise, nothing special
*) options+=("$1") ;;
esac
shift
done
set -- "${options[@]}"
unset options
# Print help if no arguments were passed.
# Uncomment to force arguments when invoking the script
# [[ $# -eq 0 ]] && set -- "--help"
# Read the options and set stuff
while [[ $1 = -?* ]]; do
case $1 in
-h|--help) usage >&2; safeExit ;;
--version) echo "$(basename $0) $version"; safeExit ;;
-u|--username) shift; username=$1 ;;
-p|--password) shift; password=$1 ;;
-v|--verbose) verbose=1 ;;
-l|--log) printLog=1 ;;
-q|--quiet) quiet=1 ;;
-s|--strict) strict=1;;
-f|--force) force=1 ;;
--endopts) shift; break ;;
*) die "invalid option: '$1'." ;;
esac
shift
done
############## End Options and Usage ###################
# ############# ############# #############
# ## TIME TO RUN THE SCRIPT ##
# ## ##
# ## You shouldn't need to edit anything ##
# ## beneath this line ##
# ## ##
# ############# ############# #############
# Trap bad exits with your cleanup function
trap trapCleanup EXIT INT TERM
# Exit on error. Append ||true if you expect an error.
set -o errexit
# Exit on empty variable
if [ "${strict}" = "1" ]; then
set -o nounset
fi
# 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`
set -o pipefail
mainScript # Run your script
safeExit # Exit cleanly

View File

@@ -1,34 +1,253 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# This script tests for the existence of certain files in Dropbox. # ##################################################
# It is used to keep a current list in the mackup.sh script. # My Generic BASH script template
#
version="1.0.0" # Sets version variable for this script
#
scriptTemplateVersion="1.1.0" # Version of scriptTemplate.sh that this script is based on
# v.1.1.0 - Added 'debug' option
#
# A Script to test if certain files from Dropbox are synced to the local hard drive.
# This script relies on a text file containing paths to documents.
#
# For logging levels use the following functions:
# - header: Prints a script header
# - input: Ask for user input
# - success: Print script success
# - info: Print information to the user
# - notice: Notify the user of something
# - warning: Warn the user of something
# - error: Print a non-fatal error
# - die: A fatal error. Will exit the script
# - debug: Debug information
# - verbose: Debug info only printed when 'verbose' flag is set to 'true'.
#
# HISTORY:
#
# * 2015-02-07 - v1.0.0 - First Creation
#
# ##################################################
# Source Scripting Utilities
# -----------------------------------
# If these can't be found, update the path to the file
# -----------------------------------
if [ -f "../lib/utils.sh" ]; then if [ -f "../lib/utils.sh" ]; then
source "../lib/utils.sh" source "../lib/utils.sh"
else else
echo "You must have utils.sh to run. Exiting." echo "Please find the file util.sh and add a reference to it in this script. Exiting."
exit 0 exit 1
fi fi
# trapCleanup Function
# -----------------------------------
# Any actions that should be taken if the script is prematurely
# exited. Always call this function at the top of your script.
# -----------------------------------
function trapCleanup() {
echo ""
if is_dir "${tmpDir}"; then
rm -r "${tmpDir}"
fi
die "Exit trapped." # Edit this if you like.
}
# Set Flags
# -----------------------------------
# Flags which can be overridden by user input.
# Default values are below
# -----------------------------------
quiet=0
printLog=0
verbose=0
force=0
strict=0
debug=0
# Set Local Variables
# -----------------------------------
# A set of variables used by many scripts
# -----------------------------------
# Set Script name and location variables
scriptName=`basename ${0}` # Full name
scriptBasename="$(basename ${scriptName} .sh)" # Strips '.sh' from name
scriptPath="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Set time stamp
now=$(date +"%m-%d-%Y %r")
# Set hostname
thisHost=$(hostname)
# 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."
}
# 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"
function mainScript() {
############## Begin Script Here ###################
header "Beginning ${scriptBasename}"
# Variables from config file # Variables from config file
if is_file "../etc/mackup.cfg"; then if is_file "../etc/mackup.cfg"; then
source "../etc/mackup.cfg" verbose "Sourcing ../etc/mackup.cfg" && source "../etc/mackup.cfg"
TESTFILE="$TESTCFG" TESTFILE="${TESTCFG}"
else else
die "Can not run without config file" die "Can not run without config file. Please locate mackup.cfg"
fi fi
if is_not_file "$TESTFILE"; then if is_not_file "${TESTFILE}"; then
die "Could not find $TESTFILE. Exiting." die "Could not find ${TESTFILE}. Exiting."
else else
notice "Confirming that Dropbox has synced..." notice "Confirming that Dropbox has synced..."
while IFS= read -r file while IFS= read -r file
do do
while [ ! -e $HOME/"$file" ] ; while [ ! -e $HOME/"${file}" ] ;
do do
notice "Waiting for Dropbox to Sync files." warning "...Waiting for Dropbox to Sync ${file}."
sleep 10 sleep 10
done done
success "Found $file" success "Found ${file}"
done < "$TESTFILE" done < "${TESTFILE}"
fi fi
header "Completed ${scriptBasename}"
############## End Script Here ###################
}
############## Begin Options and Usage ###################
# Print usage
usage() {
echo -n "${scriptName} [OPTION]... [FILE]...
This is my script template.
Options:
-u, --username Username for script
-p, --password User password
-f, --force Skip all user interaction. Implied 'Yes' to all actions
-q, --quiet Quiet (no output)
-l, --log Print log to file
-s, --strict Exit script with null variables. i.e 'set -o nounset'
-v, --verbose Output more information. (Items echoed to 'verbose')
-d, --debug Runs script in BASH debug mode (set -x)
-h, --help Display this help and exit
--version Output version information and exit
"
}
# Iterate over options breaking -ab into -a -b when needed and --foo=bar into
# --foo bar
optstring=h
unset options
while (($#)); do
case $1 in
# If option is of type -ab
-[!-]?*)
# Loop over each character starting with the second
for ((i=1; i < ${#1}; i++)); do
c=${1:i:1}
# Add current char to options
options+=("-$c")
# If option takes a required argument, and it's not the last char make
# the rest of the string its argument
if [[ $optstring = *"$c:"* && ${1:i+1} ]]; then
options+=("${1:i+1}")
break
fi
done
;;
# If option is of type --foo=bar
--?*=*) options+=("${1%%=*}" "${1#*=}") ;;
# add --endopts for --
--) options+=(--endopts) ;;
# Otherwise, nothing special
*) options+=("$1") ;;
esac
shift
done
set -- "${options[@]}"
unset options
# Print help if no arguments were passed.
# Uncomment to force arguments when invoking the script
# [[ $# -eq 0 ]] && set -- "--help"
# Read the options and set stuff
while [[ $1 = -?* ]]; do
case $1 in
-h|--help) usage >&2; safeExit ;;
--version) echo "$(basename $0) $version"; safeExit ;;
-u|--username) shift; username=$1 ;;
-p|--password) shift; password=$1 ;;
-v|--verbose) verbose=1 ;;
-l|--log) printLog=1 ;;
-q|--quiet) quiet=1 ;;
-s|--strict) strict=1;;
-d|--debug) debug=1;;
-f|--force) force=1 ;;
--endopts) shift; break ;;
*) die "invalid option: '$1'." ;;
esac
shift
done
############## End Options and Usage ###################
# ############# ############# #############
# ## TIME TO RUN THE SCRIPT ##
# ## ##
# ## You shouldn't need to edit anything ##
# ## beneath this line ##
# ## ##
# ############# ############# #############
# Trap bad exits with your cleanup function
trap trapCleanup EXIT INT TERM
# Exit on error. Append ||true if you expect an error.
set -o errexit
# Run in debug mode, if set
if [ "${debug}" == "1" ]; then
set -x
fi
# Exit on empty variable
if [ "${strict}" == "1" ]; then
set -o nounset
fi
# 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`
set -o pipefail
mainScript # Run your script
safeExit # Exit cleanly

View File

@@ -1,26 +1,106 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# ################################################## # ##################################################
# This script will in restore a new computer from a mackup backup # My Generic BASH script template
# using Dropbox. It requires a config file in '../etc' to run.
# #
# HISTORY version="1.0.0" # Sets version variable for this script
# * 2015-01-02 - Initial creation
# #
# NOTE: This script ensure that a Dropbox sync has been completed scriptTemplateVersion="1.1.0" # Version of scriptTemplate.sh
# by keeping a list of files up-to-date. These files are listed (one # that this script is based on
# per line) in a text file. If this file falls out of date, this #
# script will never run # A Bash script boilerplate. Allows for common functions, logging, tmp
# file creation, CL option passing, and more.
#
# For logging levels use the following functions:
# - header: Prints a script header
# - input: Ask for user input
# - success: Print script success
# - info: Print information to the user
# - notice: Notify the user of something
# - warning: Warn the user of something
# - error: Print a non-fatal error
# - die: A fatal error. Will exit the script
# - debug: Debug information
# - verbose: Debug info only printed when 'verbose' flag is set to 'true'.
#
# HISTORY:
#
# * 2015-02-07 - v1.0.0 - First Creation
# #
# ################################################## # ##################################################
# Source utils # Source Scripting Utilities
# -----------------------------------
# If these can't be found, update the path to the file
# -----------------------------------
if [ -f "../lib/utils.sh" ]; then if [ -f "../lib/utils.sh" ]; then
source "../lib/utils.sh" source "../lib/utils.sh"
else else
echo "You must have utils.sh to run. Exiting." echo "Please find the file util.sh and add a reference to it in this script. Exiting."
exit 0 exit 1
fi fi
# trapCleanup Function
# -----------------------------------
# Any actions that should be taken if the script is prematurely
# exited. Always call this function at the top of your script.
# -----------------------------------
function trapCleanup() {
echo ""
if is_dir "${tmpDir}"; then
rm -r "${tmpDir}"
fi
die "Exit trapped." # Edit this if you like.
}
# Set Flags
# -----------------------------------
# Flags which can be overridden by user input.
# Default values are below
# -----------------------------------
quiet=0
printLog=0
verbose=0
force=0
strict=0
debug=0
# Set Local Variables
# -----------------------------------
# A set of variables used by many scripts
# -----------------------------------
# Set Script name and location variables
scriptName=`basename ${0}` # Full name
scriptBasename="$(basename ${scriptName} .sh)" # Strips '.sh' from name
scriptPath="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Set time stamp
now=$(date +"%m-%d-%Y %r")
# Set hostname
thisHost=$(hostname)
# 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."
}
# 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"
function mainScript() {
############## Begin Script Here ###################
header "Beginning ${scriptName}"
# Variables from config file # Variables from config file
if is_file "../etc/mackup.cfg"; then if is_file "../etc/mackup.cfg"; then
@@ -28,7 +108,7 @@ if is_file "../etc/mackup.cfg"; then
MACKUPDIR="$DIRCFG" MACKUPDIR="$DIRCFG"
TESTFILE="$TESTCFG" TESTFILE="$TESTCFG"
else else
die "Can not run without config file" die "Can not run without config file. Please find mackup.cfg"
fi fi
#Run Dropbox checking function #Run Dropbox checking function
@@ -38,53 +118,182 @@ hasDropbox
# IMPORTANT: As time moves, some of these files may be deleted. Ensure this list is kept up-to-date. # IMPORTANT: As time moves, some of these files may be deleted. Ensure this list is kept up-to-date.
# This can be tested by running dropboxFileTest.sh script. # This can be tested by running dropboxFileTest.sh script.
if is_not_file "$TESTFILE"; then if [ "${force}" = "0" ]; then #Bypass the Dropbox test when script is forced
die "Could not find $TESTFILE. Exiting." if is_not_file "${TESTFILE}"; then
else die "Could not find ${TESTFILE}. Exiting."
notice "Confirming that Dropbox has synced..." else
while IFS= read -r file notice "Confirming that Dropbox has synced..."
do while IFS= read -r file
while [ ! -e $HOME/"$file" ] ;
do do
notice "Waiting for Dropbox to Sync files." while [ ! -e $HOME/"${file}" ] ;
sleep 10 do
done info "...Waiting for Dropbox to Sync files."
done < "$TESTFILE" sleep 10
done
done < "${TESTFILE}"
fi
#Add some additional time just to be sure....
info "...Waiting for Dropbox to Sync files."
sleep 10
info "...Waiting for Dropbox to Sync files."
sleep 10
# Sync Complete
success "Hooray! Dropbox has synced the necessary files."
fi fi
#Add some additional time just to be sure....
notice "Waiting for Dropbox to Sync files."
sleep 10
notice "Waiting for Dropbox to Sync files."
sleep 10
notice "Waiting for Dropbox to Sync files."
sleep 10
# Sync Complete
success "Hooray! Dropbox has synced the necessary files."
if type_not_exists 'mackup'; then if type_not_exists 'mackup'; then
info "Run 'brew install mackup' or run the Homebrew setup script." warning "Run 'brew install mackup' or run the Homebrew setup script."
die "MACKUP NOT FOUND." die "MACKUP NOT FOUND."
else
verbose "Mackup is installed."
fi fi
# upgrade mackup. Don't display in terminal
brew upgrade mackup >/dev/null 2>&1
notice "Checking for Mackup config files..." notice "Checking for Mackup config files..."
if is_not_symlink ""$HOME"/.mackup"; then if is_not_symlink "$HOME/.mackup"; then
ln -s "$MACKUPDIR"/.mackup "$HOME"/.mackup notice "Symlinking ~/.mackup"
ln -s "${MACKUPDIR}"/.mackup "$HOME"/.mackup
else
verbose "~/.mackup is symlinked"
fi fi
if is_not_symlink ""$HOME"/.mackup.cfg"; then if is_not_symlink "${HOME}/.mackup.cfg"; then
ln -s "$MACKUPDIR"/.mackup.cfg "$HOME"/.mackup.cfg notice "Symlinking ~/.mackup.cfg"
ln -s "${MACKUPDIR}"/.mackup.cfg "${HOME}"/.mackup.cfg
else
verbose "~/.mackup.cfg is symlinked"
fi fi
success "Mackup config files linked." success "Mackup config files are symlinked."
seek_confirmation "Run Mackup Restore?" seek_confirmation "Run Mackup Restore?"
if is_confirmed; then if is_confirmed; then
mackup restore verbose "Running mackup restore" && mackup restore
header "All Done." header "All Done."
else
notice "Exiting"
exit 0
fi fi
header "Completed ${scriptName}"
############## End Script Here ###################
}
############## Begin Options and Usage ###################
# Print usage
usage() {
echo -n "${scriptName} [OPTION]... [FILE]...
This is my script template.
Options:
-u, --username Username for script
-p, --password User password
-f, --force Skip all user interaction. Implied 'Yes' to all actions
-q, --quiet Quiet (no output)
-l, --log Print log to file
-s, --strict Exit script with null variables. i.e 'set -o nounset'
-v, --verbose Output more information. (Items echoed to 'verbose')
-d, --debug Runs script in BASH debug mode (set-x)
-h, --help Display this help and exit
--version Output version information and exit
"
}
# Iterate over options breaking -ab into -a -b when needed and --foo=bar into
# --foo bar
optstring=h
unset options
while (($#)); do
case $1 in
# If option is of type -ab
-[!-]?*)
# Loop over each character starting with the second
for ((i=1; i < ${#1}; i++)); do
c=${1:i:1}
# Add current char to options
options+=("-$c")
# If option takes a required argument, and it's not the last char make
# the rest of the string its argument
if [[ $optstring = *"$c:"* && ${1:i+1} ]]; then
options+=("${1:i+1}")
break
fi
done
;;
# If option is of type --foo=bar
--?*=*) options+=("${1%%=*}" "${1#*=}") ;;
# add --endopts for --
--) options+=(--endopts) ;;
# Otherwise, nothing special
*) options+=("$1") ;;
esac
shift
done
set -- "${options[@]}"
unset options
# Print help if no arguments were passed.
# Uncomment to force arguments when invoking the script
# [[ $# -eq 0 ]] && set -- "--help"
# Read the options and set stuff
while [[ $1 = -?* ]]; do
case $1 in
-h|--help) usage >&2; safeExit ;;
--version) echo "$(basename $0) $version"; safeExit ;;
-u|--username) shift; username=$1 ;;
-p|--password) shift; password=$1 ;;
-v|--verbose) verbose=1 ;;
-l|--log) printLog=1 ;;
-q|--quiet) quiet=1 ;;
-s|--strict) strict=1;;
-d|--debug) debug=1;;
-f|--force) force=1 ;;
--endopts) shift; break ;;
*) die "invalid option: '$1'." ;;
esac
shift
done
############## End Options and Usage ###################
# ############# ############# #############
# ## TIME TO RUN THE SCRIPT ##
# ## ##
# ## You shouldn't need to edit anything ##
# ## beneath this line ##
# ## ##
# ############# ############# #############
# Run in debug mode, if set
if [ "${debug}" == "1" ]; then
set -x
fi
# Trap bad exits with your cleanup function
trap trapCleanup EXIT INT TERM
# Exit on error. Append ||true if you expect an error.
set -o errexit
# Exit on empty variable
if [ "${strict}" = "1" ]; then
set -o nounset
fi
# 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`
set -o pipefail
mainScript # Run your script
safeExit # Exit cleanly

File diff suppressed because it is too large Load Diff

View File

@@ -1,11 +1,107 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# ##################################################
# My Generic BASH script template
#
version="1.0.0" # Sets version variable for this script
#
scriptTemplateVersion="1.1.0" # Version of scriptTemplate.sh that this script is based on
# v.1.1.0 - Added 'debug' option
#
# A Bash script boilerplate. Allows for common functions, logging, tmp
# file creation, CL option passing, and more.
#
# For logging levels use the following functions:
# - header: Prints a script header
# - input: Ask for user input
# - success: Print script success
# - info: Print information to the user
# - notice: Notify the user of something
# - warning: Warn the user of something
# - error: Print a non-fatal error
# - die: A fatal error. Will exit the script
# - debug: Debug information
# - verbose: Debug info only printed when 'verbose' flag is set to 'true'.
#
# HISTORY:
#
# * DATE - v1.0.0 - First Creation
#
# ##################################################
# Source Scripting Utilities
# -----------------------------------
# If these can't be found, update the path to the file
# -----------------------------------
if [ -f "../lib/utils.sh" ]; then if [ -f "../lib/utils.sh" ]; then
source "../lib/utils.sh" source "../lib/utils.sh"
else else
echo "You must have utils.sh to run. Exiting." echo "Please find the file util.sh and add a reference to it in this script. Exiting."
exit exit 1
fi fi
# trapCleanup Function
# -----------------------------------
# Any actions that should be taken if the script is prematurely
# exited. Always call this function at the top of your script.
# -----------------------------------
function trapCleanup() {
echo ""
if is_dir "${tmpDir}"; then
rm -r "${tmpDir}"
fi
die "Exit trapped." # Edit this if you like.
}
# Set Flags
# -----------------------------------
# Flags which can be overridden by user input.
# Default values are below
# -----------------------------------
quiet=0
printLog=0
verbose=0
force=0
strict=0
debug=0
# Set Local Variables
# -----------------------------------
# A set of variables used by many scripts
# -----------------------------------
# Set Script name and location variables
scriptName=`basename ${0}` # Full name
scriptBasename="$(basename ${scriptName} .sh)" # Strips '.sh' from name
scriptPath="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Set time stamp
now=$(date +"%m-%d-%Y %r")
# Set hostname
thisHost=$(hostname)
# 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."
}
# 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"
function mainScript() {
############## Begin Script Here ###################
header "Beginning ${scriptBasename}"
# Set Variables # Set Variables
LISTINSTALLED="gem list | awk '{print $1}'" LISTINSTALLED="gem list | awk '{print $1}'"
@@ -59,5 +155,132 @@ hasHomebrew
doInstall doInstall
#update gems #update gems
notice "Updating installed Gems"
gem update --system gem update --system
gem update gem update
header "Ending ${scriptBasename}"
############## End Script Here ###################
}
############## Begin Options and Usage ###################
# Print usage
usage() {
echo -n "${scriptName} [OPTION]... [FILE]...
This is my script template.
Options:
-u, --username Username for script
-p, --password User password
-f, --force Skip all user interaction. Implied 'Yes' to all actions
-q, --quiet Quiet (no output)
-l, --log Print log to file
-s, --strict Exit script with null variables. i.e 'set -o nounset'
-v, --verbose Output more information. (Items echoed to 'verbose')
-d, --debug Runs script in BASH debug mode (set -x)
-h, --help Display this help and exit
--version Output version information and exit
"
}
# Iterate over options breaking -ab into -a -b when needed and --foo=bar into
# --foo bar
optstring=h
unset options
while (($#)); do
case $1 in
# If option is of type -ab
-[!-]?*)
# Loop over each character starting with the second
for ((i=1; i < ${#1}; i++)); do
c=${1:i:1}
# Add current char to options
options+=("-$c")
# If option takes a required argument, and it's not the last char make
# the rest of the string its argument
if [[ $optstring = *"$c:"* && ${1:i+1} ]]; then
options+=("${1:i+1}")
break
fi
done
;;
# If option is of type --foo=bar
--?*=*) options+=("${1%%=*}" "${1#*=}") ;;
# add --endopts for --
--) options+=(--endopts) ;;
# Otherwise, nothing special
*) options+=("$1") ;;
esac
shift
done
set -- "${options[@]}"
unset options
# Print help if no arguments were passed.
# Uncomment to force arguments when invoking the script
# [[ $# -eq 0 ]] && set -- "--help"
# Read the options and set stuff
while [[ $1 = -?* ]]; do
case $1 in
-h|--help) usage >&2; safeExit ;;
--version) echo "$(basename $0) $version"; safeExit ;;
-u|--username) shift; username=$1 ;;
-p|--password) shift; password=$1 ;;
-v|--verbose) verbose=1 ;;
-l|--log) printLog=1 ;;
-q|--quiet) quiet=1 ;;
-s|--strict) strict=1;;
-d|--debug) debug=1;;
-f|--force) force=1 ;;
--endopts) shift; break ;;
*) die "invalid option: '$1'." ;;
esac
shift
done
############## End Options and Usage ###################
# ############# ############# #############
# ## TIME TO RUN THE SCRIPT ##
# ## ##
# ## You shouldn't need to edit anything ##
# ## beneath this line ##
# ## ##
# ############# ############# #############
# Trap bad exits with your cleanup function
trap trapCleanup EXIT INT TERM
# Exit on error. Append ||true if you expect an error.
set -o errexit
# Run in debug mode, if set
if [ "${debug}" == "1" ]; then
set -x
fi
# Exit on empty variable
if [ "${strict}" == "1" ]; then
set -o nounset
fi
# 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`
set -o pipefail
mainScript # Run your script
safeExit # Exit cleanly

View File

@@ -1,37 +1,253 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# This script creates public SSH Keys and sends them to Github # ##################################################
# My Generic BASH script template
#
version="1.0.0" # Sets version variable for this script
#
scriptTemplateVersion="1.1.0" # Version of scriptTemplate.sh that this script is based on
# v.1.1.0 - Added 'debug' option
#
# A Bash script boilerplate. Allows for common functions, logging, tmp
# file creation, CL option passing, and more.
#
# For logging levels use the following functions:
# - header: Prints a script header
# - input: Ask for user input
# - success: Print script success
# - info: Print information to the user
# - notice: Notify the user of something
# - warning: Warn the user of something
# - error: Print a non-fatal error
# - die: A fatal error. Will exit the script
# - debug: Debug information
# - verbose: Debug info only printed when 'verbose' flag is set to 'true'.
#
# HISTORY:
#
# * DATE - v1.0.0 - First Creation
#
# ##################################################
# Source Scripting Utilities
# -----------------------------------
# If these can't be found, update the path to the file
# -----------------------------------
if [ -f "../lib/utils.sh" ]; then if [ -f "../lib/utils.sh" ]; then
source "../lib/utils.sh" source "../lib/utils.sh"
else else
echo "You must have utils.sh to run. Exiting." echo "Please find the file util.sh and add a reference to it in this script. Exiting."
exit exit 1
fi fi
# trapCleanup Function
# -----------------------------------
# Any actions that should be taken if the script is prematurely
# exited. Always call this function at the top of your script.
# -----------------------------------
function trapCleanup() {
echo ""
if is_dir "${tmpDir}"; then
rm -r "${tmpDir}"
fi
die "Exit trapped." # Edit this if you like.
}
header "Running : SSH CONFIG" # Set Flags
# -----------------------------------
# Flags which can be overridden by user input.
# Default values are below
# -----------------------------------
quiet=0
printLog=0
verbose=0
force=0
strict=0
debug=0
success "Checking for SSH key in ~/.ssh/id_rsa.pub, generating one if it doesn't exist ..."
# Set Local Variables
# -----------------------------------
# A set of variables used by many scripts
# -----------------------------------
# Set Script name and location variables
scriptName=`basename ${0}` # Full name
scriptBasename="$(basename ${scriptName} .sh)" # Strips '.sh' from name
scriptPath="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Set time stamp
now=$(date +"%m-%d-%Y %r")
# Set hostname
thisHost=$(hostname)
# 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."
}
# 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"
function mainScript() {
############## Begin Script Here ###################
header "Beginning ${scriptBasename}"
notice "Checking for SSH key in ~/.ssh/id_rsa.pub, generating one if it doesn't exist."
[[ -f ~/.ssh/id_rsa.pub ]] || ssh-keygen -t rsa [[ -f ~/.ssh/id_rsa.pub ]] || ssh-keygen -t rsa
success "Copying public key to clipboard." notice "Copying public key to clipboard."
[[ -f ~/.ssh/id_rsa.pub ]] && cat ~/.ssh/id_rsa.pub | pbcopy [[ -f ~/.ssh/id_rsa.pub ]] && cat ~/.ssh/id_rsa.pub | pbcopy
# Add SSH keys to Github # Add SSH keys to Github
header "Github integration" info "Running Github integration"
seek_confirmation "Open https://github.com/account/ssh in your browser?" seek_confirmation "Open https://github.com/account/ssh in your browser?"
if is_confirmed; then if is_confirmed; then
success "Copying public key to clipboard." notice "Copying public key to clipboard."
[[ -f ~/.ssh/id_rsa.pub ]] && cat ~/.ssh/id_rsa.pub | pbcopy [[ -f ~/.ssh/id_rsa.pub ]] && cat ~/.ssh/id_rsa.pub | pbcopy
open https://github.com/account/ssh open https://github.com/account/ssh
seek_confirmation "Test Github Authentication via ssh?" seek_confirmation "Test Github Authentication via ssh?"
if is_confirmed; then if is_confirmed; then
notice "Testing..." notice "Testing..."
ssh -T git@github.com ssh -T git@github.com
fi fi
fi fi
header "Completed : SSH CONFIG" header "Completed ${scriptBasename}"
############## End Script Here ###################
}
############## Begin Options and Usage ###################
# Print usage
usage() {
echo -n "${scriptName} [OPTION]... [FILE]...
This is my script template.
Options:
-u, --username Username for script
-p, --password User password
-f, --force Skip all user interaction. Implied 'Yes' to all actions
-q, --quiet Quiet (no output)
-l, --log Print log to file
-s, --strict Exit script with null variables. i.e 'set -o nounset'
-v, --verbose Output more information. (Items echoed to 'verbose')
-d, --debug Runs script in BASH debug mode (set -x)
-h, --help Display this help and exit
--version Output version information and exit
"
}
# Iterate over options breaking -ab into -a -b when needed and --foo=bar into
# --foo bar
optstring=h
unset options
while (($#)); do
case $1 in
# If option is of type -ab
-[!-]?*)
# Loop over each character starting with the second
for ((i=1; i < ${#1}; i++)); do
c=${1:i:1}
# Add current char to options
options+=("-$c")
# If option takes a required argument, and it's not the last char make
# the rest of the string its argument
if [[ $optstring = *"$c:"* && ${1:i+1} ]]; then
options+=("${1:i+1}")
break
fi
done
;;
# If option is of type --foo=bar
--?*=*) options+=("${1%%=*}" "${1#*=}") ;;
# add --endopts for --
--) options+=(--endopts) ;;
# Otherwise, nothing special
*) options+=("$1") ;;
esac
shift
done
set -- "${options[@]}"
unset options
# Print help if no arguments were passed.
# Uncomment to force arguments when invoking the script
# [[ $# -eq 0 ]] && set -- "--help"
# Read the options and set stuff
while [[ $1 = -?* ]]; do
case $1 in
-h|--help) usage >&2; safeExit ;;
--version) echo "$(basename $0) $version"; safeExit ;;
-u|--username) shift; username=$1 ;;
-p|--password) shift; password=$1 ;;
-v|--verbose) verbose=1 ;;
-l|--log) printLog=1 ;;
-q|--quiet) quiet=1 ;;
-s|--strict) strict=1;;
-d|--debug) debug=1;;
-f|--force) force=1 ;;
--endopts) shift; break ;;
*) die "invalid option: '$1'." ;;
esac
shift
done
############## End Options and Usage ###################
# ############# ############# #############
# ## TIME TO RUN THE SCRIPT ##
# ## ##
# ## You shouldn't need to edit anything ##
# ## beneath this line ##
# ## ##
# ############# ############# #############
# Trap bad exits with your cleanup function
trap trapCleanup EXIT INT TERM
# Exit on error. Append ||true if you expect an error.
set -o errexit
# Run in debug mode, if set
if [ "${debug}" == "1" ]; then
set -x
fi
# Exit on empty variable
if [ "${strict}" == "1" ]; then
set -o nounset
fi
# 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`
set -o pipefail
mainScript # Run your script
safeExit # Exit cleanly