0

我需要从我的 shell 脚本生成 JSON 输出。我需要获取特定机器的 Ram 插槽详细信息并使用这些详细信息生成 JSON。要获取我正在使用的 Ram 详细信息,system_profiler SPMemoryDataType 它会生成如下详细信息。

    BANK 0/DIMM0:

      Size: 2 GB
      Type: DDR3
      Speed: 1600 MHz
      Status: OK
      Manufacturer: 0x802C
      Part Number: 0x384A54463235363634485A2D3147364D3120
      Serial Number: 0xE98388E6

    BANK 1/DIMM0:

      Size: 2 GB
      Type: DDR3
      Speed: 1600 MHz
      Status: OK
      Manufacturer: 0x802C
      Part Number: 0x384A54463235363634485A2D3147364D3120
      Serial Number: 0xE98388E5

从那我应该像这样形成JSON

[
{"Bank":"0/DIMM0","Serial Number":"0xE98388E6","Status":"OK"},
{"Bank":"1/DIMM0","Serial Number":"0xE98388E5","Status":"OK"}
]

要提取银行、序列号、状态​​等单独的详细信息,我们可以使用

system_profiler SPMemoryDataType | awk '/Bank/
system_profiler SPMemoryDataType | awk '/Serial/
system_profiler SPMemoryDataType | awk '/Status/

我确信需要动态变量来从结果中生成 json。但是由于我是 shell 脚本的新手,所以我很困惑。有没有办法从输出中生成 JSON?

4

2 回答 2

3
#!/usr/bin/awk -f

$1 == "BANK" {
    bank = $2
    sub(/:/, "", bank)
    while (getline > 0) {
        if ($1 == "Serial" && $2 == "Number:") {
            serial_number = $3
        } else if ($1 == "Status:") {
            status = $2
        }
        if (serial_number != "" && status != "") {
            entries[++e] = "{\"Bank\":\"" bank "\",\"Serial Number\":\"" serial_number "\",\"Status\":\"" status "\"}"
            break
        }
    }
    bank = serial_number = status = ""
}

END {
    print "["
    if (e > 0) {
        printf "%s", entries[1]
        for (i = 2; i <= e; ++i) {
            printf ",\n%s", entries[i]
        }
        print ""
    }
    print "]"
}

用法:

awk -f script.awk file
system_profiler SPMemoryDataType | awk -f script.awk

示例输出:

[
{"Bank":"0/DIMM0","Serial Number":"0xE98388E6","Status":"OK"},
{"Bank":"1/DIMM0","Serial Number":"0xE98388E5","Status":"OK"}
]

在 shell 脚本中使用:

#!/bin/bash

system_profiler SPMemoryDataType | awk '$1 == "BANK" {
    bank = $2
    sub(/:/, "", bank)
    while (getline > 0) {
        if ($1 == "Serial" && $2 == "Number:") {
            serial_number = $3
        } else if ($1 == "Status:") {
            status = $2
        }
        if (serial_number != "" && status != "") {
            entries[++e] = "{\"Bank\":\"" bank "\",\"Serial Number\":\"" serial_number "\",\"Status\":\"" status "\"}"
            break
        }
    }
    bank = serial_number = status = ""
}

END {
    print "["
    if (e > 0) {
        printf "%s", entries[1]
        for (i = 2; i <= e; ++i) {
            printf ",\n%s", entries[i]
        }
        print ""
    }
    print "]"
}'

单线:

system_profiler SPMemoryDataType | awk '$1=="BANK"{bank=$2;sub(/:/,"",bank);while(getline>0){if($1=="Serial"&&$2=="Number:"){serial_number=$3}else if($1=="Status:"){status=$2};if(serial_number!=""&&status!=""){entries[++e]="{\"Bank\":\""bank"\",\"SerialNumber\":\""serial_number"\",\"Status\":\""status"\"}";break}};bank=serial_number=status=""}END{print "[";if(e>0){printf "%s",entries[1];for(i=2;i<=e;++i){printf ",\n%s",entries[i]};print""};print "]"}'
于 2013-09-24T07:42:16.033 回答
0

有一些图书馆可以这样做。其中之一是https://github.com/jeganathgt/libjson-sh。它是独立的 shell 脚本库,提供简单方便的 API 来在控制台中生成 json 输出。

前任 :

json_init
json_add_string "serial"  "$<cmd>"
json_add_string "bankinfo"   "$<cmd>"
json_add_string "status" "$<cmd>"
json_dump
于 2018-04-01T13:34:47.430 回答