Bash脚本帮助 - 测试多个范围之间的十六进制值

时间:2014-06-18 16:38:48

标签: bash

我正在尝试编写一个bash脚本,该脚本将从dmidecode获取输入并将特定的内存地址与DIMM匹配,到目前为止我的尝试遇到了不值得分享的失败。

有问题的输出来自(带样本输出)

dmidecode -t 20

Handle 0x0036, DMI type 20, 35 bytes
Memory Device Mapped Address
        Starting Address: 0x00000000000
        Ending Address: 0x001FFFFFFFF
        Range Size: 8 GB
        Physical Device Handle: 0x001C
        Memory Array Mapped Address Handle: 0x0035
        Partition Row Position: 1

Handle 0x0037, DMI type 20, 35 bytes
Memory Device Mapped Address
        Starting Address: 0x00200000000
        Ending Address: 0x003FFFFFFFF
        Range Size: 8 GB
        Physical Device Handle: 0x001E
        Memory Array Mapped Address Handle: 0x0035
        Partition Row Position: 2

根据系统的不同,可能有1个范围,有几个

给定预定的内存地址,例如0x3fffffffe,我需要确定它在起始地址和结束地址之间的范围

下一步是显示物理设备句柄范围

编辑 我继续尝试让它工作,这是迄今为止我能想到的最好的,但它没有正确处理十六进制值

#!/bin/bash

dmidecode -t 20 > addrrng
grep 'Starting Address' addrrng | awk -F': ' '{ print $2 }' > startaddr

for i in cat startaddr; do
        if [ $1 -gt $i ]
           then
                grep -A3 $i addrrng
                break
        fi
done

错误 第7行:[:0x003fffffffe:预期的整数表达式

1 个答案:

答案 0 :(得分:1)

如果GNU awk avaliable:

dmidecode -t 20|gawk -v address=0x3fffffffe 'BEGIN{address=strtonum(address)}
               /^Handle/ {handle=$0}
               /Starting Address/ {start=strtonum($NF)}
               /Ending Address/{
                    end=strtonum($NF)
                    if ( address >= start && address <=end) {
                        print "handle: "handle;start=end=""}
                       }'

会给你:

handle: Handle 0x0037, DMI type 20, 35 bytes