将传感器输出提取到已排序的数组中 [英] Extracting Sensors Output into Sorted Array
问题描述
由于问题写得不好,我以新问题的形式改写了问题.
Since the question was poorly written, I'm rewording the question in form of new question.
基本上,我想使用传感器命令和诸如gawk和bash之类的脚本来提取GPU温度信息.
Basically I want to extract GPUs temperatures information using sensors command and scripts like gawk and bash.
传感器输出示例如下:
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)
GPU临时信息标记为amdgpu-pci-"BUS_ID",因此我们不在乎其他标签方案(skylake-virtual或coretemp-isa).需要做的事情是:
The GPU temp information is labeled amdgpu-pci-"BUS_ID", so we don't care about other label scheme (skylake-virtual nor coretemp-isa). Things need to be done are:
- 提取GPU温度信息,例如amdgpu-pci-0c00 具有50度,并放入一个数组中.
- 数组索引应从0开始并按顺序升序 BUS ID.
- Extracting the GPU temperatures info, for example amdgpu-pci-0c00 has 50 degree, and put in into an array.
- The array index should be started with 0 and ascending in the order of BUS ID.
如果使用上述数据,则以a为名称的数组为:
If using the above data, the array assuming a is the name, would be:
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
我需要的输出是一个无限循环,该循环不断使用其值更新数组索引:
What I need for the output is an infinite loop that keep updating the array index with its value:
0 => 52
1 => 53
2 => 47
3 => 51
4 => 57
新值应在旧值上打印,因此不会拖尾.更新应具有1秒的延迟,以便操作员可以轻松评估这些值.
The new value should print over the old value so it won't trail. The update should have 1 second delay so the operator can easily evaluate the values.
GAwk可以完成提取和排序,但是我需要将其存储在bash中的数组中,以便可以将其用于其他过程.
The extracting and sorting could be done by GAwk, but I need it to be stored in an array in bash so that I can use it for other process.
致谢
推荐答案
重复使用脚本中的部分和Ed Mortons的答案,我认为这可能对您有用:
Reusing parts from your script and Ed Mortons answer I think this might work for you:
#!/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
如果出于其他目的需要将值存储到数组中(如问题中所述),则可以执行以下操作:
if you need to store the values to an array (as stated in the question) for other purposes you can do it like this:
temps=( $( sensors | gawk '...' ) )
在这种情况下,将awk中的打印命令更改为仅打印温度[i].我的方法可以轻松扩展为包括传感器输出中的其他值(例如gpu标签或风扇速度).
In this case change the print command in awk to only print temps[i]. My approach can be easily extended to include other values from the sensors output (like the gpu labels or fan speed).
这篇关于将传感器输出提取到已排序的数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!