Skip to content

Instantly share code, notes, and snippets.

@unabridgedxcrpt
Forked from ageekymonk/bash_template.sh
Created August 24, 2018 23:49
Show Gist options
  • Save unabridgedxcrpt/407e108d7acb3ba1373d2077a7e58be6 to your computer and use it in GitHub Desktop.
Save unabridgedxcrpt/407e108d7acb3ba1373d2077a7e58be6 to your computer and use it in GitHub Desktop.
bash template
#!/bin/bash
# ------------------------------------------------------------------
# [Author] Title
# Description
# ------------------------------------------------------------------
set -e
SUBJECT=some-unique-id
VERSION=0.1.0
HIDE_LOG=0
LOG_FILE=
# Log Level
LOG_ERR=1
LOG_WARN=2
LOG_INFO=3
verbosity=$LOG_INFO
# --- Functions ----------------------------------------------------
log() {
if [ ! -z $LOG_FILE ]
then
echo "[$(date +'%Y-%m-%d %H:%M:%S %z')] $2" >> $LOG_FILE
fi
if [ "$verbosity" -ge $1 ]
then
echo "[$(date +'%Y-%m-%d %H:%M:%S %z')] $2" >&2
fi
}
err() {
log $LOG_ERR "ERR: $1"
}
warn() {
log $LOG_WARN "WARN: $1"
}
info() {
log $LOG_INFO "INFO: $1"
}
error_and_exit()
{
err $@
exit 1
}
# --- Option processing --------------------------------------------
if [ $# == 0 ] ; 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=/tmp/${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
# -----------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment