Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 04-06-2009, 08:25 PM   #1
Registered: Apr 2003
Posts: 152

Rep: Reputation: 15
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.

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"

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"
echo 'Please restart, it must be one or the other.'


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.
Old 04-06-2009, 09:03 PM   #2
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
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:
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,

Last edited by Robhogg; 04-06-2009 at 09:08 PM.
Old 04-06-2009, 09:09 PM   #3
Registered: Apr 2003
Posts: 152

Original Poster
Rep: Reputation: 15
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Iterate over SQL results in bash script. Is there a better way to do this? word_virus Programming 4 11-09-2008 10:15 AM
php referrer script to block people not coming from my domain steve51184 Linux - Server 6 10-11-2008 10:30 AM
Expect Scripting:- script not coming out of telnet session. nik1984 Programming 2 09-11-2008 08:14 AM
emailing script results from someone else's pc epoo Linux - General 8 04-26-2007 10:04 AM
Script Problem - Does not give expected results Jose Muņiz Linux - General 3 07-12-2003 10:10 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:39 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration