Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arithmetic problem with shell script

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

like image 875
Alucard Avatar asked Jun 26 '10 15:06

Alucard


People also ask

What is $1 and $2 in shell script?

These are positional arguments of the script. Executing ./script.sh Hello World. Will make $0 = ./script.sh $1 = Hello $2 = World. Note.

How do you do arithmetic in Bash?

The recommended way to evaluate arithmetic expressions with integers in Bash is to use the Arithmetic Expansion capability of the shell. The builtin shell expansion allows you to use the parentheses ((...)) to do math calculations. The format for the Bash arithmetic expansion is $(( arithmetic expression )) .

Can Bash script do math?

Math and arithmetic operations are essential in Bash scripting. Various automation tasks require basic arithmetic operations, such as converting the CPU temperature to Fahrenheit. Implementing math operations in Bash is simple and very easy to learn.


1 Answers

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

echo $WR + $RD | bc

and so on.

like image 133
Benjamin Bannier Avatar answered Sep 19 '22 03:09

Benjamin Bannier