Actually, how about detecting this and diagnosing
the problem rather than falling into the infloop?
This currently evokes an infloop for me:
$ seq 1000000> k
$ grep 111 k>> k
However, simply detecting when the output FD is also an input FD is
not enough, since this variant does not get stuck:
$ seq 1000> k
$ grep 111 k>> k
Whether it gets stuck is mainly a function of the buffer size for the
file. Certainly whatever is added at the end of "k" matches, so it will
be added over and over.
This is a problem for every Unix utility that reads files sequentially
(e.g. sed or, from coreutils, cut, cat, fmt, tail, comm).
Paolo