在Shell脚本中使用动态变量生成JSON

时间:2013-09-24 07:10:54

标签: linux bash shell

我需要从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/

我确信需要使用Dynamic变量从结果中执行表单json。但由于我是shell脚本的新手,我很困惑。有没有办法从输出生成JSON?

2 个答案:

答案 0 :(得分:2)

#!/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 "]"}'

答案 1 :(得分: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