Created
April 2, 2020 07:12
-
-
Save MartenH/0b5482db82a19bc795aa9873e1d7cfcf to your computer and use it in GitHub Desktop.
bash, 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
#!/bin/bash | |
if [[ "$1" = "" ]]; then | |
echo "Usage:" | |
echo " -a foo|bar Select foo or bar" | |
echo " -v verbose (set -x)" | |
echo " --arg1 One arg" | |
echo "" | |
echo "Example: | |
echo " ./arguments.sh -a foo --arg1" | |
exit 0 | |
fi | |
POSITIONAL=() | |
while [[ $# -gt 0 ]] | |
do | |
key="$1" | |
case $key in | |
-a|--alphabet) | |
export selected_a="$2" | |
shift # past argument | |
shift # past value | |
;; | |
--arg1 | |
export hello=y | |
shift # past argument | |
;; | |
-v|--verbose) | |
export verbose=y | |
shift # past argument | |
;; | |
*) # unknown option | |
POSITIONAL+=("$1") # save it in an array for later | |
shift # past argument | |
;; | |
esac | |
done | |
set -- "${POSITIONAL[@]}" # restore positional parameters |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment