Simple problem getting a comparison operator to work
I'm very new at Bash scripting and have a bone head issue that I'm trying (and failing) to resolve. I cannot get this one IF statement to work, it seems the comparison operator does not think the resulting number from the $b*$c+$b operation is an integer even though it is a number. Below is a small proof of concept script with the bit I'm having trouble with.
Code:
#! /bin/bash |
First use [[ ... ]] for arithametic tests. Actually, just use [[ .. ]] for all tests of values.
Bash variables are integer variables. Comparing the value of an integer variable with "805.00" will not work. Use "scale=0;" in the string you pass to bc. Test it outside the test to make sure you get an integer result. Consider moving the comparison inside the statements you pass to bc. You can even have the test and result printed by bc. |
Problem is bc returns a number with a fractional part
Code:
c:~$ a=800 Code:
echo "($b*$c+$b) >= $a" | bc |
Quote:
Code:
c:~$ echo "scale=0; ($b*$c+$b)"|bc |
Good point. Have a look what you can do with bc. I moved everything inside the string passed to bc:
echo 'scale=0;a=800;b=700;c=.15;print "\na=",a,"\nb=",b,"\nc=",c,"\n";if (a < b*(c+1)) print "true\n" else print "false\n"' | bc Move the comparison in bc since the results are floating point numbers. Code:
echo "($b*$c+$b) <= $a" | bc |
Thanks for the help with my problem, I got it working with the following solution.
Code:
#! /bin/bash |
Quote:
|
All times are GMT -5. The time now is 01:11 PM. |