为什么 cat 命令只从第一个文件描述符中读取?

Nar*_*asK 1 file-descriptors cat

在控制台中,我创建了 2 个空文件,并尝试同时读取它们。

$ echo -n '' | tee f1 > f2
$ cat f1 f2
$ cat <(tail -f f1) <(tail -f ./f2)
Run Code Online (Sandbox Code Playgroud)

在另一个控制台上,我运行了我的测试。

$ echo 'tee test' | tee -a f1 >> f2
$ echo 'f1 test' >> f1
$ echo 'f2 test' >> f2
$ cat f1 f2
tee test
f1 test
tee test
f2 test
Run Code Online (Sandbox Code Playgroud)

但是,cat在第一个控制台上只读取第一个fd.

$ cat <(tail -F ./f1) <(tail -F ./f2)
tee test
f1 test
Run Code Online (Sandbox Code Playgroud)

为什么?然后如何从两个或多个文件描述符中同时读取?

Ste*_*itt 6

cat依次处理其参数;tail -f f1继续运行,所以cat一直在等待输入<(tail -f f1),并且不会继续处理<(tail -f f2)

你会看到从输出tail -f f2,如果你杀第一tail

同时跟踪多个文件的更好工具是tail它本身(至少是 GNU tail):

tail -f f1 f2
Run Code Online (Sandbox Code Playgroud)

如果您不想看到文件头,请使用-q

tail -qf f1 f2
Run Code Online (Sandbox Code Playgroud)