Dates not comparing correct even the same format

I have the date of the file passed into a variable also current date formatted same passed into a separate variable and compare the two with an if statement and statement always comes up false. Even though I verified the dates. Any help would be awesome.

Filecrtdate=`ls -l $i | awk '{print $6}'`    # declaring variable of file create date
Rundate=`date +%Y-%m-%d`    # Declaring todays date variable in format matching Filecrtdate                                      

if [ $Filecrtdate = $Rundate ]
      then echo "File created today"
      else echo "File not created today"
fi

There must be a space between the square brackets and the statement:

[ $Filecrtdate = $Rundate ]
 ^                       ^

Sorry, yes that was actual correct in the code, just a typo when I entered in post. Any other ideas?

I am not sure if you are asking for this :-

$ cat f1.sh
#!/bin/sh
Rundate=`date +%Y-%m-%d`
echo $Rundate
Filecrtdate=`date +%Y-%m-%d`
echo $Filecrtdate

if [ $Filecrtdate == $Rundate ]
then
  echo "successful"
else
  echo "unsuccessful"
fi

$ sh f1.sh
2010-09-15
2010-09-15
successful

You need to add double ==

Your code works fine for me.

Very strange.... did you use a windows editor?

yes Cgwin

1 Like

If you have used a windows editor you have to remove the CR's:

tr -d '\r' < dosfile > unixfile
1 Like

No logging into server with putty and using nano to open and edit script

Can you post a sample output format from:

ls -l any_valid_filename

Is the date in the format YYYY-MM-DD ?

1 Like

Yes it is cygwin bash shell...that i executed the script

yes date is in that format which is why i modified the current date in same format so comparing same string. Now here is the interesting thing. When I type

ls -l file1.txt | awk '{print $6}'

into the command line get the date formatted 2010-09-15
when I modify the code to read

echo "The modified date of file is $Filecrtdate"

im getting Sep as the output in the script

---------- Post updated at 01:05 PM ---------- Previous update was at 12:55 PM ----------

output reads as follows

-rw-r--r-- 1 rmarcada students 0 2010-09-01 22:04 file1.txt

So are you saying that ls -l file1.txt returns this output -

-rw-r--r-- 1 rmarcada students 0 2010-09-01 22:04 file1.txt

and ls -l file1.txt | awk '{print $6}' returns this ?

Sep

tyler_durden

and ls -l file1.txt | awk '{print $6}' returns 2010-09-01
echo "The modified date of file is $Filecrtdate" returns Sep

when I change script to read
Filecrtdate=`ls -l file1.txt | awk '{print $6,$7,$8}`
echo "The modified date of file is $Filecrtdate" it returns Sep 1 22:04

Doesn't make sense seeing how the $6 field in the ls -l command is the date $7 is time stamp and $8 should be file name

Have you thought about just converting these dates into integers for the comparison?

date +%s for now, and
date --date=yesterday +%s (or whatever you inject via loop)

Yes but I would still like to know why this is not working as it should. The way I have it is it compares to strings in identical format but for some reason when I type out the ls command under the command line I get different data then what is passed into the variable. I don't know why. Logic would say if i type
ls -l file1.txt | awk '{print $6}' and it displays 2010-09-01
now do
Filecrtdate=` ls -l file1.txt | awk '{print $6}'` my variable should have the value of 2010-09-01 instead it just has the value of SEP
when I echo $Filecrtdate it has the value of SEP

Could you check if specifying time-style makes a difference ?

ls -l --time-style=long-iso f5 | awk '{print $6}'

and

x=`ls -l --time-style=long-iso f5 | awk '{print $6}'`
echo $x

tyler_durden

1 Like

you could try ls -lat --time-style=iso

---------- Post updated at 01:36 PM ---------- Previous update was at 01:35 PM ----------

Grin, guess I should refresh my pages when I get back from lunch. These would work as well..

1 Like

OK found the problem. Problem was the ls command used in the command line was of a different format then the ls command used within the bash script. So when I compared the data at the command line I was comparing the same results. We I run ls within the bash script it was formatted to show the name of month therefore the data was not the same. Problem was resolved by using the stat command and extracting the date needed from that output comparing it to the system date which was formatted to match. Script is now working as intended. Thank you all for your help.