5

I've got some issues on scripting... if someone could help me, it would be really good !

My script has:

VISITS=$((WR + RD));
SERVICE_DEMAND=$((VISITS*SERVICE_DEMAND));

And I'm getting this error:

./calc_serv_demand.sh: line 12: 0.0895406: syntax error: invalid arithmetic operator (error token is ".0895406")

Can someone help me?

I think it's because the bash works only with integer... I need to use float values, though.

thanks in advance


Problem solved:

VISITS=$(echo $WR + $RD | bc); echo $VISITS

SERVICE_DEMAND=$(echo $VISITS '*' $SERVICE_TIME | bc); echo $SERVICE_DEMAND

5 Answers 5

5

You can use bc to do your floating point calculations, i.e.

echo $WR + $RD | bc

and so on.

Sign up to request clarification or add additional context in comments.

4 Comments

HUm... it works for the sum, it doesn't work well for the multiplication. It's weird because I've checked on bc manual and it should work ! Have a look: My script: echo $WR + $RD | bc &> VISITS; VISITS=$(echo $WR + $RD | bc); # works fine echo $VISITS SERVICE_DEMAND=$(echo $VISITS * $SERVICE_TIME | bc); # return a weird error echo $SERVICE_DEMAND Error: 0.0895406 3.42007 12.401 3.5096106 (standard_in) 1: syntax error (standard_in) 1: illegal character: K (standard_in) 1: illegal character: H (standard_in) 1: illegal character: T ...
@user: Since * has a special meaning for the shell you have to write either \\* or '*'. Also, you should directly assign the result of the calculation to a variable instead of outputting to file: VISITS=$(echo $WR + $RD | bc).
@user368453: If you do an echo of an * it displays all the filenames in the current directory. Just put quotes around the formula and it will work: SERVICE_DEMAND=$(echo "$VISITS * $SERVICE_DEMAND" | bc)
quote your variables and you should be fine. echo "$WR+$RD" | bc
3

Instead of using bc, consider switching to a better programming language. Bash is simply unsuited for mathematics.

Comments

2

Use bc to do float calculations in Bash.

Comments

2

To set the precision (number of digits of the answer to the right of the decimal point), write:

WR=5
RD=7
VISITS=$[WR+RD]
SERVICE_DEMAND=.0895406
SERVICE_DEMAND=`echo "scale=5; $VISITS * $SERVICE_DEMAND" |bc -l`
echo Service Demand = $SERVICE_DEMAND

This outputs:

Service Demand = 1.0744872

The scale=5 sets 5 digits of precision; the backquotes cause the contained expression to be evaluated and the ouput (from the bc -l) to be assigned to your variable.

3 Comments

Thanks But... It returned the following mistake: (standard_in) 2: syntax error
I just ran this again on GNU bash, version 4.0.35(1)-release (i586-suse-linux-gnu)
Alucard, Perhaps your cut-and-pasting introduced a \r\n. Try <code>dos2unix scriptname</code> on your copy.
1

You'll have to use an external program like bc to do floating-point math in your scripts.

Something like:

echo ($WR+$RD)*$SERVICE_DEMAND | bc

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.