Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 10-03-2006, 06:52 PM   #1
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 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
case '-r':
@ sec = $n + 2
@ trd = $n + 3
@ n++
./right.s $argv[$n] $argv[$sec] $argv[$trd]
@ n = $n + 3
case '-c':
@ n++
while ( $n <= $#argv )

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

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


echo Illegal option
@ n++
Old 10-03-2006, 08:08 PM   #2
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:

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


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
	case "-r"
	    # do some -r stuff
	case "-v"
	    # do some -v stuff
	    # note there is no need for a second shoft because, in this case, v doesn't take an optarg

foreach f ($COUNTFILES)
    count.s $f  # $f in this case will go through each one of the things stored in $COUNTFILES
Make sense at all? Am I missing your point? To be honest, your question is pretty hard to parse.
Old 10-04-2006, 07:49 AM   #3
LQ Newbie
Registered: Oct 2006
Posts: 14

Original Poster
Rep: Reputation: 0

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.


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 06:11 PM
converting bash script to csh DJOtaku Programming 8 02-13-2006 05:35 AM
Script for csh or bash unihiekka Programming 5 10-15-2005 09:51 AM
csh script variables setting zz_s Programming 2 12-04-2004 06:55 AM
how do you set an env variable from within a csh script? BrianK Linux - General 3 05-26-2004 02:16 PM > Forums > Linux Forums > Linux - Newbie

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

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