Skip to content

Instantly share code, notes, and snippets.

@grampelberg
Last active July 25, 2022 12:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save grampelberg/5285438 to your computer and use it in GitHub Desktop.
Save grampelberg/5285438 to your computer and use it in GitHub Desktop.
#!/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
#!/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