DEAR PEOPLE FROM THE FUTURE: Here's what we've figured out so far...

Welcome! This is a Q&A website for computer programmers and users alike, focused on helping fellow programmers and users. Read more

What are you stuck on? Ask a question and hopefully somebody will be able to help you out!
+3 votes

I have created a named pipe

mkfifo ~/mypipe

and I write to it

echo "hello world" > ~/mypipe

and read from it

$ cat ~/mypipe
hello world

I have to retype cat ~/mypipe every time that I want to read it because it quits immediately. How do I keep reading from the pipe (and print to stdout) without manually running cat every time?

by

1 Answer

+2 votes
 
Best answer

More than one option:

  • with a loop:

    while true ; do cat ~/mypipe ; done

  • with socat:

    socat /home/wonk/mypipe /dev/tty

  • open the pipe for reading and writing with <>. The reader will get EOF only when the last writer to the pipe has closed. But since you're opening in both reading and writing, there will always be an open writer and it will never close:

    cat <> ~/mypipe

  • a variant of the above is to open a given file descriptor for writing to the pipe:

    // open for reading
    cat ~/mypipe
    // redirect file descriptor to pipe
    exec 3> mypipe
    // write to file descriptor
    echo "hello world" >&3
    // close file descriptor, will send EOF
    exec 3>&-

ago by
selected ago by
Contributions licensed under CC0
...