将临时工从摄氏转换为华氏度?
#!/bin/bash
read -p "Enter degree celsius temperature: " C
F=$(1.8*{$C})+32
echo The temperature in Fahrenheit is $F
在上面的shell脚本中,我试图将温度从塞尔斯转换为
华氏
/code/source.sh:第3行:1.8 {32}:找不到命令 华氏温度为+32 *
Ans应为89
#!/bin/bash
read -p "Enter degree celsius temperature: " C
F=$(1.8*{$C})+32
echo The temperature in Fahrenheit is $F
in above shell script i am trying to convert temp from Celsius to Fahrenheit
getting this error
/code/source.sh: line 3: 1.8{32}: command not found
The temperature in Fahrenheit is +32*
Ans should be 89
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您还可以对浮点计算使用
awk
,可以使用printf
来控制输出格式:或者我们可以删除
bash
partsawk
唯一的解决方案You can also use
awk
for the floating point calculation and you can control the output format withprintf
:or we can remove the
bash
parts and have anawk
only solution仅使用bash,并且具有0.1的精度:
如果您对分数部分不感兴趣,但希望对最近的整数进行舍入:
With bash only, and with a 0.1 accuracy:
If you are not interested in the fractional part but want a rounding to the nearest integer:
对于32(°C),您的公式
1.8*32+32
应产生89.6(°F),但正如您提到的 ans应该为89 ,所以我们会忘记小数并使用$((180*$ C/100+32))
,因此您的程序变为(未测试):输出:
基本上Bash不允许您使用小数(1.8),但您可以用分数替换(180/100或18/10甚至9/5,请参见评论)。 bash可以用它计算,但您会丢失小数(89.6 - > 89)。
For 32 (°C) your formula
1.8*32+32
should yield 89.6 (°F) but as you mention Ans should be 89 so we'll forget about the decimals and use$((180*$c/100+32))
instead, so your program becomes (untested):Output:
Basically Bash won't allow you to use decimals (1.8) but you can replace it with a fraction (180/100 or 18/10 or even 9/5, see the comments). Bash can compute with it but you'll lose the decimals (89.6 -> 89).