-
-
Save viktordw/c9e4adfac8d5dcff6004a724f257bc36 to your computer and use it in GitHub Desktop.
Example bash getopts
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
#!/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