diff --git a/bash/argument_parsing.bash b/bash/argument_parsing.bash new file mode 100644 index 0000000..74767f9 --- /dev/null +++ b/bash/argument_parsing.bash @@ -0,0 +1,30 @@ +#!/bin/bash + +while [[ $# -gt 0 ]]; do + case "$1" in + -a|--all) + # set option a/all + shift # and get the next argument + ;; + -h|--help) + # display help + exit # because we want to stop + ;; + -z|--zooper) + zooper=$2 # grab the NEXT argument after the -z|--zooper + shift # throw away the -z|--zooper + shift # and throw away its value that we've also used + ;; + -*|--*) + echo "We've found an unknown option: $1" + # show help if you like + shift + break # stop parsing but allow script to continue, if that's useful + ;; + *) + echo "I don't know how we got here, it shouldn't happen. Should have handled the args or hit the unknowns option just above." + exit + ;; + esac +done +