I'm currently working through Kernighan and Pike's excellent book The UNIX Programming Environment. One interesting example they give (Exercise 3-7) is the command
cat x y > y
I tried running this command with the initial contents of the files being x
contains xxx
and y
contains yyy
:
The command does not complete. x
remains unchanged (as you would expect) and y
ends up with an large number of lines of xxx
.
This is how I've rationalised the behaviour:
First thing the
>
redirection operator does is truncatey
ready to receive the redirect data. Hence noyyy
bytes end up iny
.As
y
is empty when the redirect process starts writing data., the output ofcat x y
(initially) is justxxx
.cat
will not stop writing until it reaches EOF ony
. But it never reaches EOF, because as each write operation completes, it pushes EOF further past the current read/write pointers? Socat
keeps appendingy
to itself indefinitely.
If anyone could provide a more articulate (and less handwavy) explanation of this behaviour, or correct me if this is total rubbish, that would be much appreciated.
Also I'm sure at one point I found a set of worked solutions online for the problems in this textbook, but I can't find them now. If anyone could point me to such a thing, that would be great.
Many thanks,
MB