Skip to content

Instantly share code, notes, and snippets.

@frontrangerider2004
Last active June 24, 2016 15:43
Show Gist options
  • Save frontrangerider2004/500d3ea468819f54ca7767ea9b91525c to your computer and use it in GitHub Desktop.
Save frontrangerider2004/500d3ea468819f54ca7767ea9b91525c to your computer and use it in GitHub Desktop.
#!/bin/bash
####################################################################
# Bash Script Template
# Extend this template to produce a custom bash script that can
# only be run as root, makes temp dirs, and handles ungraceful
# exits by using trap. The script also demonstrates how to accept
# command line arguments.
# NOTE: The run-as-root requirement can easily be removed.
####################################################################
#Global Definitions
DEBUG=true
SCRIPT_NAME="$(basename -- "$0")"
ABS_SCRIPT_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
GETOPT="$(which getopt)"
TMP_DIR=""
SOME_PATH=""
INTEGER_DEFAULT=3
####################################################################
# long
#
# PARAMS: msg, String
#
# Uses printf to print the message with a new line.
####################################################################
function log {
printf "%s\n" "$1"
}
####################################################################
# logD
#
# Params: message, String
# Prints the supplied message to standard out using printf
####################################################################
function logD () {
if [ $DEBUG == true ]; then
printf "%s\n" "$1"
fi
}
####################################################################
# onExit
#
# Performs cleanup in the event of ungraceful exit
####################################################################
function onExit () {
logD "[onExit]"
removeTempDir
# TODO Anything else needing cleanup
echo "################## Finished #########################"
}
###################################################
# createTempDir
# Creates a temporary directory under /tmp using
# the bash built-in mktemp -d.
###################################################
function createTempDir {
logD "[createTempDir]"
TMP_DIR="$(mktemp -d)"
}
####################################################################
# removeTempDir
#
# Determines if a temp dir has been created and then removes it.
####################################################################
function removeTempDir {
logD "[removeTempDir]"
if [ -n "$TMP_DIR" ]; then
logD "[removeTempDir] Found tmp dir $TMP_DIR"
rm -rf "$TMP_DIR" || true #Don't fail the script if this fails
if [ $? -eq 0 ]; then
logD "[removeTempDir] Successfully removed $TMP_DIR"
else
logD "[removeTempDir] Failed to remove $TMP_DIR"
fi
fi
logD "[removeTempDir] Nothing to remove."
}
####################################################################
# assertLast
#
# Checks the supplied return code and exits if it is not zero
# PARAMS: Num - exit code IE $? of the last command
# Num - the error code to use when exiting if the last command failed.
# String - An optional message to log and echo for failed commands
####################################################################
function assertLast () {
logD "[assertLast] $1"
if [ $1 -ne 0 ]; then
if [ -n "$3" ]; then
logD "$3"
fi
exit $2
fi
}
###################################################
# printUsage
# Prints a usage message by catting the description
# into the console.
###################################################
function printUsage {
cat << EOF
Usage: $SCRIPT_NAME [-h][--help] [-t][--test] [-s][--some-path]path
Do some really cool stuff:
-h,--help display this help and exit.
-t,--test print 'Hello World' and exit.
-s,--some-path sets the SOME_PATH variable to the supplied value. Value is required.
EOF
exit -5
}
#############################################################################
# fakeWork
# Runs a sleep loop to simulate work.
#############################################################################
function fakeWork () {
logD "[fakeWork]"
local fakeTry=1
while [ $fakeTry -le 5 ]; do
echo "fake work..."
sleep 1
((fakeTry++))
done
return 0
}
#############################################################################
# checkForRoot
# Make sure only root can run our script
#############################################################################
function checkForRoot {
logD "[checkForRoot]"
if [[ $EUID -ne 0 ]]; then
# Redirect this stdout to stderr
echo "!!!!!! This script must be run as root !!!!!!" 1>&2
exit 1
fi
}
###################################################
# sayHello
# Echos Hello World to the console for testing.
###################################################
function sayHello {
echo "Hello World!"
}
###################################################
# setSomePath
# Parse the
###################################################
function parseSomePath {
logD "[parseSomePath] $1"
if [ -n "$1" ]; then
logD "Found required argument for some-path, arg=$1"
SOME_PATH="$1"
else
printUsage
fi
}
#############################################################################
# parseAbsolute
# PARAMS: arg, Number
# Parse the optional argument for --absolute. Uses a default value if no
# argument is supplied.
#############################################################################
function parseInteger {
logD "[parseInteger] $1"
INTEGER=$INTEGER_DEFAULT
if [ -n "$1" ]; then
logD "Found optional argument for integer, arg=$1"
# Starting at the beginning of the line, find one or more integers
# followed by the end of the line, IE nothing else in between.
local numRegex='^[0-9]+$'
if [[ ! "$1" =~ $numRegex ]]; then
echo "[parseInteger] Error: $1 is not an integer"
exit 1;
fi
INTEGER="$1"
fi
logD "No optional argument for integer found, using default value $INTEGER_DEFAULT"
}
##########################################################################
# getScriptArguments
# Uses getopt to read user supplied arguments and options.
# After reading options/args, any globals are set.
#
# NOTE:
# '-o' followed by single letters are short options
# '--long' followed by comma separated long words are long options
#
# Single colon following short/long opt flag means required argument
# Double colon following short/long opt flag means optional argument
#
# If you use long opts with optional args you must use the = sign or
# specify the arg without a space, IE i=4 or i4. You will have to Parse
# the value off the equal sign if you use that.
##########################################################################
function getScriptArguments {
ARGS=$($GETOPT -o hts:i:: --long help,test,some-path:integer:: -n "${0##*/}" -- "$@")
#Bad arguments
if [ $? -ne 0 ]; then
printUsage
fi
#Attempt to catch null chars
if [ -z "$1" ]; then
printUsage
fi
# If no options were supplied then print usage and exit
if [ $# -eq 0 ]; then
printUsage
fi
logD "[getScriptArguments] ARGS=$ARGS"
eval set -- "$ARGS"
while true; do
case "$1" in
-h|--help) shift; printUsage;;
-t|--test) shift; sayHello;;
-s|--some-path) parseSomePath $2; shift;;
-i|--integer) parseInteger $2; shift 2;; #shift 2 at the end
--) shift; break;;
esac
done
}
###################################################
# START SCRIPT #
###################################################
echo "+++++++++++++++++ $SCRIPT_NAME +++++++++++++++++"
logD "Executing from dir::$ABS_SCRIPT_PATH"
#Trap the exit signal so we can cleanup in case there are errors
trap onExit EXIT
#Uncomment this if you want to check for root
# checkForRoot
#Pass all the script arguments as they were received to the getArgs function
getScriptArguments "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment