Skip to content

Instantly share code, notes, and snippets.

@reikind
Created November 14, 2012 15:39
Show Gist options
  • Save reikind/4072830 to your computer and use it in GitHub Desktop.
Save reikind/4072830 to your computer and use it in GitHub Desktop.
Example bash getopts
#!/bin/bash
usage() {
cat << EOT
usage $0 [-hgw]
$0 -m MESSAGE
OPTIONS
-h show this usage
-m show message "Hello <m option arguments>
-g show message "Hello Git!!"
-w show message "Hello World!!"
EOT
}
main() {
echo "Hello $HELLOTO!!"
}
HELLOTO=`whoami`
while getopts "hgwm:" option; do
case $option in
g)
HELLOTO="Git"
;;
w)
HELLOTO="World"
;;
m)
HELLOTO="$OPTARG"
;;
\?)
echo "wrong option."
usage
exit 1
;;
h)
usage
exit 0
;;
esac
done
shift $(($OPTIND - 1))
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment