Need help with getops
Greetings,
I need some help with using getops in the bash shell. I want to be able to use multiple options at the same time and some of those options will require an argument and some will not.
Here is my bash script:
#!/bin/bash
while getopts ":a:bc:de:fg:" opt ; do
case $opt in
a)
echo "-a was triggered, Argument: $OPTARG" >&2
;;
b)
echo "-b was triggered" >&2
;;
c)
echo "-c was triggered, Parameter: $OPTARG" >&2
;;
d)
echo "-d was triggered" >&2
;;
e)
echo "-e was triggered, Argument: $OPTARG" >&2
;;
f)
echo "-w was triggered" >&2
;;
g)
echo "-g was triggered, Argument: $OPTARG" >&2
;;
\?)
echo "-$OPTARG is not a valid option" >&2
exit 1
;;
echo "-$OPTARG must have an argument." >&2
exit 1
;;
esac
done
This is my problem:
Fatking@home~>./a.bash -a -b
-a was triggered, Argument: -b
I ran my script with two options. The first option -a, requires an argument. However, instead of giving me an error reporting that option -a requires an argument it takes option -b as the argument though that is an option....
FatKing@home~> ./a.bash -z -b
-z is not a valid option
Fatking@home~> ./a.bash -a -z
-a was triggered, Argument: -z
FatKing@home~> ./a.bash -a
-a must have an argument.
If I only use option -a with no arguments, the script works...
If someone can help me fix this problem that would be much appreciated.
|