Skip to content

Instantly share code, notes, and snippets.

@colorwebdesigner
Last active July 26, 2019 03:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colorwebdesigner/b0953c3346f4e944a67148f83415ddda to your computer and use it in GitHub Desktop.
Save colorwebdesigner/b0953c3346f4e944a67148f83415ddda to your computer and use it in GitHub Desktop.
Catch and processing script arguments
# 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