LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Simple problem getting a comparison operator to work (https://www.linuxquestions.org/questions/programming-9/simple-problem-getting-a-comparison-operator-to-work-787810/)

atavus 02-08-2010 11:42 AM

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
a=800
b=700
c=.15

if [ "$a" -le "$(echo "($b*$c+$b)"|bc)" ]
        then
                echo "yay"
        else
                echo "nay"
fi


jschiwal 02-08-2010 12:11 PM

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.

catkin 02-08-2010 12:16 PM

Problem is bc returns a number with a fractional part
Code:

c:~$ a=800
c:~$ b=700
c:~$ c=.15
c:~$ echo "($b*$c+$b)"
(700*.15+700)
c:~$ echo "($b*$c+$b)"|bc
805.00
c:~$ if [ "$a" -le "$(echo "($b*$c+$b)"|bc)" ]
> then
> echo Y
> else
> echo N
> fi
bash: [: 805.00: integer expression expected
N

One solution is to use bc to do the comparison
Code:

echo "($b*$c+$b) >= $a" | bc

catkin 02-08-2010 12:21 PM

Quote:

Originally Posted by jschiwal (Post 3856873)
Use "scale=0;" in the string you pass to bc. Test it outside the test to make sure you get an integer result.

That was my first idea too but testing it didn't work
Code:

c:~$ echo "scale=0; ($b*$c+$b)"|bc
805.00

Netsearching also revealed that the scale value is used for intermediate calculations as well as the result so (if scale did work!) it would round the value of c (0.15), presumably down to 0, with unintended effect.

jschiwal 02-08-2010 12:49 PM

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
0
echo "($b*$c+$b) >= $a" | bc
1

To test for equality, you need to compare the difference to a small value. This allows for rounding errors.

atavus 02-08-2010 02:15 PM

Thanks for the help with my problem, I got it working with the following solution.

Code:

#! /bin/bash
a=800
b=700
c=.15

if [ $(echo "($b*$c+$b) <= $a" | bc) -eq 0 ]
        then
                echo "yay"
        else
                echo "nay"
fi


catkin 02-08-2010 02:28 PM

Quote:

Originally Posted by atavus (Post 3857030)
Thanks for the help with my problem, I got it working with the following solution.

Glad you found a solution. Please mark the thread solved using the Thread Tools menu.


All times are GMT -5. The time now is 01:11 PM.