Last active
July 26, 2019 03:10
-
-
Save colorwebdesigner/b0953c3346f4e944a67148f83415ddda to your computer and use it in GitHub Desktop.
Catch and processing script arguments
This file contains hidden or 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
# Function to print help message and exit | |
usage () { | |
[ ! -z "$1" ] && printf "\n \e[31m[ error ]\e[0m $0: %s\n" "$1" >&2 | |
printf "$USAGE" >&2 | |
exit 2 | |
} | |
# Catch arguments | |
for (( i=1; i<=$#; i++ )); do | |
case "${!i}" in | |
-* ) VAL=$((i+1)) | |
[[ -n "${!VAL}" && "${!VAL}" != "-"* ]] && VAL="${!VAL}" || VAL=true | |
case "${!i}" in | |
--arg1|-a1 ) [ "$VAL" != true ] && ARG1="$VAL";; | |
--arg2|-a2 ) [ "$VAL" != true ] && ARG2="$VAL";; | |
--help|-h ) usage;; | |
* ) usage "unknown argument ${!i}";; | |
esac;; | |
* ) continue;; | |
esac | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment