How to compare decimal values in bash?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to compare decimal values in bash?
# 1  
Old 11-14-2013
How to compare decimal values in bash?

Hi,

I am facing some error while doing the comparision between 2 decimal values in bash. Pl help me out.

I have tried using below scripts. But its giving me error.

1)
Code:
amt=12.3 opn_amt=12.5 var=$(awk 'BEGIN{ print "'$amt'"<"'$opn_amt'" }')    

if [ "$var" -eq 1 ];then   echo "correct" else   echo "Wrong" fi

2)Tried using bc also as follows , but no resolution.

Code:
amt="12.3" opn_amt="12.2" if [ $(bc <<< "$amt <= $opn_amt") -eq 1 ]     then  
 echo "correct" else   echo "Wrong" fi

Thanks
Sibadatta

Moderator's Comments:
Mod Comment This thread was moved to the proper forum

Last edited by Scrutinizer; 11-14-2013 at 02:02 AM.. Reason: code tags
# 2  
Old 11-14-2013
A number of semicolons were missing, try:
Code:
amt="12.3" opn_amt="12.4"; if [ $(bc <<< "$amt <= $opn_amt") -eq 1 ]; then echo "correct"; else echo "Wrong" ; fi

You can avoid this and improve readability by using more lines:
Code:
amt="12.3" opn_amt="12.2"
if [ $(bc <<< "$amt <= $opn_amt") -eq 1 ]; then
  echo "correct"
else
  echo "Wrong" 
fi

# 3  
Old 11-14-2013
Just another way

Code:
if [  $(echo $amt - $opn_amt | bc | grep ^-) ]; then
  echo "correct"
else
  echo "Wrong" 
fi

# 4  
Old 11-14-2013
It should be noted that all of these examples are using the external bc utility, because BASH itself -- and most bourne shells except ksh -- do not support floating-point numbers.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare two arrays by values [BASH]

I have 2 arrays of values for example A1 ={10 15 3 21} A2 ={10 15 3 22} I need to check which one has greater values. However: A1 ={10 15 3 21} A2 ={10 15 3 21 3} - this one would be greater. A1 ={10 15 5 21} - this one greater A2 ={10 15 3 21} Basically, I want to compare patch... (6 Replies)
Discussion started by: Jutsimitsu
6 Replies

2. Shell Programming and Scripting

How compare decimal values?

I have 2 files say tp1.txt and tp2.txt having following data cat tp1.txt abc,2.20,IN20 acb,3.15,DN10 bca,3,RD10 cat tp2.txt alv,1.00,IN20 aaa,4.05,DD10 abb,5.50,RD12 i want to compare the values on 2nd field of both the file, if value of first tp1.txt is greater than value... (3 Replies)
Discussion started by: ranabhavish
3 Replies

3. Shell Programming and Scripting

bin bash decimal compare

I need decimal comparing with if. Check if apache version is less than 2.2.17. I tried this and not working. #!/bin/bash apachever=`/usr/local/apache/bin/httpd -v | head -1 | awk '{print $3}' |cut -d/ -f 2` if ]; then echo "Apache version less than 2.2.17" else ... (7 Replies)
Discussion started by: anil510
7 Replies

4. UNIX for Dummies Questions & Answers

compare decimal and integer values in if in bash shell

i need to do camparisions like the below. For the case when first=10 and second=9.9 the scripts displays process failed. I need to be able to convert the values to integer before doing the comparision. Like 9.9 should be rounded over to 10 before doing comparision. Please advice how can... (3 Replies)
Discussion started by: nehagupta
3 Replies

5. Shell Programming and Scripting

How to get decimal values ?

Hi All, In my script I've written like this- c=$( expr 100 / 3);echo $c The output coming is 33. but I want to see 33.33, decimal values too. How to get that? Thanks, Naresh (3 Replies)
Discussion started by: NARESH1302
3 Replies

6. UNIX for Advanced & Expert Users

Converting Binary decimal coded values to Ascii Values

Hi All, Is there any command which can convert binary decimal coded values to ascii values... i have bcd values like below оооооооооооо0о-- -v - Pls suggest a way to convert this. Thanks, Deepti.Gaur (3 Replies)
Discussion started by: gaur.deepti
3 Replies

7. Shell Programming and Scripting

How to sort decimal values in bash

Hi, I have a list of values from associative array from 0,..till 1.0000. I tried various sort options; sort -g, sort -nr but it still couldnt work. In other words, the numbers are not sorted accordingly. Please help. Thanks. (1 Reply)
Discussion started by: ahjiefreak
1 Replies

8. Shell Programming and Scripting

Compare integer value with decimal

Hi all, I have a issue... Is it possible to compare integer value with decimal value. If it is not possible,then how can i compare 2 decimal values in born shell.thanks! (3 Replies)
Discussion started by: MARY76
3 Replies

9. Shell Programming and Scripting

compare decimal numbers

Hi anyone, i need to compare two decimal numbers i thought that it could be do it with if but... :( So, i'm writing in csh and i really apreciate if anyone can help me if ( $ppl_kn <= $ppl_wb ) then echo "############# KNdiscount model has the lowest perplexity" set... (5 Replies)
Discussion started by: tmxps
5 Replies
Login or Register to Ask a Question