Skip to content

Instantly share code, notes, and snippets.

@mmyers1474
Created July 21, 2017 21:18
Show Gist options
  • Save mmyers1474/7535ef90e770f12a7f4f3b8b982e1297 to your computer and use it in GitHub Desktop.
Save mmyers1474/7535ef90e770f12a7f4f3b8b982e1297 to your computer and use it in GitHub Desktop.
#!/bin/bash
#================================================================
#% SYNOPSIS
#+ ${SCRIPT_NAME} [-hv] [-o[file]] args ...
#%
#% DESCRIPTION
#% This is a script template
#% to start any good shell script.
#%
#% OPTIONS
#% -o [file], --output=[file] Set log file (default=/dev/null)
#% use DEFAULT keyword to autoname file
#% The default value is /dev/null.
#% -t, --timelog Add timestamp to log ("+%y/%m/%d@%H:%M:%S")
#% -x, --ignorelock Ignore if lock file exists
#% -h, --help Print this help
#% -v, --version Print script information
#%
#% EXAMPLES
#% ${SCRIPT_NAME} -o DEFAULT arg1 arg2
#%
#================================================================
#- IMPLEMENTATION
#- version ${SCRIPT_NAME} (www.uxora.com) 0.0.4
#- author Michel VONGVILAY
#- copyright Copyright (c) http://www.uxora.com
#- license GNU General Public License
#- script_id 12345
#-
#================================================================
# HISTORY
# 2015/03/01 : mvongvilay : Script creation
# 2015/04/01 : mvongvilay : Add long options and improvements
#
#================================================================
# DEBUG OPTION
# set -n # Uncomment to check your syntax, without execution.
# set -x # Uncomment to debug this shell script
#
#================================================================
# END_OF_HEADER
#================================================================
SUBJECT=$(od -x /dev/urandom | head -1 | awk '{OFS="-"; print $2$3,$4,$5,$6,$7$8$9}')
VERSION=0.1.0
USAGE="Usage: command -hv args"
# Script operational configuration directives.
LOGGING="screenonly" # Values can be 'screenonly', 'fileonly', 'both'
LOGFILE="/var/log/${SCRIPT_NAME}.log"
TRAPERRORS=${TRUE}
MINOPTCOUNT=0
# --- Error Trapping --------------------------------------------
set -e
set -o pipefail # trace ERR through pipes
set -o errtrace # trace ERR through 'time command' and other functions
function trap-errors() {
local lineno="${1}"
local errmsg="${2}"
local errcde="${3:-1}"
if [[ -n "$errmsg" ]] ; then
echo "Error on or near line ${lineno}: ${errmsg}; exiting with status ${code}"
else
echo "Error on or near line ${lineno}; exiting with status ${errcde}"
fi
exit "${errcde}"
}
[[ ${TRAPERRORS} ]] && trap 'trap-errors ${lineno} ${?}' ERR
# --- Logging --------------------------------------------
case "${LOGGING}" in
'screenonly')
echo "All logging going to screen."
;;
'fileonly')
echo "All logging now going to ${LOGFILE}"
exec > "${LOGFILE}"
exec 2>&1
;;
'both')
echo "All logging now going to ${LOGFILE}"
exec > "${LOGFILE}"
exec 2>&1 | tee -a "${LOGFILE}"
;;
esac
# --- Option processing --------------------------------------------
if [ $# -lt "${MINOPTCOUNT}" ] ; then
echo $USAGE
exit 1;
fi
while getopts ":Vh" optname
do
case "$optname" in
"V")
echo "Version $VERSION"
exit 0;
;;
"h")
echo $USAGE
exit 0;
;;
"?")
echo "Unknown option $OPTARG"
exit 0;
;;
":")
echo "No argument value for option $OPTARG"
exit 0;
;;
*)
echo "Unknown error while processing options"
exit 0;
;;
esac
done
shift $(($OPTIND - 1))
param1=$1
param2=$2
# -----------------------------------------------------------------
LOCK_FILE=/var/run/${SUBJECT}.lock
if [[ -f "$LOCK_FILE" ]]; then
echo "Script is already running"
exit
fi
# -----------------------------------------------------------------
trap "rm -f $LOCK_FILE" EXIT
touch $LOCK_FILE
# -----------------------------------------------------------------
# SCRIPT LOGIC GOES HERE
# -----------------------------------------------------------------
echo "Hello World."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment