如何在模式匹配之前阻止管道?

时间:2016-02-12 09:54:21

标签: unix pipe sh named-pipes

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

#!/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

1 个答案:

答案 0 :(得分:0)

如果匹配模式的操作是阻塞的,则可以使用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