How to declare float in shellscript?

Hi All,

I am having an abc.txt , which contains some digits

 
Eg:abc.txt

14.5
56.6
95.5
 

I want write shellscript in suchway that if any digit is greter than 90 then it shuld display " digit is greater than 90"

Please help me to do so .I have tried below code , for integer the below code is working fine . if the abc.txt file contain float values then its not workin properly.

 
#! /bin/bash
b=90;
for i in `cat abc.txt`
do
if [ $i -gt $b ] ; then
echo " value is greater than $b "
exit
fi
done

 

please help me for the float values

Thanks

 
$ nawk '{if($0>90){print $0" is greater than 90"}else{print $0" is less than 90"}}' test
14.5 is less than 90
56.6 is less than 90
95.5 is greater than 90

hi itkamraj,

Tried the below code

 
nawk '{if($0>90){print $0" is greater than 90"}else{print $0" is less than 90"}}' abc.txt

getting below error

 
-bash: nawk: command not found

I think nawk is not working on my server

 
$ perl -lane 'print $_ if($_ > 90)' test
95.5

---------- Post updated at 12:47 PM ---------- Previous update was at 12:46 PM ----------

use awk instead of nawk

Hi,

Try this code,

 
 
if [ $(echo "$i > $b"|bc) -eq 1 ]
 
instead of below code
 
if [ $i -gt $b ] ; then
 

Cheers,
Ranga:)

1 Like

If you really want to do the test from within a shell script without the need to call an external utility such as awk or perl, you need to use a shell that supports floating point i.e. ksh93 or zsh. Bash is quite a rudimentary shell. Among other limitations, it does not support floating point.

#!/bin/ksh

b=90

while read i
do
   if (( i > b ))
   then
      echo " value is greater than $b "
      exit
   fi
done < abc.txt