1

Prerequisites:

$ mkfifo pipe1

If several lines are put into pipe1

$ (echo line 1; echo line 2) > pipe1

all the lines can be read in another terminal by

$ cat pipe1
line 1
line 2

So far all right.

But

if instead of the latter cat several partial reads are executed

$ head -n 1 pipe1; head -n 1 pipe1
line 1

then only the first line of the data piped into pipe1 is returned (by the first head -n 1); the rest of the data seems to get lost, the second read access is hanging hence waiting for (new) data available in pipe1.

How

to set up a named pipe and pipe contents to it in such a way that several consecutive partial reads are possible?

In other words, how to get this:

$ head -n 1 pipe1; head -n 1 pipe1
line 1
line 2
  • IIRC, but I may be wrong here, you can't close the pipe - this will flush the data. But head closes it. So you'll need a custom version of head that doesn't close it. Not very elegant. What are you trying to achieve with this? It sounds like an XY-Problem. – dirkt Jul 20 '18 at 13:11
  • @dirk1 In the end I want to have a persistent FIFO queue: write arbitrary times to a file, read arbitrary times from the same file, data that is read disappears from the queue. Named pipes seemed to me like a solution for that. But as pointed out in the question, it does not work like expected by me. – Min-Soo Pipefeet Jul 20 '18 at 13:18
  • Pipes won't work for this. See e.g. here for alternatives. – dirkt Jul 20 '18 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.