Loan Tran wrote:
> 
> This is how i use the unlink. Please take a look.
> Thanks.
> -----------
> 
> #!/usr/bin/perl

You should enable warnings and strictures.

#!/usr/bin/perl -w
use strict;


> # clean old file
> 
> use Carp;
> 
> $cur_time = time;   # current time in epoch (in
> seconds)
> $dir = '/opt/sybase/OCT/dump/';
> $hour = 10 ;
> 
> opendir DIR, $dir || die "Failed to open $dir: $!";

This won't die because of the precedence of ||.  It is interpreted as:
opendir DIR, ( $dir || die "Failed to open $dir: $!" );

Use either:
opendir DIR, $dir or die "Failed to open $dir: $!";
Or:
opendir( DIR, $dir ) || die "Failed to open $dir: $!";


> my @files = readdir DIR;
> closedir DIR;
> unless ($cnt = @files) {
>  print "No file to rm.\n";
>  return;

return() does nothing here, it is for returning from subroutines. 
Perhaps you meant exit().


> }
> 
> foreach $file (@files) {
>         next if $file =~ /\./;

This was pointed out in another post.

        next if $file =~ /^\.\.?$/;


>         ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev,
> $size, $atime, $mtime, $ctime, $blksize, $blocks) =
> stat $file;
  ^^^^^^^^^^
  stat "$dir$file";

But since you are only using $mtime then this would be simpler:

my $mtime = (stat "$dir$file")[9];

Or better yet, since you only want the age in hours:

my $fage = -M "$dir$file";


>         $fage = ($cur_time - $mtime) / 3600; # file in
> hours (note: 1 hr = 3600 sec)
> 
>         print "file found: $file ($fage hr)\n";
> 
>         if ($fage > $hour) {
>                 printf "\nDelete file older than $hour
> hrs : %4s  %.2f hrs old\n", "$dir"."$file",$fage;
>                 $rc = unlink "$dir"."$file" ;
>                 $stat = $rc ? "success" : "failed";
>                 print "\t$file remove status:
> $stat.\n";
>         }
>         else {
>                 printf "Too young: %15s [%.2f hr]\n",
> $file, $fage;
>         }
> }
>         opendir DIR, $dir || die "Failed to open $dir:
> $!";

Same precence problem as with previous opendir.


>         my @files = readdir DIR;
>         $cnt2 = @files;
> 
>         print  "\nTo remove all files that are older
> than $hour in directory: $dir\n";
>         printf "Before delete: %5d\n", $cnt;
>         printf "Files deleted: %5d\n", $cnt - $cnt2;
>         printf "After delete : %5d\n", $cnt2;
>         closedir DIR;



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to