954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to compare float values in shell script

I want to compare two float values in shell script,

i.e. a = 0.1
b = 0.4

if ( $a < $b )then
expression1..
expression2..
expression3..
fi

if ( $a > 0.0 ) then
expression4..
expression5..
fi
could you please suggest me how to implement in shell script ?

shyam miyatra
Newbie Poster
5 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

Fianly i got solution from google here is it.

#! /usr/bin/ksh


isfirstgreater(){
LHS=$1
RHS=$2

min=`(echo $LHS ; echo $RHS) | sort -n | head -1`
if [ "$min" = "$LHS" ]; then
return 1
else
return 0
fi
}

isfirstless(){
LHS=$1
RHS=$2

min=`(echo $LHS ; echo $RHS) | sort -n | head -1`
if [ "$min" = "$LHS" ]; then
return 0
else
return 1
fi
}
checkval(){
FUNC=$1
ARG1=$2
ARG2=$3

echo -n "$FUNC $ARG1 $ARG2 ... "
$FUNC $ARG1 $ARG2

if [ $? -eq 0 ]; then
echo TRUE
else
echo FALSE
fi
}

checkval isfirstgreater 0.1 0.2
checkval isfirstless 0.1 0.2
checkval isfirstgreater 0.9993 0.9996

I want to compare two float values in shell script,

i.e. a = 0.1 b = 0.4

if ( $a < $b )then expression1.. expression2.. expression3.. fi

if ( $a > 0.0 ) then expression4.. expression5.. fi could you please suggest me how to implement in shell script ?

shyam miyatra
Newbie Poster
5 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You