0

For a school assignment I need to make a BMI calculator. But it needs to work with decimals. So do any of you know how I make it, so I can type in a decimal on line 4 and 5 and get a decimal result on line 13? Am new to Linux BASH scripting, so all the help I can get will be very appreciated.

1.  #!/bin/bash
2.  clear
3.
4.  read -p "Weight in kilogram: " kg
5.  read -p "Lenght in meter: " m
6.  clear
7.
8.  let total_weight=$kg
9.  let total_lenght=$m*$m
10. let BMI=$total_weight/$total_lenght
11. clear
12.
13. echo "Ur BMI is: $BMI"
  • 1
    Consider using a scripting language that has built-in typing, `bash` can't do floats on its own. – Panki Oct 21 '21 at 14:38
  • 3
    `bash` only does integer arithmetic. Read `man bc` and do something like `bmi=$(echo "scale=2;$total_weight / $total_length" | bc)`. Hints: Don't use all upper case variable names (they're used for communicating with programs, "`BMI`" isn't bad by itself, but upper case variable names will bite you eventually. Check your spelling.Always paste your script into `https://shellcheck.net`, a syntax checker, or install `shellcheck` locally. Make using `shellcheck` part of your development process. – waltinator Oct 21 '21 at 14:45
  • 1
    If you *have* to use Bash for this assignment, work in different (smaller, integer) units. 2 metres is 2000 millimetres, 1.7 metres is 1700 mm, 1.76 metres is 1760 mm. Same for kg to grams. Fiddly, but Bash can manage those strings using built-in substitutions. Use similar scaling for the result, and remember squaring mm needs to shift the (invisible) decimal point. – Paul_Pedant Oct 22 '21 at 08:13
  • @waltinator Thank you so much for your reply. Finally found it. Thank you to everyone that tried to help me also – laboonmaster Oct 25 '21 at 18:41

0 Answers0