Created
October 22, 2014 06:53
-
-
Save qinshulei/ccfc702d25368a87c479 to your computer and use it in GitHub Desktop.
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/sh | |
# A POSIX variable | |
OPTIND=1 # Reset in case getopts has been used previously in the shell. | |
# Initialize our own variables: | |
output_file="" | |
verbose=0 | |
while getopts "h?vf:" opt; do | |
case "$opt" in | |
h|\?) | |
show_help | |
exit 0 | |
;; | |
v) verbose=1 | |
;; | |
f) output_file=$OPTARG | |
;; | |
esac | |
done | |
shift $((OPTIND-1)) | |
[ "$1" = "--" ] && shift | |
echo "verbose=$verbose, output_file='$output_file', Leftovers: $@" | |
# End of file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment