2

根据perldoc 中提供的解决方案,我正在尝试模拟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
}
4

1 回答 1

1

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

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

如果您使用 perl 进行更新aa,请查看有关缓冲和如何禁用的 本节。

于 2012-05-04T02:46:05.757 回答