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.
精彩评论