How to avoid echo closing FIFO named pipes? - Funny behavior of Unix FIFOs

前端 未结 6 1562
面向向阳花
面向向阳花 2020-12-04 08:10

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 &         


        
6条回答
  •  春和景丽
    2020-12-04 08:54

    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.

提交回复
热议问题