LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 06-29-2008, 02:41 AM   #1
jadeddog
LQ Newbie
 
Registered: Jun 2008
Posts: 26

Rep: Reputation: 15
problem with expr function


i have the following function that should figure out a percentage (you call the function with 5 parameters obviously):

function countresponses()
{
totalresponses=`expr $1 + $2 + $3 + $4 + $5`
badresponses=`expr $4 + $5`
percentbad=`expr $badresponses / $totalresponses * 100`
echo "Percent bad is: $percentbad"
}

but i keep getting an error:
expr: syntax error
Percent bad is:

ive tried a bunch of different ways to get a percentage, but i keep getting expr syntax errors of one type or another... i don't think this should be very hard, but its proving to be... any help would be appreciated
 
Old 06-29-2008, 03:08 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
why not use bc?
Code:
echo "$1 + $2 + $3 + $4 + $5" | bc
 
Old 06-29-2008, 03:18 AM   #3
jadeddog
LQ Newbie
 
Registered: Jun 2008
Posts: 26

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ghostdog74 View Post
why not use bc?
Code:
echo "$1 + $2 + $3 + $4 + $5" | bc
you'll have to forgive my ignorance, but i'm not aware of the bc command... could you give an example please?
 
Old 06-29-2008, 03:19 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
The syntax error is due to the multiplication symbol in expr: it should be \* not simply *, otherwise it is expanded by the shell as the wildcard character. Anyway, follow the suggestion of ghostdog74 and use bc instead. This will give you more accurate results, since in this way you can manage floating point values. You can also establish the number of decimal digits by setting the parameter scale. For example:
Code:
percentbad=$(echo "scale=3; $badresponses / $totalresponses * 100" | bc)
 
Old 06-29-2008, 03:40 AM   #5
jadeddog
LQ Newbie
 
Registered: Jun 2008
Posts: 26

Original Poster
Rep: Reputation: 15
thank you for your help, it is now working good.... that bc command is a 1000 times better than expr!! much appreciated
 
Old 06-29-2008, 03:40 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by jadeddog View Post
you'll have to forgive my ignorance, but i'm not aware of the bc command... could you give an example please?
Apart the examples me and ghostdog suggested, you will find plenty of tutorials about bc by googling around. You can find an explanation with some examples on the Advanced Bash Scripting Guide, here. Also you can look at
Code:
man bc
Another point, regarding the expr issue: in BASH you can use the ((...)) syntax, that is arithmetic expansion. It has a more intuitive syntax than expr (it permits the multiplication with * for example) and permits C style operations like increments +=. However, to manage floating point values you have to stick with bc or some other external commands that can manage them (awk, perl, python...).
 
Old 06-29-2008, 03:55 AM   #7
jadeddog
LQ Newbie
 
Registered: Jun 2008
Posts: 26

Original Poster
Rep: Reputation: 15
ok, now that ive got the percentage as 15.00 (i used scale=2), im trying to do an if statement, but am getting an "expecting integer" error from the if statment... how can i change the bc result to an integer so i can use it in an if statement?
 
Old 06-29-2008, 04:35 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
you can't really compare floating values in bash. show how you do your if statement, in fact show what you have.
 
Old 06-29-2008, 04:53 AM   #9
jadeddog
LQ Newbie
 
Registered: Jun 2008
Posts: 26

Original Poster
Rep: Reputation: 15
here is my full function as of right now:

function countresponses()
{
totalresponses=`expr $1 + $2 + $3 + $4 + $5`
badresponses=`expr $4 + $5`
percentbad=$(echo "scale=2; $badresponses / $totalresponses * 100" | bc)

if [ $percentbad -gt 5 ];
then
echo "There is a problem with the modified filter, restoring old filter configuration."
# need to rebuild teh configuration here and test once more
# taillog
else
echo "The new configuration is working properly."
fi
}

and here is the error im getting:
./spamfilter: line 122: [: 15.00: integer expression expected
The new configuration is working properly.

it shouldnt get the to the else part, since 15 > 5 obviously
 
Old 06-29-2008, 06:15 AM   #10
jadeddog
LQ Newbie
 
Registered: Jun 2008
Posts: 26

Original Poster
Rep: Reputation: 15
i found a way to convert the float to an int and now everything works properly... thanks for all the help
 
Old 06-29-2008, 08:44 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Please post your soln so we can all learn
 
  


Reply



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
How to multiply with expr? ( expr 3 * 4 doesn't work, though expr 3 + 4 does) lumix Linux - General 5 02-25-2009 05:31 PM
expr question sporty Linux - Newbie 2 03-22-2006 04:15 PM
Expr in Shell mystical_sun2000 Linux - Newbie 1 02-08-2006 06:59 AM
About how to use expr naihe2010 Programming 2 10-31-2005 06:28 AM
simple expr izza_azhar Programming 3 01-17-2005 07:38 PM

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

All times are GMT -5. The time now is 04:17 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