Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!decwrl!shelby!Portia!forel!karish From: karish@forel.stanford.edu (Chuck Karish) Newsgroups: comp.unix.questions Subject: Re: Some csh how-to, please Keywords: csh C-shell shell programming unix read Message-ID: <1175@Portia.Stanford.EDU> Date: 29 Mar 89 03:43:56 GMT References: <2127@pikes.Colorado.EDU> <7467@thorin.cs.unc.edu> Sender: USENET News System Reply-To: karish@forel.stanford.edu (Chuck Karish) Organization: Mindcraft, Inc. Lines: 51 In article <7467@thorin.cs.unc.edu> white@white.cs.unc.edu (Brian T. White) wrote: >In article <2127@pikes.Colorado.EDU>, pklammer@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@forel.stanford.edu