开发者

Command line tool to listen on several FIFOs at once

开发者 https://www.devze.com 2023-03-23 06:10 出处:网络
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

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?


tail will do that.

Use:

tail -q -n +1 a b

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:

#!/bin/sh
rm c
cat a >> c &
cat b >> c &
wait

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号