Error while comparing dates


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Error while comparing dates
# 1  
Old 03-24-2014
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
Code:
date1=`date +"%m%d%y"`
while read server date2
do
  if [[ "$date2" -le "$date1" ]]
  then
    echo " match"
  else 
    echjo " no match"
  fi
done < file

I am receiving an error for 08 day it says
[[:03/08: value too great for base (error token is "08")Smilie

Please help ...

Last edited by Franklin52; 03-24-2014 at 09:32 AM.. Reason: Please use code tags
# 2  
Old 03-24-2014
The error message indicates that the shell is trying to convert a string to a number and failing.

-le is a numeric comparison, so the shell is expecting numeric values. You haven't supplied any example input, but the error text is complaining about what looks like a slash-separated date. The shell can't convert that to a number.

Comparing dates like that won't give you valid results anyway (e.g. 280113 is numerically less than 100110) - you'll need something like 'days since', or a more sophisticated comparison.
This User Gave Thanks to CarloM For This Post:
# 3  
Old 03-24-2014
Try comparing the dates in YYMMDD format then you can compare them directly:

Code:
date1=`date +"%y%m%d"`
sed 's:/: :g' file | while read server month day year
do
  if [[ "$year$month$day" -le "$date1" ]]
  then
    echo " match"
  else
    echo " no match"
  fi
done

These 2 Users Gave Thanks to Chubler_XL For This Post:
# 4  
Old 03-27-2014
Thanks guys !!... that helped me... Smilie
i have also tried using " < " while comparing , even this was successfull ...

Thank you so much once again .... !!!! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing dates in shell script

Hi All, I have a date variable say dt="2014-01-06 07:18:38" Now i need to use this variable to search a log and get the entries which occured after that time. (1 Reply)
Discussion started by: Girish19
1 Replies

2. UNIX for Dummies Questions & Answers

about dates, comparing and calculation

Hi, can we do date comparisons in unix? like if i give the two dates find which date is old one and which is new one and difference between the two dates in terms of hours,minutes,days and months. (1 Reply)
Discussion started by: vmachava
1 Replies

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

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

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

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

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

8. Shell Programming and Scripting

Comparing last modified dates

Hi All. Can someone please give me an example of how I'd do a comparison to find out if the last modified date of a file is newer than yesterday (i.e. today - 1 day)? Example: if ; then echo "Do something..." fi Any ideas or examples? Thanks. (1 Reply)
Discussion started by: dmilks
1 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