UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 10th, 2003, 08:43 PM
lightation lightation is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 2 lightation User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy Comparing decimal numbers in Bash Shell

Hi.

I am a shell script newbie and having trouble with comparing decimal numbers using Bash shell scripts.

I tried below but I get syntax error:

if [ $var_containing_decimal_no -lt 5 ]
then
...
else
...
fi

It seems that '-lt' operator only works with integer value.

Can anyone help me ?

Reply With Quote
  #2  
Old July 29th, 2005, 07:29 PM
HellsRaison HellsRaison is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 1 HellsRaison User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 26 m 13 sec
Reputation Power: 0
Hey! I can help you!
Shell scripting float point to int to integer floating accuracy floating arithmetic bash scripting UNIX scripting UNIX scripts bash script shell script float compare float expressions float expression shell float decimal double round float

Code:
#!/bin/bash


ROUND_OFF=100000000   # This is how much accuracy we have 
                      # (to the nearest 10 millionth for this example)
                      
DECIMAL=3.3333333333  # This is our decimal value
COMPARE_TO=5          # This is the value we want to compare the decimal value to



#
# CONVERT OUR BIG DECIMAL INTO A BIG INTEGER FOR COMPARING
#

# bc -l does the float calculation
# awk cuts off the decimal part
int=`echo $DECIMAL '*10000000' | bc -l | awk -F '.' '{ print $1; exit; }'`





#
# IF COMPARE_TO IS DECIMAL TOO, CONVERT IT HERE JUST LIKE ABOVE
# IT'S NOT IN THIS CASE SO LEAVE THE BOTTOM COMMENT BY ITSELF FOR NOW
#

#compare_int=`echo $COMPARE_TO '*10000000' | bc -l | awk -F '.' '{ print $1; exit; }'`
compare_int=`expr $COMPARE_TO '*' $ROUND_OFF`  # Since COMPARE_TO is not a float, we'll just do this than above





#
# NOW COMPARE THE NUMBERS
#
if [ $compare_int -gt $int ] ; then
  echo $COMPARE_TO " > " $DECIMAL
elif [ $compare_int -lt $int ] ; then
  echo $COMPARE_TO " > " $DECIMAL
else
  echo $COMPARE_TO " == " $DECIMAL
fi




#
# OUTPUT:
#
#|psycho@Shiva|: ./decimal.sh 
#5  >  3.3333333333

Reply With Quote
  #3  
Old July 29th, 2005, 07:51 PM
christo's Avatar
christo christo is offline
Introspective
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Nov 2001
Location: London, UK
Posts: 3,297 christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 1 h 10 m 50 sec
Reputation Power: 104
Send a message via ICQ to christo Send a message via Yahoo to christo
man, that question was posted on March 11th, 2003 - that's over 2 years ago!!


christo

Reply With Quote
  #4  
Old July 30th, 2005, 09:32 AM
iribach iribach is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 23 iribach User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 31 m 47 sec
Reputation Power: 0
it could be simpler

yes, bc is a very good way to do that, also LET IT SIMPLY DO the job !

this way you can compare every things: digit <> float <> hex <> oct

have a look: set a to A and b to 10

Code:
        #!/bin/sh
        case `echo "a=$a;b=$b;r=-1;if(a==b)r=0;if(a>b)r=1;r"|bc` in
           0) echo a=$a and b=$b are equal
        ;; 1) echo a=$a is bigger then b=$b
        ;; *) echo a=$a is less then b=$b
        ;; esac

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Comparing decimal numbers in Bash Shell


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT