if [ $NOWDATE -gt $STARTDATE ] , date comparison correct syntax?

i've looked at a bunch of the date comparison threads on these boards but unfortunately not been able to figure this thing out yet. still confused by some of the way conditionals handle variables...

here is what i where i am now...

# a bunch of initializition steps are here ...
STARTDATE=`date +%Y%m%d`
 
while [ 1 ] # loop running all the time. 
do

NOWDATE=`date +%Y%m%d`

# if day changes then i want to reinitialize
if [ $NOWDATE -gt $STARTDATE ] 
then
# do all that initiallization again 
fi

# ... a bunch of other code in here

done

but that if statement doesn't work. any tips?

thanks,
dan

Did you wait long enough ?

The value of NOWDATE will change when the day changes. So if you start your script and keep it running till midnight (when the day changes), the "if" condition will return true thereafter.

$
$ cat -n t1.sh
     1  #!/bin/bash
     2
     3  # a bunch of initializition steps are here ...
     4  STARTDATE=`date +%Y%m%d`
     5  echo "STARTDATE = $STARTDATE"
     6
     7  NOWDATE=`date +%Y%m%d`
     8  if [ $NOWDATE -gt $STARTDATE ]; then
     9    echo "in if (-gt)  : NOWDATE = $NOWDATE"
    10  else
    11    echo "in else (-le): NOWDATE = $NOWDATE"
    12  fi
    13
    14  NOWDATE="20091108"
    15  if [ $NOWDATE -gt $STARTDATE ]; then
    16    echo "in if (-gt)  : NOWDATE = $NOWDATE"
    17  else
    18    echo "in else (-le): NOWDATE = $NOWDATE"
    19  fi
    20
$
$ . t1.sh
STARTDATE = 20091107
in else (-le): NOWDATE = 20091107
in if (-gt)  : NOWDATE = 20091108
$
$

tyler_durden