【问题标题】:How can I block a pipe until a pattern match?如何阻止管道直到模式匹配?
【发布时间】:2016-02-12 09:54:21
【问题描述】:

如何在匹配模式之前阻塞管道? 我想做这样的事情:

#!/bin/sh

mkfifo request response
while true; do # needs to send one response at a time
  nc -N -l 127.0.0.1 4000 >request <response & # send output to request pipe
  sed -u '/^$/q' <request; # block until a blank line is received
  date | tee response # send input back to the original command
done

【问题讨论】:

    标签: unix pipe sh named-pipes


    【解决方案1】:

    如果匹配模式的操作是阻塞的,cat 可以用来防止数据从命名管道中出来,直到有东西被写入它。 cat 将从命名管道中读取数据,直到将 EOF 写入其中。

    #!/bin/sh
    
    mkfifo response
    while true; do # send one response at a time
      cat response | nc -N -l 127.0.0.1 4000 | # pipe request out
      (
        sed -u '/^$/q' # block until a blank line is received
        date | tee response # send input back to the original command
      )
    done
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-20
      • 2015-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多