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

前端 未结 6 1568
面向向阳花
面向向阳花 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:49

    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>&-
    

提交回复
热议问题