LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Script results keep coming out to Zero (https://www.linuxquestions.org/questions/linux-newbie-8/script-results-keep-coming-out-to-zero-717302/)

acidblue 04-06-2009 08:25 PM

Script results keep coming out to Zero
 
Here is my Bash script, it's a formula for adding fertilizers to a solution.
But it keep giving me '0 ' as a result.

#!/bin/bash
echo 'How many gallons are in your res? enter a 2 digit number'
read res_gallons
echo 'Enter your current PPM/EC level'
read current_ppm
echo 'Now enter your desired target PPM/EC level'
read target_ppm
echo $target_ppm
###(target ec/ppm - current ec/ppm) / target ec/ppm) * 8 ml per gallon * res gallons = Flora Micro (ml). Double this amount of Flora Bloom (ml)###
###(target ec/ppm - current ec/ppm) / target ec/ppm) * 8 ml per gallon * res gallons = Flora Nova Bloom (ml)###
### TWo Formula's for GH 3 part and Flora Nova 2 part.###
echo 'Is this for GH 3 part? yes/no'
read GH_answer

if [ "$GH_answer" == "yes" ] ; then
let GH_result=$(( (($target_ppm - $current_ppm)/$target_ppm) *8*$res_gallons ))
let Micro=$(( ($GH_result*2 ) ))
echo "GH Bloom is $GH_result"
echo "Micro is $Micro"
else

echo 'Is this for Flora Nova 2 part? yes/no'
read FN_answer
if [ "$FN_answer" == "yes" ] ; then
FN_result=$(( (($target_ppm - $current_ppm)/$target_ppm) *8*$res_gallons ))
echo "Then add $FN_result ml of Flora Nova Bloom"
else
echo 'Please restart, it must be one or the other.'

fi
fi

I'm terrible at debugging, i can't figure out why it gives 0.
I've rewritten this a dozen times, if someone could point out where i went wrong, that would be great.

Robhogg 04-06-2009 09:03 PM

What range were you expecting the answer to be in? In the shell, all you get is integer maths, so anything less than 1 is rounded down to 0.

There is bc, which is an arbitrary precision calculator. A little fiddly to use, but something like this would work:
Code:

FN_result=$( echo "scale=5; ($target_ppm - $current_ppm) / $target_ppm \
 * 8 * $res_gallons "| bc -l)

Here, the echoed string states the required precision (5 d.p.), and then the calculation. bc can do pretty complex things - the man page is quite comprehensive, and has lots of examples.

However, if you are going to be doing a lot of this sort of thing, I would recommend learning a bit of something like Perl or (shudder!) Python.

Hope this helps,
Rob

acidblue 04-06-2009 09:09 PM

Aha!
Yes just before you posted i realized the '/' was truncating my result.
Yes python would be better, but it's been so long since i've done anything Pyhton.
i figured i'd give bash a shot first, but now you got me thinking "Python", I never learned perl.
Thanks, you been a great help


All times are GMT -5. The time now is 03:24 PM.