【问题标题】:Bash Power/Exponent Raising ^0.16 and setting it a variableBash 幂/指数提升 ^0.16 并将其设置为变量
【发布时间】:2022-11-11 13:57:29
【问题描述】:

我是编程和 Linux Bash 的新手,希望我能正确解释我的问题。

因此,在我的 bash Linux 脚本中,我正在接受 Airtemp 和 Wind Speed,并且我试图将 Wind Speed 提高到 ^0.16 以计算 Windchill Temp。

供参考 Windchill 是什么。

#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)。如果我使用 ^ 或 ** 我试图将它提高到 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 不是内置函数,而是外部 program。在您的发行版中安装相应的软件包。
  • 当我这样做并将其更改为“low=$(($Wind Speed0.16))" 我得到错误 "30.16:语法错误:算术运算符无效(错误标记是“.16”“知道如何解决这个问题吗?
  • 抱歉,bash 不支持浮点数学。您可以使用bc,只需安装包含它的软件包。
  • 我不会使用 bc,而是切换到 zsh 或另一个原生支持浮点的 shell。或者完全不同的语言。
  • * 是 bash 的一个特殊符号,它扩展到当前目录中的所有文件(这个过程称为 globbing)。如果你想避免这种情况,你必须引用它,即\*'*',这取决于你的口味。

标签: linux bash shell


【解决方案1】:

在以“Windchill=”开头的行上,您需要在行尾反引号之前添加“| bc”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    • 2015-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多