将临时工从摄氏转换为华氏度?

发布于 2025-02-03 08:26:46 字数 302 浏览 5 评论 0原文

#!/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心房敞 2025-02-10 08:26:46

您还可以对浮点计算使用awk,可以使用printf来控制输出格式:

#!/bin/bash
read -p "Enter degree celsius temperature: " c
awk -v c="$c" 'BEGIN {printf("The temperature in Fahrenheit is %.2f\n", 1.8 * c + 32)}'

或者我们可以删除bash parts awk唯一的解决方案

#!/usr/bin/awk -f
BEGIN {
    printf("Enter degree celsius temperature "); getline c;
    printf("The temperature in Fahrenheit is %.2f\n", 1.8 * c + 32)
}

You can also use awk for the floating point calculation and you can control the output format with printf:

#!/bin/bash
read -p "Enter degree celsius temperature: " c
awk -v c="$c" 'BEGIN {printf("The temperature in Fahrenheit is %.2f\n", 1.8 * c + 32)}'

or we can remove the bash parts and have an awk only solution

#!/usr/bin/awk -f
BEGIN {
    printf("Enter degree celsius temperature "); getline c;
    printf("The temperature in Fahrenheit is %.2f\n", 1.8 * c + 32)
}
美人迟暮 2025-02-10 08:26:46

仅使用bash,并且具有0.1的精度:

$ cat c2f
#!/usr/bin/env bash

declare -i C F
read -p "Enter degree celsius temperature: " C
F=$(( 18 * C + 320 ))
echo "The temperature in Fahrenheit is ${F:0: -1}.${F: -1: 1}"

$ ./c2f
Enter degree celsius temperature: 32
The temperature in Fahrenheit is 89.6

如果您对分数部分不感兴趣,但希望对最近的整数进行舍入:

$ cat c2f
#!/usr/bin/env bash

declare -i C F
read -p "Enter degree celsius temperature: " C
F=$(( 18 * C + 325 ))
echo "The temperature in Fahrenheit is ${F:0: -1}"

$ ./c2f
Enter degree celsius temperature: 32
The temperature in Fahrenheit is 90

With bash only, and with a 0.1 accuracy:

$ cat c2f
#!/usr/bin/env bash

declare -i C F
read -p "Enter degree celsius temperature: " C
F=$(( 18 * C + 320 ))
echo "The temperature in Fahrenheit is ${F:0: -1}.${F: -1: 1}"

$ ./c2f
Enter degree celsius temperature: 32
The temperature in Fahrenheit is 89.6

If you are not interested in the fractional part but want a rounding to the nearest integer:

$ cat c2f
#!/usr/bin/env bash

declare -i C F
read -p "Enter degree celsius temperature: " C
F=$(( 18 * C + 325 ))
echo "The temperature in Fahrenheit is ${F:0: -1}"

$ ./c2f
Enter degree celsius temperature: 32
The temperature in Fahrenheit is 90
绳情 2025-02-10 08:26:46
#!/bin/bash
read -p "Enter degree celsius temperature: " C
F=`echo "1.8 * $C + 32" | bc`
echo The temperature in Fahrenheit is $F
#!/bin/bash
read -p "Enter degree celsius temperature: " C
F=`echo "1.8 * $C + 32" | bc`
echo The temperature in Fahrenheit is $F
梓梦 2025-02-10 08:26:46

对于32(°C),您的公式1.8*32+32应产生89.6(°F),但正如您提到的 ans应该为89 ,所以我们会忘记小数并使用$((180*$ C/100+32)),因此您的程序变为(未测试):

#!/bin/bash
read -p "Enter degree celsius temperature: " c
f=$((180*$c/100+32))
echo The temperature in Fahrenheit is $f

输出:

89

基本上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):

#!/bin/bash
read -p "Enter degree celsius temperature: " c
f=$((180*$c/100+32))
echo The temperature in Fahrenheit is $f

Output:

89

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文