LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-08-2010, 11:42 AM   #1
atavus
LQ Newbie
 
Registered: Feb 2010
Posts: 6

Rep: Reputation: 0
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
 
Old 02-08-2010, 12:11 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

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

Last edited by jschiwal; 02-08-2010 at 12:19 PM.
 
Old 02-08-2010, 12:16 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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
 
Old 02-08-2010, 12:21 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by jschiwal View Post
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.
 
Old 02-08-2010, 12:49 PM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.
 
Old 02-08-2010, 02:15 PM   #6
atavus
LQ Newbie
 
Registered: Feb 2010
Posts: 6

Original Poster
Rep: Reputation: 0
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
 
Old 02-08-2010, 02:28 PM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by atavus View Post
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.
 
  


Reply


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
Webmin vs Plesk vs GoDaddy Simple Panel at Fedora (comparison) Libertes Fedora 1 01-27-2014 12:13 AM
Bash file comparison operator question gizmola Programming 5 12-23-2009 08:49 AM
Simple Scripting Problem [Comparison of several strings] thomasknowles Programming 7 07-22-2008 06:53 AM
operator= doesn't work with a template class The_Nerd Programming 1 05-22-2006 02:54 PM
Simple Operator Precedence C++ Problem rovitotv Programming 9 03-21-2005 10:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:46 AM.

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