You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bash脚本计算风寒温度时bc命令未找到问题求助

Bash脚本计算风寒温度时bc命令报错问题

我是编程和Linux Bash新手,编写脚本计算风寒温度时遇到问题。风寒温度公式如下:

WindChill = 35.74 + (0.6215×AirTemp) - (35.75×(WindSpeed^0.16)) + (0.4275×AirTemp×(WindSpeed^0.16))

读取AirTemp和WindSpeed后,我用命令pow=$(echo "$WindSpeed^0.16" | bc)计算WindSpeed的0.16次幂,但不管用^还是**,都报错:Line 61: bc: command not found。相关脚本代码片段如下:

# get operands and start computing based on the user's choice
if [[ $choice -eq 1 ]] ; then
    echo Enter AirTemp value:
    read AirTemp
    echo Enter WindSpeed value:
    read WindSpeed
pow=$(echo "$WindSpeed^0.16" | bc)
WindChill=`echo "35.74 + (0.6215 * $AirTemp) - (35.75 * $pow) + (0.4275 * $AirTemp * $pow)"` 
#WindChill = (35.74 + (0.6215*AirTemp) - (35.75*(WindSpeed^0.16))+(0.4275*AirTemp*(WindSpeed^0.16)) =77.95  EX: Airtemp 75F, Windspeed 3mph
    echo ----------------------------------------
    echo Windchill Temp of $AirTemp and $WindSpeed in F is $WindChill
    echo ----------------------------------------
    echo

解决步骤

  • 安装bc工具:报错bc: command not found是因为系统未安装bc计算器工具,根据你的Linux发行版执行对应安装命令:

    • Debian/Ubuntu系:sudo apt install bc
    • RHEL/CentOS系:sudo yum install bc
    • Fedora:sudo dnf install bc
  • 启用bc数学库处理浮点幂运算:bc默认不支持小数次幂计算,需要加-l参数调用数学库,修改幂运算命令:

    pow=$(echo "$WindSpeed^0.16" | bc -l)
    
  • 用bc计算WindChill结果:原命令直接用echo输出的是字符串,不会执行浮点计算,需同样通过bc处理:

    WindChill=$(echo "35.74 + (0.6215 * $AirTemp) - (35.75 * $pow) + (0.4275 * $AirTemp * $pow)" | bc -l)
    
  • 修正脚本缩进:pow=和WindChill=两行需与if块内其他命令保持一致缩进,避免逻辑错误。

修改后的完整代码片段

# get operands and start computing based on the user's choice
if [[ $choice -eq 1 ]] ; then
    echo Enter AirTemp value:
    read AirTemp
    echo Enter WindSpeed value:
    read WindSpeed
    pow=$(echo "$WindSpeed^0.16" | bc -l)
    WindChill=$(echo "35.74 + (0.6215 * $AirTemp) - (35.75 * $pow) + (0.4275 * $AirTemp * $pow)" | bc -l)
    # 示例:AirTemp=75F,WindSpeed=3mph时,结果应为77.95左右
    echo ----------------------------------------
    echo Windchill Temp of $AirTemp and $WindSpeed in F is $WindChill
    echo ----------------------------------------
    echo
fi

内容的提问来源于stack exchange,提问作者Matthew Watson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 20:30:46