SH if statement using FLOAT values | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

SH if statement using FLOAT values

Shell Programming and Scripting


Tags
bc, bourne, float, if statement, sh

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-14-2009
sjepsen sjepsen is offline
Registered User
 
Join Date: Mar 2009
Last Activity: 14 March 2009, 9:30 PM EDT
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Java SH if statement using FLOAT values

Today I spent longer than I'd like to admit figuring out how to write a Bourne shell IF statement that tests a FLOAT value before executing a block of statements. Here's the solution I found, which invokes bc. Hope this will come in handy for someone:

value = [some floating point number]
testval = [some floating point number]

if [ `echo "if($value > $testval) 1; if($value <= $testval) 0" | bc` -eq 1 ]
then
body of if statement
fi


--Steve
Sponsored Links
    #2  
Old 03-15-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 30 October 2014, 6:24 PM EDT
Location: Foxborough, MA
Posts: 7,667
Thanks: 152
Thanked 586 Times in 551 Posts

Code:
#!/bin/ksh

a=1.72
b=1.71

if [ "$(echo "if (${a} > ${b}) 1" | bc)" -eq 1 ] ; then
   echo ">"
else
   echo "<"
fi;

Sponsored Links
    #3  
Old 03-15-2009
ripat ripat is offline Forum Advisor  
Registered User
 
Join Date: Oct 2006
Last Activity: 17 February 2014, 7:51 AM EST
Location: Belgium
Posts: 537
Thanks: 4
Thanked 38 Times in 28 Posts
I would have thought that all version of ksh support floating point calculation/comparison.

Mine does:


Code:
a=1.699999
b=1.7
if [[ $a < $b ]];then echo $a smaller than $b;fi

ksh version M 1993-12-28 s+
    #4  
Old 03-15-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 30 October 2014, 6:24 PM EDT
Location: Foxborough, MA
Posts: 7,667
Thanks: 152
Thanked 586 Times in 551 Posts
Quote:
Originally Posted by ripat View Post
I would have thought that all version of ksh support floating point calculation/comparison.

Mine does:


Code:
a=1.699999
b=1.7
if [[ $a < $b ]];then echo $a smaller than $b;fi

ksh version M 1993-12-28 s+
don't know about k93, but k88 does not.
The OP is actually asking about Bourne - the above should work Bourne as well.
Sponsored Links
    #5  
Old 03-15-2009
danmero danmero is offline Forum Advisor  
 
Join Date: Nov 2007
Last Activity: 12 October 2014, 10:11 AM EDT
Location: H3X
Posts: 2,151
Thanks: 10
Thanked 117 Times in 110 Posts
Quote:
Originally Posted by vgersh99 View Post
The OP is actually asking about Bourne - the above should work Bourne as well.
Not my Bourne, however work on Bash.
Sponsored Links
    #6  
Old 03-15-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 30 October 2014, 6:24 PM EDT
Location: Foxborough, MA
Posts: 7,667
Thanks: 152
Thanked 586 Times in 551 Posts
OK, this version should work under Bourne - sorry 'bout that:

Code:
#!/bin/sh

a=1.72
b=1.71

if [ "`echo \"if (${a} > ${b}) 1\" | bc`" -eq 1 ] ; then
   echo ">"
else
   echo "<"
fi;

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Iterative statement to cut values from a line mannepalli Shell Programming and Scripting 4 02-13-2009 10:33 AM
math.h: float ceilf(float x) jonas.gabriel Programming 1 06-14-2007 03:20 AM
Dividing float values shash UNIX for Dummies Questions & Answers 3 01-17-2007 04:37 AM
comparing two float values kavitha Shell Programming and Scripting 2 04-03-2004 12:32 AM
Code checking for all values in the same if statement. oracle8 Shell Programming and Scripting 4 01-23-2004 08:30 PM



All times are GMT -4. The time now is 08:32 AM.