【问题标题】:Extracting Sensors Output into Sorted Array将传感器输出提取到排序数组中
【发布时间】:2019-03-08 14:04:21
【问题描述】:

这个问题是Extracting Ubuntu Sensors Command Using Scripts的延续

由于问题写得不好,我正在以新问题的形式改写问题。

基本上,我想使用传感器命令和脚本(如 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
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)。需要做的事情是:

  1. 提取 GPU 温度信息,例如 amdgpu-pci-0c00 有 50 度,并放入一个数组中。
  2. 数组索引应该从0开始并按升序排列 总线 ID。

如果使用上述数据,假设 a 为名称的数组将是:

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 中的数组中,以便我可以将其用于其他进程。

问候

【问题讨论】:

  • 不幸的是,您的问题仅包含要求:它没有显示您自己为解决此问题所做的任何努力。请edit您的问题显示your attempt far,您将更有可能收到有助于您进步的答案。以minimal reproducible example 向我们展示您的工作、您期望的结果和您得到的结果,我们将帮助您解决问题。重新阅读How to Ask可能会有所帮助。
  • @tobby-speight,这个问题实际上是 1 个大问题的第三部分。到目前为止,我的努力是在第一段的链接中。问候

标签: bash ubuntu awk


【解决方案1】:

重用脚本中的部分和 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

编辑:如果您需要将值存储到数组中(如问题中所述)用于其他目的,您可以这样做:

temps=( $( sensors | gawk '...' ) )

在这种情况下,将 awk 中的打印命令更改为仅打印 temps[i]。我的方法可以很容易地扩展到包括来自传感器输出的其他值(如 gpu 标签或风扇速度)。

【讨论】:

  • 这是完美的。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多