Created
December 17, 2010 12:58
-
-
Save miceno/744895 to your computer and use it in GitHub Desktop.
How to combine getopts options with (-) and non-options. After processing all options, variable OPTIND contains the value of the first non-option (commandline argument that doesn't start with (-)). Shift command will clean previous commandline optio
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 | |
while getopts "u:p:" opt; do | |
case $opt in | |
u) | |
echo "-u was triggered, Parameter: $OPTARG" | |
dbuser="$OPTARG" | |
;; | |
p) | |
echo "-p was triggered, Parameter: $OPTARG" | |
dbpass="$OPTARG" | |
;; | |
\?) | |
echo "Invalid option: -$OPTARG" | |
exit 1 | |
;; | |
:) | |
echo "Option -$OPTARG requires an argument." | |
exit 1 | |
;; | |
esac | |
done | |
# Clear all options and reset the command line | |
shift $(( OPTIND -1 )) | |
# First parameter | |
if [ -z "$1" ]; then | |
echo "usage: $0 [-u name] [-p password] file" | |
exit | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment