Dates not comparing correct even the same format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Dates not comparing correct even the same format
# 15  
Old 09-15-2010
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
# 16  
Old 09-15-2010
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)
# 17  
Old 09-15-2010
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
# 18  
Old 09-15-2010
Could you check if specifying time-style makes a difference ?

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

and

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

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 19  
Old 09-15-2010
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..


Quote:
Originally Posted by durden_tyler
Could you check if specifying time-style makes a difference ?

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

and

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

tyler_durden
This User Gave Thanks to Guyverix For This Post:
# 20  
Old 09-16-2010
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Error while comparing dates

Hi I am failing to write a script which compares a list of dates in a file with today's date. OS: Solaris 10 I have a file which has server names & dates, i need to compare the date in this file with today's date, if it is less than today it should print the server name. code i tried is ... (3 Replies)
Discussion started by: nanz143
3 Replies

2. Shell Programming and Scripting

Comparing Dates

Hi I'm trying to compare the current date (dd-Mmm-yyyy) against a variable that is an extracted date from an sql script. Below is the code: datenow=`date '+%d-%h-%Y'` #datenow is the current date in the format dd-Mmm-yyyy sqlplus $dbuserid/$dbpassword @ $SCRIPT_PATH/business-date.sql >... (3 Replies)
Discussion started by: joyAV
3 Replies

3. Shell Programming and Scripting

Comparing dates

Hi, I want to compare today's date(DDMMYYYY) with yesterday(DDMMYYYY) from system date,if (today month = yesterday month) then execute alter query else do nothing.One more condition is change of year also i.e today is Jan1 2012 and yesterday is Dec 31 2011. The above rek i want in Shell... (4 Replies)
Discussion started by: kumarmsk1331
4 Replies

4. Shell Programming and Scripting

Could not able to get the correct dates.

Hi, I have written the following script which will provide the first date of the 3rd previous month and last date of the 2nd previous month but when I change the month for 03 (March) then I am getting the incorrect results. Ex : - If today is 1 Jan 2010 then the script should provide the... (1 Reply)
Discussion started by: kandi.reddy
1 Replies

5. UNIX for Dummies Questions & Answers

comparing two dates.

Hi I have yesterday date and todays date stored in two variables. Today date is stored in variable -- testdate=`date +%m/%d/%Y` I found the yesterday date and stored in variable -- ydate=$month'/'$day1'/'$year Now i am trying to find out whether $testdate is less that $ydate. I am... (6 Replies)
Discussion started by: intiraju
6 Replies

6. Shell Programming and Scripting

Need script to generate all the dates in DDMMYY format between 2 dates

Hello friends, I am looking for a script or method that can display all the dates between any 2 given dates. Input: Date 1 290109 Date 2 010209 Output: 300109 310109 Please help me. Thanks. :):confused: (2 Replies)
Discussion started by: frozensmilz
2 Replies

7. Shell Programming and Scripting

comparing dates

Hi guys I have a a variable called check_ts which holds a date value. this date value keeps refreshing every 15 minutes. I am going to start a cron job 5 minutes after the refresh. I have to check if the current date > 20 min of check_ts. how do i do that. thanks ragha (17 Replies)
Discussion started by: ragha81
17 Replies

8. Shell Programming and Scripting

Comparing two dates

Hi, Can some one guide me how to compare two dates in unix. TIA Gupta (5 Replies)
Discussion started by: guptan
5 Replies

9. Programming

comparing dates

hi is there a c function in linux for comparing dates. thanx in advance. svh (2 Replies)
Discussion started by: svh
2 Replies

10. Shell Programming and Scripting

comparing 2 dates

hi , I have two variables both containg dates, x= `date` and y= `date' their format being -> Fri Nov 12 22:59:50 MST 2004 how do I compare which one is greater. ->Can dates be converted into integer and then compared? ( one lengthy way would be to compare the words one by... (7 Replies)
Discussion started by: k_oops9
7 Replies
Login or Register to Ask a Question