Some csh how-to, please

Chuck Karish karish at forel.stanford.edu
Wed Mar 29 13:43:56 AEST 1989


In article <7467 at thorin.cs.unc.edu> white at white.cs.unc.edu
(Brian T. White) wrote:
>In article <2127 at pikes.Colorado.EDU>, pklammer at pikes.Colorado.EDU
(Peter Klammer) writes:
>> Could someone please tell me how to read a file line at a time
>> into the C shell?

>To read a file one line at a time, try
>
>foreach p ( "`cat file`" )
>	set line=`echo $p`
>	(commands acting on $line)
>end

This fails if 'file' is longer than the allowable length for an
input line.

The following script fragment reads standard input up to a magic cookie
(I don't know how to detect EOF in csh):

#! /bin/csh

set fie=1
while ( "$fie" != "@" )
	set fie=$<
	echo A $fie
end

If this is in an executable file called 'foo', the command would look like

	(cat file; echo "@") | foo

Note that, in csh, it's not possible to pipe to a loop inside the
script, and the script doesn't get a return value from the 'set' command.
An sh script to do this job would look like

cat file |
while read fie
do
	echo A $fie
done

No magic cookies needed; 'read fie' fails at EOF.
A pipe or redirect after 'done' receives the entire output of the loop.

csh is OK as an interactive command line interpreter.  As a programming
language, it's not so hot.   My advice would be to get a copy of
Kernighan and Pike, and use sh instead.

	Chuck Karish	hplabs!hpda!mindcrf!karish	(415) 493-7277
			karish at forel.stanford.edu



More information about the Comp.unix.questions mailing list