Bash 将传感器输出提取到排序阵列中
这个问题是问题的继续 由于这个问题写得不好,我正在以新问题的形式重新编写这个问题 基本上,我想使用sensors命令和gawk和bash等脚本提取gpu温度信息 传感器输出示例如下所示:Bash 将传感器输出提取到排序阵列中,bash,ubuntu,awk,Bash,Ubuntu,Awk,这个问题是问题的继续 由于这个问题写得不好,我正在以新问题的形式重新编写这个问题 基本上,我想使用sensors命令和gawk和bash等脚本提取gpu温度信息 传感器输出示例如下所示: amdgpu-pci-0c00 Adapter: PCI adapter fan1: 1972 RPM temp1: +50.0°C (crit = +0.0°C, hyst = +0.0°C) amdgpu-pci-0600 Adapter: PCI adapter fan
amdgpu-pci-0c00
Adapter: PCI adapter
fan1: 1972 RPM
temp1: +50.0°C (crit = +0.0°C, hyst = +0.0°C)
amdgpu-pci-0600
Adapter: PCI adapter
fan1: 1960 RPM
temp1: +47.0°C (crit = +0.0°C, hyst = +0.0°C)
amdgpu-pci-0200
Adapter: PCI adapter
fan1: 1967 RPM
temp1: +52.0°C (crit = +0.0°C, hyst = +0.0°C)
pch_skylake-virtual-0
Adapter: Virtual device
temp1: +33.0°C
amdgpu-pci-0900
Adapter: PCI adapter
fan1: 1893 RPM
temp1: +51.0°C (crit = +0.0°C, hyst = +0.0°C)
amdgpu-pci-0300
Adapter: PCI adapter
fan1: 1992 RPM
temp1: +53.0°C (crit = +0.0°C, hyst = +0.0°C)
coretemp-isa-0000
Adapter: ISA adapter
Package id 0: +24.0°C (high = +80.0°C, crit = +100.0°C)
Core 0: +23.0°C (high = +80.0°C, crit = +100.0°C)
Core 1: +21.0°C (high = +80.0°C, crit = +100.0°C)
temps=( $( sensors | gawk '...' ) )
GPU临时信息标记为amdgpu pci-“总线ID”,因此我们不关心其他标签方案(skylake virtual或coretemp isa)。需要做的事情有:
a[0] = 52 ;amdgpu-pci-0200
a[1] = 53 ;amdgpu-pci-0300
a[2] = 47 ;amdgpu-pci-0600
a[3] = 51 ;amdgpu-pci-0900
a[4] = 50 ;amdgpu-pci-0c00
我需要输出的是一个无限循环,它不断用数组索引的值更新数组索引:
0 => 52
1 => 53
2 => 47
3 => 51
4 => 57
新值应该打印在旧值上,这样它就不会跟踪。更新应有1秒延迟,以便操作员可以轻松评估值
提取和排序可以由GAwk完成,但我需要将其存储在bash中的数组中,以便将其用于其他进程
关于重复使用脚本中的部分,Ed Mortons回答我认为这可能对你有用:
#!/bin/bash
while true
do
while read -r i temp ; do
echo -en "GPU $i temp is $temp \r "
sleep 1
done < <(
sensors | gawk '
!NF {name=""}
/amdgpu/ {
name=$1
}
/^temp1:/ && name {
temps[name]=gensub(/^[^0-9]*([0-9]+).*/,"\\1",1,$2);
}
END {
PROCINFO["sorted_in"] = "@ind_str_asc"
ctr=0;
for (i in temps) {
print ctr++,temps[i]
}
} '
)
done
在这种情况下,请将awk中的“打印”命令更改为仅打印temps[i]。我的方法可以很容易地扩展到包括传感器输出的其他值(如gpu标签或风扇速度)。不幸的是,您的问题只包含要求:它没有显示您方面为自己解决此问题所做的任何努力。请把你的问题展示出来,这样你就更有可能得到有助于你进步的答案。向我们展示你迄今为止的工作,你期望的结果和你得到的结果,我们将帮助你找出答案。重新阅读可能会有所帮助。@tobby speight,这个问题实际上是一个大问题的第三部分。到目前为止,我的努力是在第一段的链接上。这是完美的。我感谢你的帮助。