Last active
December 15, 2020 14:36
-
-
Save jkereako/fce5018cd4feb23a8d0d87fddc20d4e7 to your computer and use it in GitHub Desktop.
Shell script skeleton
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 | |
# | |
# Name of the Script | |
# Your Name | |
# | |
# Description of the script. | |
# | |
# Usage: | |
# | |
# $> yourscript -a[f1|f2] -b | |
# | |
set -Eeuo pipefail | |
readonly SCRIPT_NAME=${0##*/} | |
# Print usage | |
function help() { | |
cat <<END; | |
USAGE: | |
$SCRIPT_NAME -a [f1|f2] | |
END | |
exit | |
} | |
# Prints messages to STDERR and exits with a non-zero status code | |
function err() { | |
echo -e "\033[0;31mERROR:\033[0m ${1}" >&2; | |
exit 1 | |
} | |
function func_1() { | |
echo "Hello from func_1!" | |
} | |
function func_2() { | |
echo "Hello from func_2!" | |
} | |
function main() { | |
# Reset in case `getopts` was recently used in the shell. | |
OPTIND=1 | |
while getopts ":a:" opt; do | |
case $opt in | |
a) | |
case $OPTARG in | |
f1) | |
func_1 | |
exit 0 | |
;; | |
f2) | |
func_2 | |
exit 0 | |
;; | |
*) | |
err "Invalid argument: -$OPTARG"; | |
;; | |
esac | |
;; | |
\?) | |
err "Invalid option"; | |
;; | |
:) | |
err "Option -$OPTARG requires an argument."; | |
;; | |
esac | |
done | |
shift $((OPTIND-1)) | |
# No more arguments. Invoke the help command | |
[[ -z "$1" ]] && help | |
} | |
# Run | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
3S (Shell Script Skeleton)
This is my adapted version of z017's Shell Script Skeleton with help from this tutorial and this StackOverflow answer. It does most of what z017's script does, but the main differentiator is this is fewer lines of code and easier to understand.