using getopt with a switch statement

hi all,

i have been trying to get a script working that can take in more than one option using getopt. But for some reason, even when i type in a covered option, it skips directly to my error message of "no match." Any ideas of what might be wrong?

set - 'getopt frd*: $*'

for i in $*
do
case $i in
-f) rm -f;;
-r) rm -r;;
-d) rm -d;;
"") rm;;
*) echo "no match" ;
exit;;
esac
done

try this:

set - 'getopt "frd*:" "$*"'

also change it in

for i in "$*"
do
case "$i" in

set - 'getopt frd*: $'
is wrong. You need a double hyphen:
set -- 'getopt frd
: $*'

Exactly, change it to double hyphen.

hi,

thanks for your replies, but it still seems to be straight to my error message. Here's the modified script. Is the for loop in its correct format? :confused:

set -- 'getopt "frd*:" "$*"'

for i in "$"
do
case "$i" in
-f) rm -f;;
-r) rm -r;;
-d) rm -d;;
"
") rm*;;
*) echo "no match" ; exit;;
esac
done

It's
set - `getopt frd*: $*`

There are two quotes here and they are both backquotes, not single quotes. You might need to put frd* in double quotes if you have a screwy file that matches that template. Then you need:

for i in $*

No quotes here!

Change
-f) rm -f
to
-f) echo rm -f
until you get this working. Same for the other rm's. rm is dangerous.

After the "do", put "echo i = $i" so you can see what is happening.

it works! thanks. it seem that in the loop's last iteration, i = -- and so the error message prints every time because I did not have a case for that. After adding the case the problem went away. :smiley: