Linux 执行期间的管道数据
我想在不等待程序执行结束的情况下实时修改程序的输出 例如,我有一个程序:speak.plLinux 执行期间的管道数据,linux,bash,perl,Linux,Bash,Perl,我想在不等待程序执行结束的情况下实时修改程序的输出 例如,我有一个程序:speak.pl #!/usr/bin/env perl use 5.010; use Time::HiRes qw( usleep ); for(1..10) { say "I am counting: $_"; usleep(100000); } 还有这个:wrapper.pl: #!/usr/bin/env perl print "You are saying: $_\n" while(&
#!/usr/bin/env perl
use 5.010;
use Time::HiRes qw( usleep );
for(1..10) {
say "I am counting: $_";
usleep(100000);
}
还有这个:wrapper.pl:
#!/usr/bin/env perl
print "You are saying: $_\n" while(<>);
不幸的是,在speak.pl终止其执行之前,不会发生任何事情。有没有办法在执行过程中修改speak.pl的输出数据
我还尝试通过一个中间文件传递:
./speak.pl > tmp & ./read.pl tmp
为此:
#!/usr/bin/env perl
use 5.010;
use File::Tail;
my $file=File::Tail->new(name=>$ARGV[0], maxinterval=>300, adjustafter=>7);
while (defined(my $line=$file->read)) {
print "You're saying: $line";
}
但输出文件似乎只在执行结束时写入
有什么线索吗
顺便说一句,我正在用Cygwin跑步,希望不会有什么不同 这可能与缓冲有关。数据在通过管道发送(或在执行结束时刷新)之前存储到一定大小 尝试使用usig
stdbuf-i0-o0-e0./speak.pl | stdbuf-i0-o0-e0./wrapper.pl
禁用缓冲
使用Perl时,请考虑禁用Perl自己的缓冲机制。
#!/usr/bin/env perl
use 5.010;
use File::Tail;
my $file=File::Tail->new(name=>$ARGV[0], maxinterval=>300, adjustafter=>7);
while (defined(my $line=$file->read)) {
print "You're saying: $line";
}