diff --git a/scripts/bash-template.sh b/scripts/bash-template.sh new file mode 100644 index 0000000..9ac15c7 --- /dev/null +++ b/scripts/bash-template.sh @@ -0,0 +1,111 @@ +#!/usr/bin/env bash + +# https://betterdev.blog/minimal-safe-bash-script-template/ +# https://medium.com/better-programming/my-minimal-safe-bash-script-template-300759114040 + +set -E -o errexit -o nounset -o pipefail +trap cleanup SIGINT SIGTERM ERR EXIT + +readonly SCRIPT_NAME=$(basename "${BASH_SOURCE[0]}") +readonly SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P) + +function usage() { + cat <] [--abc ] [-f | --flag] +DESCRIPTION + Your description here. +OPTIONS: +-h, --help + Print this help and exit. +-f, --flag + Description for flag option. +-a + Description for the -a option. +--abc + Description for the --abc option. +EOT +} + +function cleanup() { + trap - SIGINT SIGTERM ERR EXIT + # script cleanup here +} + +function msg() { + echo >&2 -e "${1-}" +} + +function die() { + local -r msg="$1" + local -r code="${2:-90}" + echo "${msg}" >&2 + exit "${code}" +} + +function parse_params() { + local -r args=("${@}") + + while true; do + + case "$1" in + --abc) + abc_option_flag=1 + shift + readonly abc_arg="$1" + shift + ;; + + -a) + a_option_flag=1 + shift + readonly a_arg="$1" + shift + ;; + + --help|-h) + usage + exit 0 + shift + ;; + + --flag|-f) + flag_option_flag=1 + shift + ;; + + --) + shift + break + ;; + + *) + break + ;; + esac + done +} + +# MAIN + +a_option_flag=0 +abc_option_flag=0 +flag_option_flag=0 + + +parse_params "$@" + +if ((flag_option_flag)); then + echo "flag option set" +fi + +if ((abc_option_flag)); then # Check if the flag options are set or ON: + # Logic for when --abc is set. + # "${abc_arg}" should also be set. + echo "Using --abc option -> arg: [${abc_arg}]" +fi + +if ((a_option_flag)); then + # Logic for when -a is set. + # "${a_arg}" should also be set. + echo "Using -a option -> arg: [${a_arg}]" +fi