2016-07-28 3 views
0

シェル取得bashで円の半径は、円周が与えられている間、私は円の半径を見つけるためにスクリプトを作成した後、領域を見つける必要が

#!/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"" 

式は: RADIUS =円周/ (2 * PI)は 問題は、bashが、私はそこに答えをたくさん読んで、それでも私は

のようなものを持っている

を望むものを得ることができません小数点除算 を受け入れていないので、私は、この式の仕事を作るカントであります

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

は-l bcの使用異なる亜種の多くを、しようとしていたが、まだ右のそれを行うことができず、ミス

+2

ここには何もありませんか? http://unix.stackexchange.com/questions/40786/how-to-do-integer-float-calculations-in-bash-or-other-languages-frameworks –

+1

_ "bc -l'を使用しましたが、それは正しいですか?_あなたが 'bc'でどんな問題を抱えているかを示してください。 – John1024

答えて

1
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"" 
0

これを試してみてくださいが常に存在しています。

#!/bin/bash 
echo -n "Enter the circumference: " 
read CIRC 

PI=3.14 
RAD=$(echo $CIRC \/ \(2\*$PI\)| bc -l) 
AREA=$(echo $PI \* $RAD \* $RAD| bc -l) 

echo "The area of a circle is: "$AREA"" 
0

awk有する別の溶液;

#!/bin/bash 
echo -n "Enter the circumference: " 
read CIRC 
awk -v CIRC=$CIRC " BEGIN { PI=3.14; RAD=CIRC/(2*PI); AREA=PI*RAD*RAD; print \"The area of a circle is: \",AREA}" 
関連する問題