Last active
June 25, 2018 16:42
-
-
Save peterj/e29d42403f31b2f49b3c6e7e76bba1e5 to your computer and use it in GitHub Desktop.
Bash script template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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