Strange Number comparison issue

Hi,
I am comparing two numbers, but it gives strange results:
My Code:
if [ ${STARTTIME} -gt ${DATE_TIME} ]
then
echo "True"
else
echo "False"
fi

This code gives False for the follwoing comparison
[ 20040109195224 -gt 20070409200951 ]

where as True for the following:
[ 20050109195224 -gt 20070409201317 ]

Any reason for this? Both Should have given False...

I am using ksh shell on HP UX

Please help me
Shihab

The numbers are too large for a -gt or -lt type comparison.

Try it with a numerical comparison:

if (( STARTTIME > DATE_TIME )) ; then
...
fi

or

if (( $STARTTIME > $DATE_TIME )) ; then
...
fi

*note in numerical comparison the "$" on the variables can be either ommitted or used

Sorry it is not working :frowning:

**shrug** It works fine for me, but since you gave no indication of any behaviour or error there is noting I can do.

perl -e 'if ( 20040109195224 < 20070409200951 ) { print "yes\n" }'

Yes,

As pointed, it would work,

here is a sample that I tried in zsh

if [ 20040109195224 -lt 20070409200951 ]
then
echo "yes"
else
echo "no"
fi
yes

Should work in ksh too ! :slight_smile:

See My code :
#!/bin/ksh
STARTTIME=20050109195224
DATE_TIME=20070409201317
if (( STARTTIME > DATE_TIME )) ; then
echo "No"
else
echo "Yes"
fi

STARTTIME=20060109195224
DATE_TIME=20070409201317
if (( STARTTIME > DATE_TIME )) ; then
echo "No"
else
echo "Yes"
fi

Gives No for first if and Yes for the second one

As I mentioned using KSH on HP-UX

ksh on HP-UX uses 32 bit arithmetic and the largest integer it can handle is 2147483648. bc can perform arithmetic on any size number. If a is less than b, then the expression a-b will be negative. Negative numbers will have a minus sign as the first character.

c=$(echo $a - $b | bc)
if [[ $c = -* ]] ; then

In your case, both numbers have the same number of digits. If that is guaranteed to be true, an alpha compare will work:
if [[ $a < $b ]] ; then

Hi Perderabo

I tried this and is working fine.. thanks a lot
Between what is this alpha comparison? is it just the string comparison?

Thanks
Shihab

Yes it is. And it should work for you as the string will always be YYYYMMDDHHMMSS.