我想使用 xinput 来监视击键次数和鼠标移动按下次数。为了简单起见,我想要的是这两个命令:
xinput test 0
xinput test 1
同时写入屏幕。
我在 Perl 脚本中使用它,例如:
open(my $fh, '-|', 'xinput test 0') or die $!;
while(my $line = <$fh>) {
...stuff to keep count instead of logging directly to file
}
编辑: 像这样:
open(my $fh, '-|', 'xinput test 0 & xinput test 1') or die $!;
不起作用。
最佳答案
我不确定您想对输出做什么,但听起来您想同时运行这些命令。在这种情况下,我的第一个想法是每个命令 fork 一次 Perl 进程,然后执行您关心的命令的子进程。
foreach my $command ( @commands ) { # filter @commands for taint, etc
if( fork ) { ... } #parent
else { # child
exec $command or die "Could not exec [$command]! $!";
}
}
fork 进程共享相同的标准文件句柄。如果您在父进程中需要它们的数据,则必须在两者之间建立某种通信。
CPAN 上还有几个 Perl 框架用于处理异步多进程的东西,例如 POE , AnyEvent , 等等。他们会为您处理所有这些细节。
关于linux - 如何将两个命令的输出并行发送到标准输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20817584/