我在带有 AllWinner H3 QuadCore Arm V7 的 Orange Pi 上运行 Ubuntu Xenial,需要不断监控 CPU 频率和温度,因此我有这个 bash 脚本来帮助我,但问题是它只显示核心 3 的信息,而不是其他的信息。
#/bin/bash
while (true); do
## retrieves core 0 info
cpu0_cur=$(( `cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq` / 1000 ))
cpu0_max=$(( `cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq` / 1000 ))
cpu0_min=$(( `cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq` / 1000 ))
cpu0_cur="$(printf "%4s" $cpu0_cur)"
cpu0_max="$(printf "%4s" $cpu0_max)"
cpu0_min="$(printf "%4s" $cpu0_min)"
## retrieves core 1 info
cpu1_cur=$(( `cat /sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq` / 1000 ))
cpu1_max=$(( `cat /sys/devices/system/cpu/cpu1/cpufreq/scaling_max_freq` / 1000 ))
cpu1_min=$(( `cat /sys/devices/system/cpu/cpu1/cpufreq/scaling_min_freq` / 1000 ))
cpu1_cur="$(printf "%4s" $cpu1_cur)"
cpu1_max="$(printf "%4s" $cpu1_max)"
cpu1_min="$(printf "%4s" $cpu1_min)"
## retrieves core 2 info
cpu2_cur=$(( `cat /sys/devices/system/cpu/cpu2/cpufreq/scaling_cur_freq` / 1000 ))
cpu2_max=$(( `cat /sys/devices/system/cpu/cpu2/cpufreq/scaling_max_freq` / 1000 ))
cpu2_min=$(( `cat /sys/devices/system/cpu/cpu2/cpufreq/scaling_min_freq` / 1000 ))
cpu2_cur="$(printf "%4s" $cpu2_cur)"
cpu2_max="$(printf "%4s" $cpu2_max)"
cpu2_min="$(printf "%4s" $cpu2_min)"
## retrieves core 3 info
cpu3_cur=$(( `cat /sys/devices/system/cpu/cpu3/cpufreq/scaling_cur_freq` / 1000 ))
cpu3_max=$(( `cat /sys/devices/system/cpu/cpu3/cpufreq/scaling_max_freq` / 1000 ))
cpu3_min=$(( `cat /sys/devices/system/cpu/cpu3/cpufreq/scaling_min_freq` / 1000 ))
cpu3_cur="$(printf "%4s" $cpu3_cur)"
cpu3_max="$(printf "%4s" $cpu3_max)"
cpu3_min="$(printf "%4s" $cpu3_min)"
## retrieves system temp
tempr=`cat /sys/devices/virtual/hwmon/hwmon1/temp1_input`
tempr="$(printf "%3s" $tempr)"
## retrieves ddr info
ddr_cur=$(( `cat /sys/devices/platform/sunxi-ddrfreq/devfreq/sunxi-ddrfreq/cur_freq` / 1000 ))
ddr_max=$(( `cat /sys/devices/platform/sunxi-ddrfreq/devfreq/sunxi-ddrfreq/max_freq` / 1000 ))
ddr_min=$(( `cat /sys/devices/platform/sunxi-ddrfreq/devfreq/sunxi-ddrfreq/min_freq` / 1000 ))
ddr_cur="$(printf "%4s" $ddr_cur)"
ddr_max="$(printf "%4s" $ddr_max)"
ddr_min="$(printf "%4s" $ddr_min)"
## prints results
echo -ne " CORE 0 (MHz): $cpu0_cur (min $cpu0_min, max $cpu0_max) TEMPERATURE: $tempr MEMORY (MHz): $ddr_cur (min $ddr_min, max $ddr_max) "\\r
echo -ne " CORE 1 (MHz): $cpu1_cur (min $cpu1_min, max $cpu1_max) TEMPERATURE: $tempr MEMORY (MHz): $ddr_cur (min $ddr_min, max $ddr_max) "\\r
echo -ne " CORE 2 (MHz): $cpu2_cur (min $cpu2_min, max $cpu2_max) TEMPERATURE: $tempr MEMORY (MHz): $ddr_cur (min $ddr_min, max $ddr_max) "\\r
echo -ne " CORE 3 (MHz): $cpu3_cur (min $cpu3_min, max $cpu3_max) TEMPERATURE: $tempr MEMORY (MHz): $ddr_cur (min $ddr_min, max $ddr_max) "\\r
sleep 0.2
done
但它的输出只是这样的:
CPU 3(MHz):1536(最小480,最大1536)温度:74 内存(MHz):672(最小408,最大672)
我已经记不太清楚 bash 脚本了,已经过去很多年了,不过请问一下我遗漏了什么?提前谢谢您 ;)
答案1
你有:
echo -ne "..."\\r
意思是:
-n
:不在末尾输出换行符,因此任何后续命令都从同一行开始-e
:解释反斜杠转义序列,因此:\r
变成回车,下一个命令从同一行的开头开始输出
因此,所有命令都会打印到同一行,每条命令都会覆盖前一条命令,并且只有最后一条命令可见。删除-ne
和\r
:
echo " CORE 0 (MHz): $cpu0_cur (min $cpu0_min, max $cpu0_max) TEMPERATURE: $tempr MEMORY (MHz): $ddr_cur (min $ddr_min, max $ddr_max) "
此外,您不需要所有这些变量:
while true; do
tempr=$(cat /sys/devices/virtual/hwmon/hwmon1/temp1_input)
## retrieves ddr info
ddr_cur=$(( $(cat /sys/devices/platform/sunxi-ddrfreq/devfreq/sunxi-ddrfreq/cur_freq) / 1000 ))
ddr_max=$(( $(cat /sys/devices/platform/sunxi-ddrfreq/devfreq/sunxi-ddrfreq/max_freq) / 1000 ))
ddr_min=$(( $(cat /sys/devices/platform/sunxi-ddrfreq/devfreq/sunxi-ddrfreq/min_freq) / 1000 ))
for i in {0..3}; do
## retrieves core info
cur=$(( $(cat /sys/devices/system/cpu/cpu$i/cpufreq/scaling_cur_freq) / 1000 ))
max=$(( $(cat /sys/devices/system/cpu/cpu$i/cpufreq/scaling_max_freq) / 1000 ))
min=$(( $(cat /sys/devices/system/cpu/cpu$i/cpufreq/scaling_min_freq) / 1000 ))
## retrieves system temp
printf '\tCORE %d (MHz): %4s (min %4s, max %4s) TEMPERATURE: %4s MEMORY (MHz): %4s(min %4s, max %4s)\n' $i "$cur" "$min" "$max" "$tempr" "$ddr_cur" "$ddr_min" "$ddr_max"
done
sleep 0.2
done