I'm trying to create a PERL script to delete old log files. One of the key things I want the script to be able to do is allow me to pass arguments for directory, name of the file (such as test.log-*), and age of the file.
It's been a while since I've used PERL and I'm not that great anyway, so I'd appreciate some help. I'm also not terribly familiar with the getopt::long module. Here's what I'm thinking so far, and while I'm sure it's not correct, please give me any feedback that might assist.
I want to run the script along the lines of "script.pl --dir /release/logs --type test.log-* --days 7"
#!/usr/perl
use strict;
use warnings;
use Data::Dumper;
use Getopt::Long;
my $file;
my ($dir,$type,$days);
GetOptions( 'dir' => $dir,
'type' => $type,
'days' => $days);
foreach my $file (<$dir/$type>){
if (-M $file < $days) {
print "
Deleting log more than '$days' old:".$file;
unlink $file;
# or die "
Failed to remove $file";
}
}
exit;
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…