How to avoid echo closing FIFO named pipes? - Funn

2019-01-16 06:25发布

问题:

I want to output some data to a pipe and have the other process do something to the data line by line. Here is a toy example:

mkfifo pipe
cat pipe&
cat >pipe

Now I can enter whatever I want, and after pressing enter I immediately see the same line. But if substitute second pipe with echo:

mkfifo pipe
cat pipe&
echo "some data" >pipe

The pipe closes after echo and cat pipe& finishes so that I cannot pass any more data through the pipe. Is there a way to avoid closing the pipe and the process that receives the data, so that I can pass many lines of data through the pipe from a bash script and have them processed as they arrive?

回答1:

When a FIFO is opened for reading, it blocks the calling process (normally). When a process opens the FIFO for writing, then the reader is unblocked. When the writer closes the FIFO, the reading process gets EOF (0 bytes to read), and there is nothing further that can be done except close the FIFO and reopen. Thus, you need to use a loop:

mkfifo pipe
(while cat pipe; do : Nothing; done &)
echo "some data" > pipe
echo "more data" > pipe

An alternative is to keep some process with the FIFO open.

mkfifo pipe
sleep 10000 > pipe &
cat pipe &
echo "some data" > pipe
echo "more data" > pipe


回答2:

Put all the statements you want to output to the fifo in the same subshell:

# Create pipe and start reader.
mkfifo pipe
cat pipe &
# Write to pipe.
(
  echo one
  echo two
) >pipe

If you have some more complexity, you can open the pipe for writing:

# Create pipe and start reader.
mkfifo pipe
cat pipe &
# Open pipe for writing.
exec 3>pipe
echo one >&3
echo two >&3
# Close pipe.
exec 3>&-


回答3:

You can solve this very easily by opening the read side of the pipe in read-write mode. The reader only gets an EOF once the last writer closes. So opening it in read-write makes sure there is always at least one writer.

So change your second example to:

mkfifo pipe
cat <>pipe &
echo "some data" >pipe


回答4:

Honestly, the best way I was able to get this to work was by using socat, which basically connections two sockets.

mkfifo foo
socat $PWD/foo /dev/tty

Now in a new term, you can:

echo "I am in your term!" > foo
# also (surprisingly) this works
clear > foo

The downside is you need socat, which isn't a basic util everyone gets. The plus side is, I can't find something that doesn't work... I am able to print colors, tee to the fifo, clear the screen, etc. It is as if you slave the whole terminal.



回答5:

As an alternative to the other solutions here, you can call cat in a loop as the input to your command:

mkfifo pipe
(while true ; do cat pipe ; done) | bash

Now you can feed it commands one at a time and it won't close:

echo "'echo hi'" > pipe
echo "'echo bye'" > pipe

You'll have to kill the process when you want it gone, of course. I think this is the most convenient solution since it lets you specify the non-exiting behavior as you create the process.



回答6:

I enhanced the second version from the Jonathan Leffler's answer to support closing the pipe:

dir=`mktemp -d /tmp/temp.XXX`
keep_pipe_open=$dir/keep_pipe_open
pipe=$dir/pipe

mkfifo $pipe
touch $keep_pipe_open

# Read from pipe:
cat < $pipe &

# Keep the pipe open:
while [ -f $keep_pipe_open ]; do sleep 1; done > $pipe &

# Write to pipe:
for i in {1..10}; do
  echo $i > $pipe
done

# close the pipe:
rm $keep_pipe_open
wait

rm -rf $dir