首页 > 解决方案 > 无法获取命令输出的绝对值

问题描述

所以我想制作一个简单的脚本来继续检查我的 RasPi 的 CPU 温度,它存储在 中/sys/class/thermal/thermal_zone0/temp,因此cat /sys/class/thermal/thermal_zone0/temp会给出温度,但是像这样:

cat /sys/class/thermal/thermal_zone0/temp
38459

这基本上意味着 38.459 摄氏度。

我无法将输出格式化为 38.594 °C

我的代码:

tempT="$(cat /sys/class/thermal/thermal_zone0/temp)"
tempC=$($tempT / 1000)
echo "$tempC °C"

我得到的错误:

-bash: 38459: command not found
 °C

谢谢

标签: linuxbashshellterminalsh

解决方案


如果它在您的系统上可用,我会使用它。

$ CELSIUS=$(bc -l <<< $(cat /sys/class/thermal/thermal_zone0/temp)/1000)
$ echo $CELSIUS 
25.00000000000000000000

推荐阅读