1830237 Members
13036 Online
109999 Solutions
New Discussion

c shell script

 
windflower_1
Occasional Contributor

c shell script

Hi, everyone,

Please help a beginner with a c shell script... the requirement is:
the script finds a specified string in each line of a file. I know that the following command works:
grep the_string the_file

How can I reach the same result in a c shell?

Thanks
windflowers
5 REPLIES 5
Christian Gebhardt
Honored Contributor

Re: c shell script

Hi
I don't see the problem:

------------------------
#!/bin/csh

grep the_string the_file

-------------------------

Chris
Chuck J
Valued Contributor

Re: c shell script

Don't know about csh, but for sh:

##########################
#!/usr/bin/sh

AFILE='/tmp/yourfile'

grep "hello there" $AFILE
##########################


Chuck J
Tom Jackson
Valued Contributor

Re: c shell script

Hi:

There are several ways to do a pattern search in unix script. You can put any of these commands in a file, then execute the file as a script:

grep pattern file
sed -n /pattern/p file
awk ' /pattern/ { print $0 } ' file

All three of the above commands give the same output, and all three can be put in a file to execute.

Tom
harry d brown jr
Honored Contributor

Re: c shell script

windflower,

Putting the grep statement in a script is a no brainer, but what is probably more important is what you want to do when you "FIND" the string in the_file ???

live free or die
harry
Live Free or Die
Shannon Petry
Honored Contributor

Re: c shell script

First, I'd suggest some heavy reading in some scripting books. There are literally thousands available, some covering shell specifics (I.E. Borne Shell Scripting), and others cover many. (I.E. Basic Unix Scripting).

In both of your questions, you are asked to write a "wrapper" script. This is also very basic scripting.

First, Borne and Korne shell have C like variable setting.
I.E.
I=1
NAME="john"

C-shell uses more basic like language.
I.E.
set I=1
set NAME="john"

In the shell, you need to know what the STDARGS is, and how to use it. Borne/Korn shell use stdargs differently than C-Shell.

You also need to look at the return value from your command to know if the command failed, or your string does not exist.

So the logic you need is to do this. Look for 2 arguments: filename and search_string.

1. Process input and look for entry of both arguments.
2 strings=continue
less than 2 strings=exit with error.
2. test to see if string 1 or string 2 is a filename. Hint: if ( -f "name") ; endif will test to see if name is a file.
3. use argument not a file with grep on the file.
Look at the return code from grep.
If you have matching strings, they will print to stdout unless you specify otherwize. If grep returns a 0, and no lines then grep was fine, but nothing was found. If grep returns anything greater than 0, then grep failed.

While many here including myself could write your assignment for you, it would be much better for you to learn and do it on your own.

All of the information needed for this work can be found in "Unix in a Nutshell", published by O'Reilly and Associates. There are sections for basic Borne/Korn and C-Shell scripting. You may have to read 2-4 pages in the C-Shell section to do this script.


Regards,
Shannon
Microsoft. When do you want a virus today?