Skip to content
Advertisement

Get a radius of a circle in bash shell

I need to write a script to find radius of a circle and then find the area while the circumference is given

#!/bin/bash
echo -n "Enter the circumference: "
read CIRC
 PI=3.14
let RAD=$(($CIRC/((2*$PI )) )) 
let  AREA=$PI * $RAD * $RAD
echo "The area of a circle is: "$AREA""

The formula is: RADIUS=CIRCUMFERENCE/(2 * PI) The problem is I cant make this formula work because bash doesn’t accept decimal division I read a lot of answers there but still can’t get what I want

I have something like

let RAD=$(($CIRC/((2*$PI )) )) 

was trying a lot of different variants,used bc -l but still can’t do it right and there are always a mistakes

Advertisement

Answer

echo -n "Enter the circumference: "
read CIRC
PI=3.14
RAD=`bc -l <<< "$CIRC/(2*$PI)"`
echo $RAD
AREA=`bc  -l <<< "$PI*$RAD*$RAD"`
echo "The area of a circle is: "$AREA""
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement