Last active
July 8, 2020 07:46
-
-
Save rinormaloku/46b89e10d0f06e74fb6c6460af347a65 to your computer and use it in GitHub Desktop.
named arguments sample for bash
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 | |
#set -e | |
help() { | |
cat <<EOF | |
A minimalistic script to showcase named arguments in bash. | |
The following flags are required. | |
--req-arg If required arg is not specified execution is interrupted. | |
The following flags are optional. | |
--opt-arg Variable defaults to 'default' | |
EOF | |
exit 1 | |
} | |
while [[ $# -gt 0 ]]; do | |
case ${1} in | |
--req-arg) | |
REQUIRED_ARG="$2" | |
shift | |
;; | |
--opt-arg) | |
OPTIONAL_ARG="$2" | |
shift | |
;; | |
*) | |
help | |
;; | |
esac | |
shift | |
done | |
# Exiting if required argument is not defined | |
[ -z "${REQUIRED_ARG}" ] && echo "--req-arg is required, type --help for help" && exit 1; | |
# Defaulting the optional argument | |
[ -z "${OPTIONAL_ARG}" ] && OPTIONAL_ARG=default | |
echo "--req-arg=$REQUIRED_ARG" | |
echo "--opt-arg=$OPTIONAL_ARG" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Applying named arguments
Optional argument defaulting to the default value
Missing Required argument
Help