csh reading from stdin
Glenn Huxtable
glenn at wacsvax.uwa.oz
Thu Aug 23 20:21:31 AEST 1990
While modifying a csh script (hey, I dont write them, I just fix them) I came
across the following odd behaviour in csh. Can anyone expalin why it does this?
More to the point, is it correct and standard? (I have only tried this on a
SunOS 4.0.3 host).
I wanted to look a the first line of a file (on stdin) and decide which filter
to pass it to, depending on the first few characters. I thought this would be
quite straight forward, use $< to read the first line, decide which filter to
use then echo the line just read and cat the remaining file to the filter.
Something like ...
set line=$<
# decide which filter
(echo $line ; cat) | $FILTER
this resulted in two copies of the first line.
now, the csh manual says that $< can be used to read values from the
keyboard, it doesnt say anything about reading from a file, however you would
expect ...
a simpler test is the following
set first=$<
set second=$<
echo $first
echo $second
cat
running this with stdin taken from the keyboard (ie. no redirect) does what
you would expect, reads two lines, echos them then cats the rest back at you.
running this with input redirected from a file prints the first two lines
of the file, then the entire file INCLUDING the first two line.
Just to make life more interesting, taking input from a pipe does the expected
(ie no duplication of lines). It looks very much like csh is looking to see
if stdin is a file and maintaining duplicate file pointers for itself and
subsequent commands.
As it turns out, this is exactly what I want, since I thought that having read
the first line (in my example above) that I would have to mess around with
the (echo $line; cat) subshell to get the line back intothe subsequent input
stream of the filter. The question is, can I trust this is normal? If it is,
someone should probably be shot!
glenn
ACSnet - glenn at wacsvax.uwa.oz glenn huxtable
Internet - glenn at wacsvax.cs.uwa.oz.au dept of computer science
phone - +61 09 380 2878 university of western australia
fax - +61 09 382 1688
More information about the Comp.unix.questions
mailing list