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
You can use bc
to do your floating point calculations, ie
echo $WR + $RD | bc
and so on.
Instead of using bc
, consider switching to a better programming language. Bash is simply unsuited for mathematics.
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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.