文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>windows下的tail脚本

windows下的tail脚本

时间:2009-08-25  来源:snowtty

以前写的windows下的tail脚本,用法仿照unix下的tail命令,用来看oracle的日志很方便:tail -f alertX.log

#!/usr/bin/perl
use strict;
use File::Tail;
my($flush,$tail_num,$filename);
my($exec_name)=$0;
&usage() if @ARGV>3 || @ARGV<1;
while(my $arg=shift @ARGV){     #判断命令行参数,-f用来标记刷新读取,-123的数字用来表示从最后的123行开始读取
        if($arg=~/^\-f\s*/){
                $flush=1;
        }
        elsif($arg=~/^\-(\d+)\s*/){
                $tail_num=$1;
        }
        elsif($arg=~/^\-\S*/||$arg=~/^\+\S*/){
                &usage();
        }
        else{
                $filename=$arg;
        }
}
&usage() unless $filename;
$tail_num=10 unless $tail_num;
my($timeout)=1;
die "No filename specified!\n" unless $filename;
unless(-f $filename){
        print STDERR "the file \"$filename\" does NOT exist!\n";
        exit;
};
my $f=File::Tail->new(name=>$filename,maxinterval=>1, tail=>$tail_num);
unless($flush){    如果没有-f参数,直接把结果输出
        print $f->read;
        exit;
}
my ($line,$nfound,$timeleft,$pending);
while (1) {   #如果有-f参数,那么不停的尝试读取文件,采用select的阻塞方式,这样占用资源最小
        ($nfound,$timeleft,$pending)=
                 File::Tail::select(undef,undef,undef,$timeout,$f);
    unless($nfound){
            #warn "timeout for waiting the file,and blocking...\n";
    }
    else{
       print $pending->read;
    }
}

sub usage{
        print "Usage: $exec_name [-f] [-N] filename\n";
        exit;
}



相关阅读 更多 +
排行榜 更多 +
滑动贴合3d

滑动贴合3d

休闲益智 下载
红蓝人冰火世界

红蓝人冰火世界

休闲益智 下载
阿凡提跑酷最新版

阿凡提跑酷最新版

冒险解谜 下载