Some problem with script.

guys i have a Script was not written by me... that delete a particular file once it ages 7 days...
never the less, the script is not working as planed...

can someone help me out?

@fields = split( /\./, $logname );

    $_ = $fields[1]; \# get roll over time

    \#  $1        $2    $3
    \#  Y Y Y Y   M M   D D
    /\(\\d\\d\\d\\d\)\(\\d\\d\)\(\\d\\d\)/;

    $year = $1 - 1900;  <-----WHY 1900??
    $month = $2 - 1; <----- one month back???
    $day = $3;

    $time\_of_log = timelocal\( 0, 0, 0, $day, $month, $year \);

    if \($time\_of_log < $time_expired\)
    \{
        \# delete file
        $filename = $dirname . "/" . $logname;
        unlink $filename;

ERROR received,

Use of uninitialized value in subtraction (-) at ./script line 81.
Use of uninitialized value in subtraction (-) at ./script line 82.
Month '-1' out of range 0..11 at ./script line 85

I don't understand some of the lines here... is there a software that would show you the output for each line? i think that would help tremendously!

Message deleted, wrong input.

Actually, the assignment syntax in the script is correct, this is perl.

Oh sorry for that, I never noticed that and I dont any thing about PERL too.

$logname doe not have a value. It appears to be a filename with an embedded date/time stamp.

UNIX time (internal) is a number of seconds since an arbitrary starting time.... 1900 is the 'earliest' year, so 2006 would be year 106 which is 2006 - 1900.

Months start counting from zero ie., Jan = zero , Feb = 1....

Thanks jim... i believe the 1900 is the epoch year right?.. okay...
then something is wrong...
it seems that i earlier suspected that it could be the assignment of values, but reborg confirms that its correct... hmmmm...

The epoch year is 1970.

$ perl -e "printf localtime(0) . \"\\n\";"
Wed Dec 31 18:00:00 1969

My time zone is GMT-6 hours, so I have to add 6 hours for the epoch:

$ perl -e "printf localtime(0 +(60)*(60)*(6) ) . \"\\n\";"
Thu Jan  1 00:00:00 1970

The error message indicates that variables $1 , $2 and $3 are not initialized.

What value is assigned to $logname?

not initialized? can you please elaborate futher?

beside's the $logname is define as...

while ($logname = readdir( DIR ))
{
    if ($logname =~ m/not_terminated/ || $logname =~ /^\./)
    {
        ; # leave this file alone
    }

    else
    {
        @fields = split( /\./, $logname );

        $_ = $fields[1]; # get roll over time

        #  $1        $2    $3
        #  Y Y Y Y   M M   D D
        /(\d\d\d\d)(\d\d)(\d\d)/;

        $year = $1 - 1900;
        $month = $2 - 1;
        $day = $3;

        $time_of_log = timelocal( 0, 0, 0, $day, $month, $year );

        if ($time_of_log < $time_expired)
        {
            # delete file
            $filename = $dirname . "/" . $logname;
            unlink $filename;
        }
    }
}

closedir( DIR );

Moderator's note: 12yearold, please be sure to put your code within code tags. Makes it easier to read.

The $1 $2 $3 varaibles are returned by executing a regex with /() () ()/ in it.

Each separate (regex) becomes $1 or $2 according to the order the results are found.

Your code is not finding the regex, ie. the files are not there, or not named the way the code expects. So $1, $2, $3 are not defined.

you were SPOT ON!!!
solid find bro....

now i have one slight problem...

is my filename is 20060726160000.20060727160000.xxxxxxxxxx
and i would just like to use the 20060726 part, so i just include in the brackets... but attach no variables to it?

    \#  $1        $2    $3
    \#  Y Y Y Y   M M   D D
    /\(\\d\\d\\d\\d\)\(\\d\\d\)\(\\d\\d\)\(\\d\\d\\d\\d\).\(\\d\\d\\d\\d\)\(\\d\\d\)\(\\d\\d\)\(\\d\\d\\d\\d\).//;

would that be okay?

hmmm...tried... it doesn't work...
would appreciate some help please....

Come on. Learn some Perl first and look for some of the beginner's tutorials online first. Don't really expect to use Perl effectively without any reading. It is not going to work.

my ($y, $m, $d);
my $str = "200610240000.abcdefghijkl";
if ($str =~ /^(\d{4})(\d{2})(\d{2})/) {
    ($y, $m, $d) = ($1, $2, $3);
}
# $y = 2006
# $m = 10
# $d = 24

sorry... i agree with you about learning up alil... but perl isn't really my thing.. it just happens.. that i need to modify this script to get it work...

i'm sorry for posting such questions... but thanks anyways for trying to help...
appreciate it..

There's nothing wrong posting these questions. But what I would like to say is the fact, that if you do not have some prior learning on your side, you are unlikely to arrive at the answer on your own the next time and you will learn nothing, relying on others to give you the answers.

Therefore, help others help you.

cbkihong... I agree with what you say.. i'll make it a point to read something up when i have the time... and i bear no grudges on you for what you say.. i know you mean well... thanks