Perl 中的日志文件检查验证

Perl 中的日志文件检查验证

我还有“FDP_RecordLength_Error_02202018_020107.log”“FDP_HeaderOrTrailerRecord_Error_02202018_020107.log”“FDP_DetailRecord_Error_02202018_020107.log”和其他日志文件。我想检查所有日志的文件大小。如果日志文件大小为零,则它将打印“日志文件大小为零”或“日志文件大小不为零”。我怎样才能在 Perl 中做到这一点?谁能帮我吗 ?

答案1

你可以只使用find

find . -type f -size 0 -exec echo "The logfile has a 0 size: {}" \;

find . -type f ! -size 0 -exec echo "The logfile does not have a 0 size: {}" \;

或者perl

#!/usr/bin/perl --
use File::Find;

# directory to start looking for log files
my $dir = '/tmp/a';

# search base directory and call subroutine for each file found
find(\&size_check, $dir);

# subroutine to be called by find
sub size_check{
        # check filename matches regex and is a file (not directory)
        if($_ =~ /^.*\.log$/ and -f $_){
                # call stat and put data into an array
                my @stat = stat($_);

                # check to see if the size is zero
                if($stat[7] == 0){
                        print $_ . " has a size of 0\n";
                }else{
                        print $_ . " has a " . $stat[7] . " size\n";
                }
        }
}

相关内容