From e37dc437185983dd4b23ab401ec60cc550b4f353 Mon Sep 17 00:00:00 2001 From: daryl Date: Thu, 26 May 2022 16:08:22 +0930 Subject: [PATCH] Add 'bash/argument_parsing.bash' --- bash/argument_parsing.bash | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 bash/argument_parsing.bash 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 +