Operating System - HP-UX
1753930 Members
10121 Online
108810 Solutions
New Discussion юеВ

Perl script not working as expected

 
SOLVED
Go to solution
KAKA_2
Honored Contributor

Perl script not working as expected

Hi Experts,

I want to check if a file how old is the file. to check this the simple code is as below.

##############################################
#!/usr/bin/perl

$file="/tmp/access_test_log";
my($SECS) = 24*60*60; #seconds in a day

my $ageinsecs = int((-M $file)* $SECS);
print "value of age in secs is $ageinsecs";
##############################################
output is as below:
value of age in secs is 0

Where ls -l /tmp/access_test_log
-rw-r----- 1 root system 0 Jun 21 15:39 /tmp/access_test_log

so if we see actually it is very old file but output say 0. Can some one help me to fix the issue???

KAKA
4 REPLIES 4
john korterman
Honored Contributor

Re: Perl script not working as expected

Hi KAKA,

I also get "0" for a non-existing file. You probably have an invisible character somewhere in the file name path.

regards,
John K.
it would be nice if you always got a second chance
KAKA_2
Honored Contributor

Re: Perl script not working as expected

I dont see any such invisible character in the file path name.

Is there other way around to check how old is file???


KAKA
James R. Ferguson
Acclaimed Contributor
Solution

Re: Perl script not working as expected

Hi:

Generalize your script and be defensive:

# cat ./myage
#!/usr/bin/perl
use strict;
use warnings;
my $file = shift or die "filename expected\n";
die "'$file' doesn't exist\n" unless -f $file or -d $file;
my $SECS = 24 * 60 * 60; #seconds in a day
my $ageinsecs = int( ( -M $file ) * $SECS );
print "value of age in secs is $ageinsecs\n";

# ./myage "file with spaces in name"

# ./myage myfile

# ./myage /tmp

Regards!

...JRF...
KAKA_2
Honored Contributor

Re: Perl script not working as expected

you are great. It solved my issue. Thank You.
KAKA