Skip to content

Instantly share code, notes, and snippets.

@Great-Antique
Last active March 26, 2020 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Great-Antique/d8560b0123c38a033d2a2511a8c84bd6 to your computer and use it in GitHub Desktop.
Save Great-Antique/d8560b0123c38a033d2a2511a8c84bd6 to your computer and use it in GitHub Desktop.
Bash script template
#!/usr/bin/env bash
# https://kvz.io/bash-best-practices.html
set -o errexit
set -o pipefail
set -o nounset
# DEBUG
#set -o xtrace
__file="${BASH_SOURCE[0]}"
__real_file="$(readlink -f "${BASH_SOURCE[0]}")"
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
__real_dir="$(cd "$(dirname "${__real_file}")" && pwd)"
__caller_dir="$(pwd)"
# https://github.com/matejak/argbash
# https://argbash.io/
die()
{
local _ret=${2:-1}
echo "$1" >&2
exit ${_ret}
}
print_help()
{
printf 'Bash script "Test".\n'
printf '\n'
printf 'Usage: %s REQUIRED_PARAMETER_1 REQUIRED_PARAMETER_2 [OPTIONAL_PARAMETER_1] [OPTIONAL_PARAMETER_2] [REST]...\n' "$0"
printf '\n'
printf 'Additional message.'
}
if [ -z ${1:-} ]; then
print_help
exit 0
fi
first_argument=${1}
# https://medium.com/@Drew_Stokes/bash-argument-parsing-54f3b81a6a8f
eval set -- "${@:2}"
# https://ryanstutorials.net/bash-scripting-tutorial/bash-if-statements.php#case
case ${first_argument} in
"first_case" | 1.2) # string OR int
;;
*)
die "Invalid first argument." 3 # die "Message" (int)exit_code
esac
# YOUR CODE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment