Some csh how-to, please

David Elliott dce at stan.UUCP
Tue Apr 4 01:38:48 AEST 1989


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.

I agree with you in principle.  It is silly to use cat and a pipe
when simple redirection will work.

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.

Try this script:

	#!/bin/sh

	PLACE=out
	while read line
	do
		PLACE=in
	done < /etc/group

	echo "PLACE is $PLACE"
	exit 0

The output on my system (SunOS 4.0.1) is

	PLACE is out

A better solution is

	exec 3<&0 <file
	while read fie
	do
		something with $fie
	done
	exec 0<&3

This really doesn't take any additional processes, and it leaves the
file descriptor for standard input available in fd 3 in case you need
it (it's bad, though typical, practice to redirect from /dev/tty, which
may not be correct).

-- 
David Elliott		...!pyramid!boulder!stan!dce
"Splish splash, I was rakin' in the cash"  -- Eno



More information about the Comp.unix.questions mailing list