Shell comparing two dates in bourne shell

Hi All,

This should be easy, but I think it is due to my inexperience. I'm trying to compare to dates in bourne shell.

the first date comes from an SQL field type that stores the date and the second variable gets the date by this command: date -v -6m "+%F %T"

The two variables are
$login
$sixmonthsago

I need to print only the records that match if the login is less than six months ago.


From looking around, I thought this might work, but it seems to print all records still. Note: there are records older than six months being printed. I also wondered if it is messing up since some $logins are NULL or blank.
if [ "$login" \< "$sixmonthsago" ]
then
print record
fi


I will continue to work on solving this, just thought I would ask more experienced people.
 
Translate each date to their epoch seconds numerical value. Then you can simply test if one integer value is larger than the other.
 
Thanks Sir Dice

I ended up not needing to do that, and was just coming back to say I got it working now. The problem is that with how I was pulling back the data from the SQL statement. Thank you for taking the time to reply and giving me another option. :)
 
You have to express dates into epoc times before comparing them.


date -j -f "%F %T" "`date -v -6m`" "+%s"


Here a script that takes a date in format "%F %T" on the first argument.

Bash:
#!/bin/sh

# Six month ago
# -------------

SIXM=`date -j -v"-6m" +"%F %T"`

## Epoc
SIXM_EPOC=`date -j -f "%F %T" "$SIXM" "+%s"`

## express SQL date into epoc time
##

SQL_DATE_EPOC=`date -j -f "%F %T" "${1}" "+%s"`

##  test sql_date < sixm

[ "${SQL_DATE_EPOC}" -gt "$SIXM_EPOC" ] && echo "match"

You have to make sure that the date provided by the SQL query is "%F %T".

if [ "$login" \< "$sixmonthsago" ]
then
print record
fi

I wonder how the "\<" is interpreted by test(1)?
As the input redirection ?
 
SirDice string comparison is fine provided you can ensure YYYYMMDDHHMMSS on both sides of the comparison (before databases had date fields, that's how we used to do it).

I wonder how the "\<" is interpreted by test(1)?
As the input redirection ?
With the escape it is not redirection:

Code:
$ if [ "a" \< "b" ]; then echo yes;fi
yes
 
You have to make sure that the date provided by the SQL query is "%F %T".

Code:
if [ "$login" \< "$sixmonthsago" ]
then
print record
fi

I wonder how the "\<" is interpreted by test(1)?
As the input redirection?

Use -lt instead (means less than). Or -le for less than or equal to. Instead of \<
 
Use -lt instead (means less than). Or -le for less than or equal to. Instead of \<

According to the original request:
I need to print only the records that match if the login is less than six months ago.

It should even be `-gt`. - ;)


SirDice string comparison is fine provided you can ensure YYYYMMDDHHMMSS on both sides of the comparison (before databases had date fields, that's how we used to do it).

You are right, I did no pay attention to this syntax for comparing string before.
I have a good excuse though, the linux man page for test does not mention it.
 
Back
Top