0

this is the code i am making the calculations are in float i want to now compare this answer to the following if and else command but every time i get illegal number error.

#!/bin/sh

echo "\t\t\t\t CALCULATION OF BMI ";
echo "Enter weight in kilograms(kg)";
read weight;
echo "Enter height in meters(m)";
read height;
echo "Your weight is "$weight" kg";
echo "Your height is "$height" meters";
BMI=`expr "scale=2;($weight/($height*$height))" |bc`;
echo "Your BMI is "$BMI" kg/m2";
if   [ "$BMI" -ge 15 -a "$BMI" -le 16 ]
then
echo "Severely underweight";
elif [ "$BMI" -ge 16 -a "$BMI" -le 18.5 ]
then
echo "Underweight";
elif [ "$BMI" -ge 18.5 -a "$BMI" -le 25 ]
then
echo "Normal Range (healthy)";
elif [ "$BMI" -ge 25 -a "$BMI" -le 30 ]
then
echo "Overweight";
elif [ "$BMI" -ge 30 -a "$BMI" -le 35 ]
then`enter code here`
echo "Obese class I – Moderately obese";
elif [ "$BMI" -ge 35 -a "$BMI" -le 40 ]
then
echo "Obese class II – Severely obese";
fi

error messages

John1024
  • 109,961
  • 14
  • 137
  • 171
  • Bash simply does not do floating point numbers. There are many good ways to accomplish in bash, though, using tools such as `expr`, `bc`, or `awk`. See the linked question. – John1024 May 19 '20 at 23:35
  • Here is another question that also addresses the issue of floating point in shell: [Floating point comparison in shell](https://stackoverflow.com/questions/2424770/floating-point-comparison-in-shell). – John1024 May 19 '20 at 23:36
  • Here is yet another question addressing the same issue: [How do I use floating-point division in bash?](https://stackoverflow.com/questions/12722095/how-do-i-use-floating-point-division-in-bash). – John1024 May 19 '20 at 23:38
  • If you really want to do floating point _directly_ arithmetic in a shell and you don't mind non-POSIX code, use [zsh, ksh, or fish](https://hyperpolyglot.org/unix-shells). – John1024 May 19 '20 at 23:42

0 Answers0