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
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
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
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
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.