Created
May 21, 2019 14:49
-
-
Save MickaelBaye/68c1647ac450bc17cf5e9ccc1b62cb97 to your computer and use it in GitHub Desktop.
Template for Bash scripts
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
#!/bin/bash | |
## DESCRIPTION: | |
## AUTHOR: $USER_FULLNAME | |
declare -r SCRIPT_NAME=$(basename "$BASH_SOURCE" .sh) | |
## exit the shell(default status code: 1) after printing the message to stderr | |
bail() { | |
echo -ne "$1" >&2 | |
exit ${2-1} | |
} | |
## help message | |
declare -r HELP_MSG="Usage: $SCRIPT_NAME [OPTION]... [ARG]... | |
-h display this help and exit | |
" | |
## print the usage and exit the shell(default status code: 2) | |
usage() { | |
declare status=2 | |
if [[ "$1" =~ ^[0-9]+$ ]]; then | |
status=$1 | |
shift | |
fi | |
bail "${1}$HELP_MSG" $status | |
} | |
while getopts ":h" opt; do | |
case $opt in | |
h) | |
usage 0 | |
;; | |
\?) | |
usage "Invalid option: -$OPTARG \n" | |
;; | |
esac | |
done | |
shift $(($OPTIND - 1)) | |
[[ "$#" -lt 1 ]] && usage "Too few arguments\n" | |
#==========MAIN CODE BELOW========== |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment