cat, pipes, and filters

Chris Torek torek at elf.ee.lbl.gov
Sat Jun 1 23:40:15 AEST 1991


>In article <1991May31.165446.1530 at progress.com> root at progress.COM
>(Root of all Evil) writes:
>>    cat $FILE | sed s/"$ENTRY"/"$NEWENTRY"/ | tee $FILE 1>/dev/null

In article <16438 at darkstar.ucsc.edu> jik at cats.ucsc.edu [ucsc?!]
(Jonathan I Kamens) writes:
>In this case, cat gets started and opens the file in order to read it, and
>*then* tee gets started, sees that it's supposed to send output to $FILE, and
>opens the file and truncates it in preparation for sending output to it.  Once
>cat has opened the file for read, it gets to read it, because the file that
>tee is sending output to is (in effect) a "different file" from the kernel's
>point of view.

Although the two processes hold different file table entries, they use
the same vnode and hence this is not a reliable way of doing things.
Depending on O/S vagaries, cat might read some, all, or none of $FILE
before tee truncates it; when tee truncates $FILE, anything cat has not
yet read will vanish forever.

As it happens, on most Unix boxes cat will run long enough to get the
first `block' (however big a block is, typically 8K under 4BSD-
derivative file systems) before sed will run, and sed will run long
enough to wait for cat before tee will run.  It is unwise to depend
on this.
-- 
In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 415 486 5427)
Berkeley, CA		Domain:	torek at ee.lbl.gov



More information about the Comp.unix.shell mailing list