Skip to content

Instantly share code, notes, and snippets.

@brennanfee
Last active July 3, 2023 03:03
Show Gist options
  • Save brennanfee/2404746698aed56750f7e45d95aa6d3f to your computer and use it in GitHub Desktop.
Save brennanfee/2404746698aed56750f7e45d95aa6d3f to your computer and use it in GitHub Desktop.
Bash Script Template
#!/usr/bin/env bash
# Author: Brennan Fee
# License: MIT License
# Version: 0.1
# Date: 2023-06-23
# Bash strict mode
([[ -n ${ZSH_EVAL_CONTEXT:-} && ${ZSH_EVAL_CONTEXT:-} =~ :file$ ]] \
|| [[ -n ${BASH_VERSION:-} ]] && (return 0 2> /dev/null)) && SOURCED=true || SOURCED=false
if ! ${SOURCED}; then
set -o errexit # same as set -e
set -o nounset # same as set -u
set -o errtrace # same as set -E
set -o pipefail
set -o posix
#set -o xtrace # same as set -x, turn on for debugging
shopt -s inherit_errexit
shopt -s extdebug
IFS=$(printf '\n\t')
fi
# END Bash scrict mode
SCRIPT_APP=$(basename "$0")
SCRIPT_AUTHOR="Brennan Fee"
SCRIPT_LICENSE="MIT License"
SCRIPT_VERSION="0.2"
SCRIPT_DATE="2023-06-23"
SCRIPT_DIR="$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$(xdg-user-dir DOTFILES)/bash/script-tools.bash"
EXIT_CODE="0"
## Defaults
# TODO: replace with default paramters
HELP="false"
#### START: Process arguments
# TODO: Added input options
ARGS=$(getopt --name "${SCRIPT_APP}" --options hv --longoptions "help,version" -- "$@")
# TODO: Remove this if passing in no arguments or flags is an error condition and should not show help
# shellcheck disable=SC2181
if [[ $? -ne 0 ]]; then
show_help
fi
eval set -- "${ARGS}"
unset ARGS
## Process "options" and "flags"
while true; do
case "$1" in
'-h' | '--help')
HELP="true"
show_help
;;
'-v' | '--version')
show_version
;;
# Reading a "flag" example
#'-f' | '--flag')
# FLAG="true"
# shift
# continue
# ;;
# Reading an "option" example, "--option value", $2 contains the passed in value
#'-o' | '--option')
# OPTION=$2
# shift 2
# continue
# ;;
'--')
shift
break
;;
*)
error_msg "Unknown option: $1"
;;
esac
done
## Now process positional arguments
ARG_COUNT=1
for arg; do
case "${ARG_COUNT}" in
1)
# Example, normalize to lower case
ARG_1=$(echo "${arg}" | tr "[:upper:]" "[:lower:]")
;;
2)
break
;;
*)
error_msg "Internal Argument Error"
;;
esac
ARG_COUNT=$((ARG_COUNT + 1))
done
unset ARG_COUNT
#### END: Process arguments
#### START: Help
function show_version() {
print_status "${SCRIPT_APP}"
print_status "Author: ${SCRIPT_AUTHOR} License: ${SCRIPT_LICENSE}"
print_stauts "Version: ${SCRIPT_VERSION} Date: ${SCRIPT_DATE}"
print_blank_line
exit 0
}
function show_help() {
if [[ "${HELP}" == "false" ]]; then
print_warning "Incorrect parameters or options provided."
print_blank_line
fi
print_status "${SCRIPT_APP}"
print_status "Author: ${SCRIPT_AUTHOR} License: ${SCRIPT_LICENSE}"
print_stauts "Version: ${SCRIPT_VERSION} Date: ${SCRIPT_DATE}"
print_blank_line
# TODO: Write help
if [[ "${HELP}" == "false" ]]; then
exit 1
else
exit 0
fi
}
#### END: Help
function main() {
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment