1

I'm writing a script which checks for the space in the server and deletes the Old backup/'s is the space is less than 2 GB . The Script is working fine , but as i'm using use strict; use warnings; in my script for my practise purpose this error is thrown out .

Here is the script

#!/usr/bin/perl
use strict;
use warnings;

my @backups;
my $now=time();
my $dayago=10;

my (@space,@freesp);
@space=grep /\/dev\/md0/,`df`;

for(@space){
        chomp;
        @freesp=split /\s+/ ,$_;
        }

chdir '/home/ftpusr/backup' or die "Can't cd to backup dir: $!\n";

while (($freesp[3]/1024/1024 < 2.0) && ($dayago > 0)){
                my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($now-$dayago*60*60*24);
                my $today_timestamp=sprintf("%4d%02d%02d",$year+1900,$mon+1,$mday);
                        @backups = glob("*NODE0000.CATN0000.$today_timestamp*");
                        #print "@backups\n";
                        $dayago -= 1;
                        #print "$dayago\n";
                        unlink ($backups[0]);
        }

I have initialized $dayago parameter to 10 so that it checks for backup from last 10 days and comes near to current date , so as far as i understood for the 1st iteration of while loop its did not find the specific file with the timespace so it could not inlink so throwed up this warning . Is there any way can i eliminate this warning ?

Here is the O/P when i run the script

    9
    Use of uninitialized value in unlink at purge3.pl line 26.

    8
    Use of uninitialized value in unlink at purge3.pl line 26.

    7
    Use of uninitialized value in unlink at purge3.pl line 26.

    6
    Use of uninitialized value in unlink at purge3.pl line 26.

    5
    Use of uninitialized value in unlink at purge3.pl line 26.

    4
    Use of uninitialized value in unlink at purge3.pl line 26.
    GSRTC.0.db2inst1.NODE0000.CATN0000.20130315102900.001
    3
    GSRTC.0.db2inst1.NODE0000.CATN0000.20130316150941.001 GSRTC.0.db2inst1.NODE0000.CATN0000.20130316171526.001
    2

    1
    Use of uninitialized value in unlink at purge3.pl line 26.
    GSRTC.0.db2inst1.NODE0000.CATN0000.20130318095532.001
    0

And the files in the directory will be listed like which i need to delete them

GSRTC.0.db2inst1.NODE0000.CATN0000.20130315102900.001
GSRTC.0.db2inst1.NODE0000.CATN0000.20130316150941.001
GSRTC.0.db2inst1.NODE0000.CATN0000.20130318095532.001
AWDRT.0.db2inst1.NODE0000.CATN0000.20130319092156.001
GSRTC.0.db2inst1.NODE0000.CATN0000.20130319095258.001
4

2 回答 2

1

如果$backups[0]undef,那么这意味着您glob没有匹配任何文件(大概是因为那天没有任何文件)。如果没有找到文件,则无需unlink任何操作。

所以将unlink行更改为

unlink($backups[0]) if @backups;

或者

unlink($backups[0]) if $backups[0];
于 2013-03-19T11:41:14.053 回答
1

如果你稍微改写一下,你可以让 Perl 来做检测:

my @backups = glob "*NODE0000.CATN0000.$today_timestamp*";
unlink @backups;

甚至

unlink glob "*NODE0000.CATN0000.$today_timestamp*";  # No conditionals!

但我认为这File::Find是解决您的问题的正确方法。

于 2013-03-19T12:15:22.027 回答