Last active
March 26, 2020 07:54
-
-
Save Great-Antique/d8560b0123c38a033d2a2511a8c84bd6 to your computer and use it in GitHub Desktop.
Bash script template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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