compare file's date with machine date (293 Views)
Reply
Valued Contributor
DeafFrog
Posts: 294
Registered: ‎02-25-2009
Message 1 of 5 (293 Views)

compare file's date with machine date

Hi Gurus ,

 

Script to compare file's date with machine date.

 

d=$(date | awk '{print $2,$3}')
d2=$(ls -la $file | awk '{print $6,$7}')
if [ "$d" = "$d2" ]
then
echo "they match"
elif [ "$d" != "$d2" ]
then
echo "Check date"
fi

The issue is that when the system date is single digit , like "Feb 2" , the value of
d will be "Feb  2" in my machine (extra space between Feb and 2)
and the value of d2 will be "Feb 2" , this will led to a incorrect run of script.

The above script will run fine if the date is double digit , like "Feb 12".
Machine is running Hp ux v3.

 

 

 

 


Referance :
John Meissner solution in the tread
http://h30499.www3.hp.com/t5/System-Administration/file-date-verses-current-date/m-p/2879437/highlig...

FrogIsDeaf
Acclaimed Contributor
Dennis Handly
Posts: 25,058
Registered: ‎03-06-2006
Message 2 of 5 (292 Views)

Re: compare file's date with machine date

>d=$(date | awk '{print $2,$3}')

 

There is no need to use awk here.  date(1) has a "+ format" that will do this.

 

>elif [ "$d" != "$d2" ]

 

There is no need to check twice.

 

What locale are you using?  Some scummy locales suppress blanks.  Some have blanks.

The "right" local has leading zeros.

 

Valued Contributor
DeafFrog
Posts: 294
Registered: ‎02-25-2009
Message 3 of 5 (289 Views)

Re: compare file's date with machine date

Hi Dennis , Thank you for the reply. I will try something like day=`date +%d` ;echo $day , to check if it display like "2" or " 2" ( space and then 2) once i change the date on a test system. here's the locale o/p LANG= LC_CTYPE="C" LC_COLLATE="C" LC_MONETARY="C" LC_NUMERIC="C" LC_TIME="C" LC_MESSAGES="C" LC_ALL= Regards , .
FrogIsDeaf
Acclaimed Contributor
Dennis Handly
Posts: 25,058
Registered: ‎03-06-2006
Message 4 of 5 (277 Views)

Re: compare file's date with machine date

>I will try something like day=$(date +%d)

 

(Using "%e" would have that space.)

 

If you don't want to fiddle with locales and message catalogs, you can use awk to convert to numeric:

d1=$(date | awk '{print $2*100 + $3}')
d2=$(ll $file | awk '{print $6*100 + $7}')
if [ $d1 -eq $d2 ]; then

Acclaimed Contributor
Dennis Handly
Posts: 25,058
Registered: ‎03-06-2006
Message 5 of 5 (270 Views)

Re: compare file's date with machine date

Oops that won't work since you have the month abbreviation.  Try:

d1=$(date | awk '{print $2, $3 + 100}')
d2=$(ll $file | awk '{print $6, $7 + 100}')

if [ "$d" = "$d2" ]; then

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.