Created
February 16, 2012 02:35
-
-
Save shelling/1841125 to your computer and use it in GitHub Desktop.
template of getopt in shell
This file contains hidden or 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 | |
| qamode="false" | |
| action=$1 | |
| shift | |
| case "$action" in | |
| start|stop) | |
| ;; | |
| *) | |
| echo "Usage: $0 {start|stop} [options]" | |
| exit 2 | |
| ;; | |
| esac | |
| options=$(getopt -o a:qh -l append,qamode,help -- $*) | |
| if [ $? != 0 ]; then | |
| exit 2; | |
| fi | |
| set -- $options | |
| while true | |
| do | |
| case $1 in | |
| -a|--append) | |
| echo append arg $2 | |
| # $1 is -a | --append now | |
| # $2 is the argument of -a now | |
| shift | |
| ;; | |
| -q|--qamode) | |
| qamode="true" | |
| ;; | |
| -h|--help) | |
| echo "help" | |
| exit 0 | |
| ;; | |
| *) | |
| break; | |
| ;; | |
| esac | |
| shift | |
| done | |
| if [[ "true" == $qamode ]]; then | |
| echo "qa mode was enabled" | |
| else | |
| echo "no qa mode" | |
| fi | |
| echo "done" | |
| case $action in | |
| start) | |
| echo "start process" | |
| ;; | |
| stop) | |
| echo "stop process" | |
| ;; | |
| esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment