Some csh how-to, please

David Korn[drew] dgk at ulysses.homer.nj.att.com
Wed Apr 5 12:03:27 AEST 1989


In article <718 at salgado.stan.UUCP>, dce at stan.UUCP (David Elliott) writes:
> In article <4258 at omepd.UUCP> merlyn at intelob.intel.com (Randal L. Schwartz @ Stonehenge) writes:
> >Why do people use 'cat' at every opportunity?  Maybe that's why
> >they sell so many multiprocessor machines? :-) :-)
> 
> >Simpler:
> >
> >	while read fie
> >	do
> >		something with $fie
> >	done <file
> >
> >no additional processes.
> 
> 
> In practice, you are unfortunately wrong.  The best that redirection
> gives you is one less exec.  Current "standard" versions of sh (in
> other words, most sh's other than ksh) fork when redirection is done
> on a loop.
> 

The big problem with
	cat file | while read ...

is that it runs about 1/10th the speed of the redirection loop.
The reason is that the shell (sh and/or ksh) have to read one
character at a time when reading from a pipe.  The reason for
reading one character at a time is to make scripts like
	cat file | ( read line;cat)
behead only a single line.

Unfortunately there is not way to seek back on a pipe.  Nor is
there an ioctl() to specify line buffering.  Thus, single characters
reads are used.  This is not the case when you specify redirection.

I hope that someone will add a stream discipline that will cause
pipes to use a line buffering protocol.  Until such time, scripts
would do better to use a temporary file rather than piping to
a while read loop.



More information about the Comp.unix.questions mailing list