2012-05-04 61 views
2

根据solution provided in perldoc,我试图效仿tail -f,但它不能按预期工作。下面的代码可以首次打印所有行,但不会附加新行。请您详细说明我是否在此遗漏任何事情。在Perl中模拟tail -f

#!/usr/bin/perl 
open (LOGFILE, "aa") or die "could not open file reason $! \n"; 

for (;;) 
{ 
     seek(LOGFILE,0,1); ### clear OF condition   
     for ($curpos = tell(LOGFILE); <LOGFILE>; $curpos = tell(LOGFILE)) 
     { 
       print "$_ \n"; 
     } 

     sleep 1; 
     seek(LOGFILE,$curpos,0); ### Setting cursor at the EOF 
} 
+0

“aa”是什么意思? – sarnold

+0

aa是不断增长的文件名,我需要做tail -f aa。 – user419534

+3

另请参阅[File :: Tail](http://search.cpan.org/perldoc?File::Tail) – ikegami

回答

1

适合我。你是如何更新"aa"? 如果是buffered写入"aa",您不会立即看到数据。 您可以在不同的终端中尝试以下操作,并检查您是否看到任何更新。

while (1) 
echo "test" >> aa 
end 

如果你正在使用Perl来更新aa,检查this节上缓冲和如何禁用。

+0

如果我使用vi编辑器添加行,它不起作用。而回声“测试”>> aa确实有效。 – user419534

+2

那么你可能[患有缓冲](http://perl.plover.com/FAQs/Buffering.html)。 – mob

+0

当你使用vi时,'tail -f aa'怎么样?当你使用'vi'来更新''aa''时,你是否看到'tail -f“aa”'和你的脚本的不同行为? – dpp