LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   csh script help (https://www.linuxquestions.org/questions/linux-newbie-8/csh-script-help-489168/)

vijraj 10-03-2006 06:52 PM

csh script help
 
Hi everyone!
I am writing a new shell mf.s which acts as the commandline parser
which does the following.
ex commandline: mf.s -c *.c -f right tempfile -r 2 3 4
it will select the option given at the command line and do the appropriate job.
there r three options to select from
-f -r and -c and any other option given at command line is illegal option.
So if the command line has -f it will run a script findtext.s(written by me)
and if the option is -r, it will run the script right.s and if the given option is
-c it will run the script count.s.
let me tell you about the option -c.this will rin the script count.s which has the wc command in it.So if we pass a -c *.c option to it ,it should select each file and display the no of words char and lines in it.
My problem is ,when should I stop running the count and handle a different option.
*.c pass a bunch of files as arguments .So I execute a while loop to handle the files.But when will the *.c end to handle the next option.
So,I gave a if statement like this:
if ( $argv[$n] == '-[a-z]' ) break
I want to break out of the while if the next argument is an option and not a .c file .
I want that if statement to handle an option i.e if it is a flag like string and just come out of the loop.is my if statement correct?
will it chech whether an argument is a string which contains an option like
-r or -f or any alphabet after a '-'.

I am also posting the while statement for you to see.
I need a reply urgently.Please reply.Thankyou.
while ( $n <= $#argv )

switch ( $argv[$n] )
case '-f':
@ arg1 = $n + 2
@ n++
./findtext.s $argv[$n] $argv[$arg1]
@ n = $n + 2
breaksw
case '-r':
@ sec = $n + 2
@ trd = $n + 3
@ n++
./right.s $argv[$n] $argv[$sec] $argv[$trd]
@ n = $n + 3
breaksw
case '-c':
@ n++
while ( $n <= $#argv )

if ( $argv[$n] == '-[a-z]' ) break

./count.s $argv[$n]
@ n = $n + 1

end

breaksw
default:
echo Illegal option
@ n++
endsw
end

BrianK 10-03-2006 08:08 PM

first off, I'd use getopt instead of trying to parse your way through the argv's yourself. That will make your life a lot easier. If you do that, then the your options will look something like so:

Code:

set argv = ( `/usr/bin/getopt c:vr: $*` )

set COUNTFILES = ""

foreach i ( $* )
    switch ( $i )
        case "-c"
            shift # this essentially removes '-c' from the arg list
            set COUNTFILES = ($CONUTFILES $1) # $1 is now the optarg for -c.  Doing the assignment this way will create an array of each one of the files to be counted
            shift # essentially removed the -c optarg from the arg list
            breaksw
        case "-r"
            shift
            # do some -r stuff
            shift
            breaksw
        case "-v"
            shift
            # do some -v stuff
            # note there is no need for a second shoft because, in this case, v doesn't take an optarg
            breaksw
    endsw
end

foreach f ($COUNTFILES)
    count.s $f  # $f in this case will go through each one of the things stored in $COUNTFILES
end

Make sense at all? Am I missing your point? To be honest, your question is pretty hard to parse. ;)

vijraj 10-04-2006 07:49 AM

Thanks
 
Thank you,Brian.UR answer gave me a lot of knowledge and solved my problem.I was working on it for 3 long days.
Sorry for posting my question in that way.I got exhausted at the end of the day and I posted it in a hurry.Thanks again.


All times are GMT -5. The time now is 06:16 AM.