BetaCrasher
BetaCrasher

Reputation: 23

Dividing multiple numbers with a variable in bash

I need to calculate the area of a circle if the user inputs the circumference. This is what I have, but it's not working:

let radius=$circumference/(2*3.1415) 

and

let area=3.1415*$radius*$radius

Upvotes: 1

Views: 425

Answers (1)

Vinicius Placco
Vinicius Placco

Reputation: 1731

As the comment pointed out, bash won't do floats. I'd try a simple echo+bc solution, but you can use awk and others as well.

radius=$(echo $circumference/\(2*3.1415\) | bc -l)

and

area=$(echo 3.1415*$radius*$radius | bc -l)

not elegant or particularly portable, but it works.

Edit: I created a test.sh file:

#!/bin/bash

circumference=4

radius=$(echo $circumference/\(2*3.1415\) | bc -l)

area=$(echo 3.1415*$radius*$radius | bc -l)

echo $radius $area

and when I do bash test.sh on the terminal I get:

.63663854846410950183 1.27327709692821900365

Upvotes: 2

Related Questions