尝试计算BASH中的圆的半径和面积 [英] Trying to calculate radius and area of circle in BASH

查看:69
本文介绍了尝试计算BASH中的圆的半径和面积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个基本脚本来计算圆的半径和面积,其中PI = 3.14,并给出了周长.我对脚本编写非常陌生,但似乎无法弄清楚.

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.

推荐答案

下面的脚本可以做到:

#!/bin/bash
pi=3.14
circ=5
rad=$( echo "scale=2;$circ / (2 * $pi)" | bc )
printf "Radius: %.2f\n" $rad
area=$( echo "scale=2;$pi * $rad * $rad" | bc )
printf "Area: %.2f\n" $area

注释

  1. 请参见 [命令替换] .
  2. 请不要在脚本中使用完整的大写变量,因为它们通常是系统保留的,请检查 [此] .
  3. scale bc 控制精度,请检查[此] .
  1. See [ command substitution ].
  2. Never use full uppercase variables in your script as they are usually reserved for the system, check [ this ].
  3. scale with bc controls the precision, check [ this ].

这篇关于尝试计算BASH中的圆的半径和面积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆