Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created September 30, 2013 21:52
Show Gist options
  • Save cowboy/6770845 to your computer and use it in GitHub Desktop.
Save cowboy/6770845 to your computer and use it in GitHub Desktop.
getopts wtf
#!/bin/bash
function my_test() {
echo '== my_test =='
local opt k s
while getopts 'ks' opt; do
echo "opt: $opt, OPTARG: $OPTARG"
# [[ "$1" == '-s' ]] && s=1
# [[ "$1" == '-k' ]] && k=1
done
shift $((OPTIND-1))
echo "$@"
}
my_test 1
my_test -s 2
my_test -k 3
my_test -s -k 4
my_test -k -s 5
my_test -g 6
# == my_test ==
# 1
# == my_test ==
# opt: s, OPTARG:
# 2
# == my_test ==
# 3
# == my_test ==
# opt: k, OPTARG:
# 4
# == my_test ==
# 5
# == my_test ==
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment