Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

compare file's date with machine date

Valued Contributor [Founder]

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/highlight/true#M100534

FrogIsDeaf
4 REPLIES
Acclaimed Contributor [Founder]

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 [Founder]

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 [Founder]

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

Highlighted
Acclaimed Contributor [Founder]

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