以前写的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;
}
阅读(3067) | 评论(2) | 转发(0) |