我需要从Linux Ubuntu应用程序,传感器中提取并处理以下输出的显卡温度整数:
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)
假设我想提取与 amd GPU 温度相关的信息,即 50、47、52、51 和 53。到目前为止,我所拥有的是执行以下代码:
sensors|grep temp| grep -Eo '+[0-9]{0,9}'
我得到了:
+50
+0
+0
+47
+0
+0
+52
+0
+0
+32
+51
+0
+0
+53
+0
+0
所以我需要弄清楚:
- 正则表达式环顾断言,以便它捕获数字开头有 + 号的整数,而不显示 +(加号)。
- 一种仅获取 amdgpu 信息的方法,这样它就不会抓取其他信息。
- 一种处理这些温度数字的方法,例如,我可以编写一个 bash 脚本来处理这些数字,而如果温度低于 30,则执行此操作,如果超过 70,则执行此操作。我应该将结果放在数组中并进行循环,还是有其他实用的方法?
请帮忙。 问候
在那里,您需要的温度存储在一个数组中,然后您可以使用它们进行数学运算。
arr=( $( IFS=$'n' gawk 'BEGIN{ RS="nn"} { if($0 ~ /amdgpu/) print $0 }' test.txt | gawk 'BEGIN{ FS="[+.]" } { if($1 ~ /temp1:/) print $2 }' ) )
echo "${arr[*]}"
50 47 52 51 53
测试.txt包含示例输出。从传感器获取输入命令(未测试)
arr=( $( sensors | IFS=$'n' gawk 'BEGIN{ RS="nn"} { if($0 ~ /amdgpu/) print $0 }' | gawk 'BEGIN{ FS="[+.]" } { if($1 ~ /temp1:/) print $2 }' ) )
echo "${arr[*]}"
50 47 52 51 53
你也可以用一个grep来获取临时值,如果你愿意使用类似Perl的正则表达式:
sensors | grep -oP 'tempd:s++Kd+'
我们 greptemp
后跟一个数字和一个冒号,然后至少一个空格字符和一个加号,然后我们给出 lookback 断言K
丢弃它之前的所有内容,最终捕获只是d+
(一个或多个数字)。