The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Unix Arithmatic operation issue , datatype issue thambi Shell Programming and Scripting 23 02-19-2008 07:19 AM
Strange issue AndrewSH UNIX for Dummies Questions & Answers 1 01-29-2008 05:33 PM
root Password ... strange issue badrali Security 6 06-28-2007 09:41 AM
random number logic -- issue asutoshch High Level Programming 1 05-09-2003 11:07 AM
comparison cnf Filesystems, Disks and Memory 2 05-14-2002 01:52 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-09-2007
shihabvk shihabvk is offline
Registered User
  
 

Join Date: May 2005
Posts: 54
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
  #2 (permalink)  
Old 04-09-2007
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,209
The numbers are too large for a -gt or -lt type comparison.

Try it with a numerical comparison:

Code:
if (( STARTTIME > DATE_TIME )) ; then
...
fi
or
Code:
if (( $STARTTIME > $DATE_TIME )) ; then
...
fi

*note in numerical comparison the "$" on the variables can be either ommitted or used
  #3 (permalink)  
Old 04-09-2007
shihabvk shihabvk is offline
Registered User
  
 

Join Date: May 2005
Posts: 54
Sorry it is not working :-(
  #4 (permalink)  
Old 04-09-2007
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,209
Quote:
Originally Posted by shihabvk
Sorry it is not working :-(
**shrug** It works fine for me, but since you gave no indication of any behaviour or error there is noting I can do.
  #5 (permalink)  
Old 04-09-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
Quote:
Originally Posted by reborg
**shrug** It works fine for me, but since you gave no indication of any behaviour or error there is noting I can do.
Yes,

As pointed, it would work,

here is a sample that I tried in zsh

Code:
if [ 20040109195224 -lt 20070409200951 ]
then
echo "yes"
else
echo "no"
fi
yes
Should work in ksh too !
  #6 (permalink)  
Old 04-09-2007
shihabvk shihabvk is offline
Registered User
  
 

Join Date: May 2005
Posts: 54
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
  #7 (permalink)  
Old 04-09-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
Code:
perl -e 'if ( 20040109195224 < 20070409200951 ) { print "yes\n" }'
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:45 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0