I'm trying to write a basic script to calculate the radius and area of a circle, where PI=3.14, and the circumference is given. I am very very new to scripting, and I can't seem to figure this out.
#!/bin/bash
PI=3.14
CIRC=5
RAD=echo "((CIRC/2*PI))" | bc-l
printf "Radius: %.2f" $RAD
AREA=echo "((PI*RAD**2))" | bc-l
printf "Area: %.2f" $AREA
The sum of both equations are not being stored in those variables, and I have no idea why. I hope someone can help explain.
Below script would do it :
Notes
scale
withbc
controls the precision, check [ this ].Since
bc
can print strings, there's no need forprintf
. Nor backticks or$()
, or even some of the variables. Withbash
, theecho
can be replaced with<<<
:POSIX shell code version, using a here document:
Pure
bc
: