LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-03-2006, 07:52 PM   #1
vijraj
LQ Newbie
 
Registered: Oct 2006
Posts: 14

Rep: Reputation: 0
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
 
Old 10-03-2006, 09:08 PM   #2
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
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.
 
Old 10-04-2006, 08:49 AM   #3
vijraj
LQ Newbie
 
Registered: Oct 2006
Posts: 14

Original Poster
Rep: Reputation: 0
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
csh script stellafrank Linux - Newbie 3 11-15-2006 07:11 PM
converting bash script to csh DJOtaku Programming 8 02-13-2006 06:35 AM
Script for csh or bash unihiekka Programming 5 10-15-2005 10:51 AM
csh script variables setting zz_s Programming 2 12-04-2004 07:55 AM
how do you set an env variable from within a csh script? BrianK Linux - General 3 05-26-2004 03:16 PM


All times are GMT -5. The time now is 08:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration