Skip to content

Instantly share code, notes, and snippets.

@peterj
Last active June 25, 2018 16:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterj/e29d42403f31b2f49b3c6e7e76bba1e5 to your computer and use it in GitHub Desktop.
Save peterj/e29d42403f31b2f49b3c6e7e76bba1e5 to your computer and use it in GitHub Desktop.
Bash script template
#!/bin/bash
set -eo pipefail
IFS=$'\n\t'
readonly LOG_FILE="/tmp/$(basename "$0").log"
info() { echo "[INFO] $*" | tee -a "$LOG_FILE" >&2 ; }
warning() { echo "[WARNING] $*" | tee -a "$LOG_FILE" >&2 ; }
error() { echo "[ERROR] $*" | tee -a "$LOG_FILE" >&2 ; }
fatal() { echo "[FATAL] $*" | tee -a "$LOG_FILE" >&2 ; exit 1 ; }
printUsage() {
cat << EOF
Usage:
${0##*/}
This is a script!
EOF
}
cleanup() {
# Add any script cleanup code here.
info "Done."
}
indent() {
sed 's/^/ /'
}
OPTIND=1
while getopts ":a:b:" opt; do
case $opt in
a|--along) A=$OPTARG
;;
b) B=$OPTARG
;;
\?)
error "Invalid option: -$OPTARG"
exit 1
;;
esac
done
shift "$((OPTIND-1))"
if [[ "${BASH_SOURCE[0]}" = "$0" ]]; then
trap cleanup EXIT
# Put the script here
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment