Last active
July 25, 2022 12:22
-
-
Save grampelberg/5285438 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env bash | |
# Take all the positional parameters ($*) and normalize them | |
# -ab1 becomes -a -b 1 or -b1 -a becomes -b 1 -a | |
# Note that the : after b means that it should have a value instead of | |
# just being the boolean flag that a is. | |
args=`getopt ab: $*` | |
# Rewrite ARGV with the output of getopt | |
set -- $args | |
# i refers to each positional parameter | |
for i do | |
case "$i" in | |
-a) | |
a_defined=0; | |
# Remove the current option from ARGV so that the next one is | |
# available | |
shift;; | |
-b) | |
# Since this has an actual value, that needs to be shifted | |
# off ARGV along with -b | |
b_value=$2; shift; | |
shift;; | |
--) | |
shift; break;; | |
esac | |
done |
This file contains 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
#!/usr/bin/env bash | |
while getopts "ab:" opt; do | |
declare "opt_$opt=${OPTARG:-0}" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment