可同时监听多个 FIFO 的命令行工具
我正在寻找一种工具来一次读取多个 FIFO(可能使用 select(2)
)并输出读取的内容,并在所有 FIFO 关闭时关闭流。更准确地说,程序
的行为如下:
$ mkfifo a b
$ program a b > c &
$ echo 'A' > a
$ echo 'B' > b
[1] + done program a b > c
$ cat c
A
B
$ program a b > c &
$ echo 'B' > b
$ echo 'A' > a
[1] + done program a b > c
$ cat c
B
A
我的第一次尝试是使用cat
,但第二个示例不起作用(echo 'B' > ; b
将挂起),因为 cat
按顺序读取每个参数,而不是同时读取。在这种情况下,正确使用的工具是什么?
I am looking for a tool to read several FIFOs at once (probably using select(2)
) and output what is read, closing the stream when all the FIFOs are closed. To be more precise, the program
would behave as follows:
$ mkfifo a b
$ program a b > c &
$ echo 'A' > a
$ echo 'B' > b
[1] + done program a b > c
$ cat c
A
B
$ program a b > c &
$ echo 'B' > b
$ echo 'A' > a
[1] + done program a b > c
$ cat c
B
A
My first attempt was to use cat
, but the second example will not work (echo 'B' > b
will hang), because cat
reads from each argument in order, not simultaneously. What is the correct tool to use in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尾巴会做到这一点。
使用:
编辑: 抱歉,没用。我看看能不能找到别的东西。
抱歉,我找不到任何东西。
如果您不想自己编程,那么我的建议是多个命令:
您可能会得到一些交错,但否则一切都应该正常工作。等待是为了防止程序退出,直到所有 cat 程序完成为止(以防万一您在完成所有操作后需要运行一些命令)。 rm 是为了确保 c 开始为空,因为 cat 命令附加到文件中。
tail will do that.
Use:
Edit: Sorry that didn't work. I'll see if I can find something else.
Sorry, I could not find anything.
If you don't want to program this yourself then my suggestion is multiple commands:
You may get some interleaving but otherwise everything should work fine. The wait is to prevent the program from exiting till all the cat programs are done (just in case you have some command you need to run after everything is done). And the rm is to make sure c starts empty since the cat commands append to the file.