#!/usr/bin/env bash # !!!!!!!!!!!!!!!!!!!!!!!! # JUST TEMPLATE # !!!!!!!!!!!!!!!!!!!!!!!! # https://betterdev.blog/minimal-safe-bash-script-template/ # https://medium.com/better-programming/my-minimal-safe-bash-script-template-300759114040 # https://github.com/koalaman/shellcheck echo "Just tempalte. Change it to by your requirements" exit 10 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) readonly ERROR_CODE=90 function usage() { cat <<-EOT Usage: ${SCRIPT_NAME} [-h | --help] [-a ] [--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 msg="$1" local code="${2:-$ERROR_CODE}" echo "$msg" >&2 exit "$code" } function parse_params() { local EXTRA_ARGS=() for ARG in "$@"; do case "$ARG" 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 ;; *) EXTRA_ARGS+=("$ARG") ;; esac done # join EXTRA_ARGS a ARGS } # 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