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
These are positional arguments of the script. Executing ./script.sh Hello World. Will make $0 = ./script.sh $1 = Hello $2 = World. Note.
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 )) .
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.
You can use bc
to do your floating point calculations, i.e.
echo $WR + $RD | bc
and so on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With