Skip to content

Instantly share code, notes, and snippets.

@danielpetisme
Last active October 7, 2017 13:45
Show Gist options
  • Save danielpetisme/9eb1fa948968c20038e6b55604856f60 to your computer and use it in GitHub Desktop.
Save danielpetisme/9eb1fa948968c20038e6b55604856f60 to your computer and use it in GitHub Desktop.
Shell script template
#!/usr/bin/env bash
# Inspired by https://dev.to/thiht/shell-scripts-matter
set -euo pipefail
IFS=$'\n\t'
#/ Usage:
#/ Description:
#/ Examples:
#/ Options:
#/ --help: Display this help message
usage() { grep '^#/' "$0" | cut -c4- ; exit 0 ; }
expr "$*" : ".*--help" > /dev/null && usage
readonly LOG_FILE="/tmp/$(basename "$0").log"
debug() { echo -e "[\033[39mDEBUG\033[0m] $*" | tee -a "$LOG_FILE" >&2 ; }
info() { echo -e "[\033[36mINFO\033[0m] $*" | tee -a "$LOG_FILE" >&2 ; }
warning() { echo -e "[\033[33mWARNING\033[0m] $*" | tee -a "$LOG_FILE" >&2 ; }
error() { echo -e "[\033[31mERROR\033[0m] $*" | tee -a "$LOG_FILE" >&2 ; }
fatal() { echo -e "[\033[41mFATAL\033[0m] $*" | tee -a "$LOG_FILE" >&2 ; exit 1 ; }
cleanup() {
# Remove temporary files
# Restart services
# ...
# The function must return something
: # : is equivalent to 'true'
}
if [[ "${BASH_SOURCE[0]}" = "$0" ]]; then
trap cleanup EXIT
# Script goes here
# ...
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment