using getopt with a switch statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using getopt with a switch statement
# 1  
Old 02-08-2005
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
# 2  
Old 02-08-2005
Quote:
Originally Posted by gammarays
hi all,

set - 'getopt frd*: $*'

done
try this:

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

also change it in

for i in "$*"
do
case "$i" in
# 3  
Old 02-08-2005
set - 'getopt frd*: $*'
is wrong. You need a double hyphen:
set -- 'getopt frd*: $*'
# 4  
Old 02-08-2005
Quote:
Originally Posted by Perderabo
set - 'getopt frd*: $*'
is wrong. You need a double hyphen:
set -- 'getopt frd*: $*'
Exactly, change it to double hyphen.
# 5  
Old 02-09-2005
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? Smilie

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
# 6  
Old 02-09-2005
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.
# 7  
Old 02-09-2005
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. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Missing Logic Looping Through Switch Statement

Having trouble with the logic when looping over this switch case again: for (j = 0; data != 0; j++){ switch(data){ case 'c': output = ranit(r_brace_array); break; case 'h': output = ranit(pipe_array); break; ... (6 Replies)
Discussion started by: Azrael
6 Replies

2. Shell Programming and Scripting

Getopt Help

Hi All, An old work friend wrote a script which I've been trying to understand how a section of it currently works and work out how i can add some command line switches which i can use later in the script to append the output depending on the command line arguements. Currently it works by... (1 Reply)
Discussion started by: mutley2202
1 Replies

3. Programming

Passing arguments from command line to switch case statement in C

Hi Am pretty new to C.. Am trying to pass the arguments from command line and use them in switch case statement.. i have tried the following #include <stdlib.h> main(int argc, char* argv) { int num=0; if ( argc == 2 ) num = argv; printf("%d is the num value",num); switch ( num ) ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

4. Shell Programming and Scripting

Something is wrong with this switch( case statement.

I started writing a script to save the files from a camera I got the other day, which mounts in /Volumes , and I got into it and started building this menu. The only problem is that the switch case is coming up as a syntax error at the parenthesis after a case. Here is the code: while : do ... (2 Replies)
Discussion started by: snakemasterAK
2 Replies

5. Shell Programming and Scripting

how to access console of a switch having rj45 on switch side to db 9 female on pc side console cable

hi, how to access console of a switch having rj45 on switch side to db 9 female on pc side console cable which needs to be connected to one console server having rj11 on its side and db 9 female on other end.i.e. on switch side,console cable has rj45 and db 9 pin female connector on other side of... (1 Reply)
Discussion started by: pankajd
1 Replies

6. Shell Programming and Scripting

Tcl switch statement

I am just learning Tcl and there are few things about it that is perplexing me. I have a question about the switch statement. Why are these two switch statements giving me different results? $ cat test_switch.tcl #!/usr/bin/tcl set foo "abc" switch abc a - b {puts "No. 1"} $foo {puts... (2 Replies)
Discussion started by: SFNYC
2 Replies

7. Programming

Comapilation error with Switch statement

Hello , How to resolve below compilation error.activity_type is a member of structure and the output from databse will be stored in structure.Expected output wil l be either D or N or C . sample struct format: struct a{ char acAtivity_type; } code: switch (a->activity_type)... (1 Reply)
Discussion started by: jagan_kalluri
1 Replies

8. Shell Programming and Scripting

Help with getopt

Hi, I want to use the getopt function to parse some arguments for a script. while getopts "i:f:r:" OPTION do case $OPTION in i) iter=$OPTARG;; f) frame=$OPTARG;; r) roi=$OPTARG;; ?) echo Usage: ...... exit 2;; esac done However, I... (5 Replies)
Discussion started by: giorgos193
5 Replies

9. Shell Programming and Scripting

getopt help

I m trying to use getopt This is my script, but it doesn't take argument in variable, Please help. set - - `getopt mscl: $*` if then echo "Exiting...." exit 2 fi for i in $* do case $i in -m) MAIL="$i"; shift;; -s) SCRIPT=$OPTARG; shift;; -c) COB=$OPTARG; shift;;... (2 Replies)
Discussion started by: darshakraut
2 Replies

10. Shell Programming and Scripting

getopt help

scriptname i have made a script to perform so tasks and i managed to complete the tasks for all the options the problem i am facing is that i can run the scripts individually but i would like to make it such that it can accept multiple options and give me the appropriate output e.g.... (1 Reply)
Discussion started by: problems
1 Replies
Login or Register to Ask a Question