Skip to content

Instantly share code, notes, and snippets.

@viktordw
Forked from reikind/gist:4072830
Created December 25, 2021 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viktordw/c9e4adfac8d5dcff6004a724f257bc36 to your computer and use it in GitHub Desktop.
Save viktordw/c9e4adfac8d5dcff6004a724f257bc36 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