簡體   English   中英

在Perl中模擬tail -f

[英]simulating tail -f in Perl

按照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
}

對我來說很好。 您如何更新"aa" 如果它是對"aa"buffered寫入,則不會立即看到數據。 您可以在其他終端中嘗試以下操作並檢查是否看到任何更新。

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

如果您正在使用perl更新aa ,請查看節有關緩沖以及如何禁用的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM