Step 1: I open a terminal and type
mkfifo mypipe
cat < mypipe
Step 2: I open another and type:
for elem in {1..100} ; do echo "$elem" > mypipe ; done
A random ammount of numbers get printed, the first script exits and the second hangs.
Please share the wizdom. Ubuntu bug? If more info is needed please ask, i don’t imagine anything relevant atm.
Thx,
you guys are awesome.
The way named pipes work is that when the writing process closes the pipe, the reading process receives an EOF. When
catsees the file has ended it stops reading and exits. Here bash seems to reuse the open file most of the time in the loop, rather than closing and reopening it in each run.To ensure that the file is opened and closed exactly once you could write this:
To get
catoutput exactly one number in each run you have to use the program/bin/echorather than the built-inecho. This forces Bash to open and close the file in each run of the loop: