長方形の面積を表示するスクリプト

長方形の面積を表示するスクリプト

長方形の幅と高さをセンチメートル単位で表す2つの数字を入力するように求められ、長方形の面積を平方メートルと平方インチ(1インチ= 2.54センチメートル)で出力するスクリプトを作成したいと思います。

私はこれが比較的簡単なはずだと思いますが、有効な結論を出すことはできません。

ベストアンサー1

#!/bin/sh

read -p "Enter the width and height of rectangle in meters: " width height 

sqm=$(echo "$width * $height" | bc -l)
sqin=$(echo "$sqm * 1550" | bc -l)

echo "Area of the rectangle is: $sqm Square Meters or $sqin Square Inches."

(参考までに1平方メートルは1550平方フィートと同じです。Googleから知らせて知っています。)

実行例:

$ ./area.sh 
Enter the width and height of rectangle in meters: 3.5 4.5
Area of the rectangle is: 15.75 Square Meters or 24412.50 Square Inches.

おすすめ記事