Skip to content

Instantly share code, notes, and snippets.

@endorama
Last active September 19, 2022 21:47
Show Gist options
  • Save endorama/6595286 to your computer and use it in GitHub Desktop.
Save endorama/6595286 to your computer and use it in GitHub Desktop.
Bash template file
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
SCRIPT="$(readlink --canonicalize-existing "$0")"
SCRIPTPATH="$(dirname "$SCRIPT")"
SCRIPTNAME="$(basename "$SCRIPT")"
# Thanks https://dev.to/thiht/shell-scripts-matter :)
# set -x if DEBUG env var is set - any value suffice
[ -z "${DEBUG:-}" && set -x
#/ Usage:
#/ Description:
#/ Examples:
#/ Options:
#/ --help: Display this help message
#/ --version: Display programm version
#/ Version: 0.1.0
usage() { grep '^#/' "$0" | cut -c4- ; exit 0 ; }
version() { grep '^#/ Version:' "$0" | cut -c13- ; exit 0 ; }
expr "$*" : ".*--help" > /dev/null && usage
expr "$*" : ".*--version" > /dev/null && version
readonly LOG_FILE="/tmp/$SCRIPTNAME.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 ; }
# cleanup() {}
# Parse command line options.
# while getopts ab: OPT; do
# case "$OPT" in
# a)
# echo "set a"
# b)
# echo $OPTARG
# ;;
# \?)
# # getopts issues an error message
# usage
# exit 1
# ;;
# esac
# done
# # Remove the switches we parsed above.
# shift `expr $OPTIND - 1`
if [[ "${BASH_SOURCE[0]}" = "$0" ]]; then
# trap cleanup EXIT
# Script goes here
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment