Skip to content

Instantly share code, notes, and snippets.

@JohnScottUK
Last active July 16, 2021 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JohnScottUK/5288d8ef6f6a0e3c15bf190fb0378b3d to your computer and use it in GitHub Desktop.
Save JohnScottUK/5288d8ef6f6a0e3c15bf190fb0378b3d to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -o errexit -o errtrace -o nounset -o pipefail # Robust scripting.
# Find real script name and location...
cd .;origDir="$PWD";origScript="${BASH_SOURCE[0]}";[[ $origScript != */* ]]&&origScript="./$origScript"
realScript="$origScript";scriptName="${origScript##*/}";scriptDir="${origScript%/*}"
while [[ -L $realScript ]];do cd "${realScript%/*}";realScript="$(readlink "${realScript##*/}")";[[ $realScript != */* ]]&&realScript="./$realScript";done
cd -P "${realScript%/*}";realScript="$PWD/${realScript##*/}";realDir="$PWD";realName="${realScript##*/}";cd "$origDir"
#
# Expanding help...
#
display_help() {
cat << :END
TODO: Rename this template script.
Usage: $scriptName [options] ...
:END
if [[ ${1:-} = "--help" ]]; then
cat << :END
TODO: Replace this text with a short description of the scripts purpose.
This $scriptName script can be used as a basis for command-line scripts.
Options:
--todo Add options with parameter description here.
-h Display usage.
--help Display this help.
--verbose Display verbose messages.
--debug Display debug (very verbose) messages.
--trace Display execution trace.
:END
else
echo "Try --help for more."
fi
}
#
# Verbose Echo...
#
exec 3>/dev/null
v_echo() {
local rc=$?
if [ $rc -gt 0 ]; then
echo "$* (RC $rc)" >&3
else
echo "$*" >&3
fi
}
v_var() {
echo "$1=\"${!1}\"" >&3
}
#
# Exit script due to error...
#
die() {
local rc=$?
if [ $rc -gt 0 ]; then
echo "$scriptName: ${*:-Died} (RC $rc)" >&2
else
echo "$scriptName: ${*:-Died}" >&2
fi
exit 1
}
#
# Parse arguments...
#
if [[ $# -eq 0 || $1 = "-h" || $1 = "--help" ]]; then
# Check for help option...
display_help "$@"
exit 0
fi
args=( "$@" )
for (( arg=0; arg < ${#args[@]}; arg++ )); do
opt="${args[arg]:-}"
param="${args[arg + 1]:-}"
case "$opt" in
# TODO: Add script-specific options here...
--todo) todo="$param" # Set the TODO option.
arg=$(( $arg + 1 ));; # Skip the parameter for the next arg.
-v|--verbose) exec 3>&2;; # Redirect verbose output to stderr.
-x|--debug) set -o verbose;; # Display each line of script.
-xx|--trace) set -o verbose # and...
set -o xtrace;; # Trace evaluated statements.
--) arg=$(( $arg + 1 )) # No more options (point at next argument).
break;;
-*) die "Invalid option $opt specified. Try --help.";;
*) break;; # No more options.
esac
done
shift $arg
unset args arg opt param
#
# Initialise variable defaults and display variables...
#
v_echo "Orig Script: $origScript"
v_echo "Script Name: $scriptName"
v_echo "Script Dir: $scriptDir"
v_echo "---"
v_echo "Real Script: $realScript"
v_echo "Real Name: $realName"
v_echo "Real Dir: $realDir"
v_echo "---"
v_echo "Orig Dir: $origDir"
v_echo "Curr Dir: $(pwd)"
v_echo "---"
v_echo "TODO: ${todo:=default}"
v_var todo
# Do some work...
rc=0
# Exit with the return code...
exit $rc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment