ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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:
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...).
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?
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.