Using named pipes with bash - Problem with data loss

前端 未结 6 2213
余生分开走
余生分开走 2020-12-14 10:16

Did some search online, found simple \'tutorials\' to use named pipes. However when I do anything with background jobs I seem to lose a lot of data.

[[Edit: found a

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-14 11:04

    Like camh & Dennis Williamson say don't break the pipe.

    Now I have smaller examples, direct on the command line:

    Server:

    (
      for i in {0,1,2,3,4}{0,1,2,3,4,5,6,7,8,9};
      do
        if read s;
          then echo ">>$i--$s//";
        else
          echo "<<$i";
        fi;
      done < tst-fifo
    )&
    

    Client:

    (
      for i in {%a,#b}{1,2}{0,1};
      do
        echo "Test-$i" > tst-fifo;
      done
    )&
    

    Can replace the key line with:

        (echo "Test-$i" > tst-fifo&);
    

    All client data sent to the pipe gets read, though with option two of the client one may need to start the server a couple of times before all data is read.

    But although the read waits for data in the pipe to start with, once data has been pushed, it reads the empty string forever.

    Any way to stop this?

    Thanks for any insights again.

提交回复
热议问题