如何将输出转换为 .JSON 并将其保存为 .json 文件

如何将输出转换为 .JSON 并将其保存为 .json 文件

获取系统信息

#!/bin/bash

readarray -t array <<< "$(df -h)"

var=$(echo "${array[1]}"| grep -aob '%' | grep -oE '[0-9]+')

echo "${array[3]:$var-3:4}

echo -e "Manufacturer:\t"`cat /sys/class/dmi/id/chassis_vendor`

echo -e "Product Name:\t"`cat /sys/class/dmi/id/product_name`

echo -e "Version:\t"`cat /sys/class/dmi/id/bios_version`

echo -e "Serial Number:\t"`cat /sys/class/dmi/id/product_serial`

echo -e "PC Name:\t"`hostname`

echo -e "Operating System:\t"`hostnamectl | grep "Operating System" | cut 
              -d ' ' -f5-`

echo -e "Architecture:\t"`arch`

echo -e "Processor Name:\t"`awk -F':' '/^model name/ {print $2}' 
              /proc/cpuinfo | uniq | sed -e 's/^[ \t]*//'`

echo -e "Memory:\t" `dmidecode -t 17 | grep "Size.*MB" | awk '{s+=$2} END 
                {print s / 1024 "GB"}'`

echo -e "HDD Model:\t" `cat /sys/block/sda/device/model`

echo -e "System Main IP:\t"`hostname -I`

我想像这样显示我的输出

    ({"Manufacturer":"Lenovo","Product Name":"Thinkpad":"Version":"T590","Serial Number":"1234567890" })

答案1

这是您的真正任务还是非常简单的任务?如果是真的,您只需使用引号和花括号重写此脚本即可,工作量不大。请提及第三行代码中遗漏的结束双引号。

#!/bin/bash
readarray -t array <<< "$(df -h)"

var=$(echo "${array[1]}"| grep -aob '%' | grep -oE '[0-9]+')

echo "${array[3]:$var-3:4}"

echo -e "({\"Manufacturer\":\""`cat /sys/class/dmi/id/chassis_vendor`"\","
echo -e "\"Product Name\":\""`cat /sys/class/dmi/id/product_name`"\","
echo -e "\"Version\":\""`cat /sys/class/dmi/id/bios_version`"\","
echo -e "\"Serial Number\":\""`cat /sys/class/dmi/id/product_serial`"\","
echo -e "\"PC Name\":\""`hostname`"\","
echo -e "\"Operating System\":\""`hostnamectl | grep "Operating System" | cut -d ' ' -f5-`"\","
echo -e "\"Architecture\":\""`arch`"\","
echo -e "\"Processor Name\":\""`awk -F':' '/^model name/ {print $2}' /proc/cpuinfo | uniq | sed -e 's/^[ \t]*//'`"\","
echo -e "\"Memory\":\"" `dmidecode -t 17 | grep "Size.*MB" | awk '{s+=$2} END {print s / 1024 "GB"}'`"\","
echo -e "\"HDD Model\":\""`cat /sys/block/sda/device/model`"\","
echo -e "\"System Main IP\":\""`hostname -I`"\"})"

要重定向此输出,您可以使用多种方法。首先 - 您可以在调用脚本时使用重定向,例如

script.sh > result.json. 

第二步(如果您想始终使用相同的文件名) - 您可以在脚本的开头添加以下行:

exec > filename.json

第三种方法是用花括号括起你想要的输出并重定向这部分:

#!/bin/bash
readarray -t array <<< "$(df -h)"

var=$(echo "${array[1]}"| grep -aob '%' | grep -oE '[0-9]+')

echo "${array[3]:$var-3:4}"

{echo -e "({\"Manufacturer\":\""`cat /sys/class/dmi/id/chassis_vendor`"\","
echo -e "\"Product Name\":\""`cat /sys/class/dmi/id/product_name`"\","
echo -e "\"Version\":\""`cat /sys/class/dmi/id/bios_version`"\","
echo -e "\"Serial Number\":\""`cat /sys/class/dmi/id/product_serial`"\","
echo -e "\"PC Name\":\""`hostname`"\","
echo -e "\"Operating System\":\""`hostnamectl | grep "Operating System" | cut -d ' ' -f5-`"\","
echo -e "\"Architecture\":\""`arch`"\","
echo -e "\"Processor Name\":\""`awk -F':' '/^model name/ {print $2}' /proc/cpuinfo | uniq | sed -e 's/^[ \t]*//'`"\","
echo -e "\"Memory\":\"" `dmidecode -t 17 | grep "Size.*MB" | awk '{s+=$2} END {print s / 1024 "GB"}'`"\","
echo -e "\"HDD Model\":\""`cat /sys/block/sda/device/model`"\","
echo -e "\"System Main IP\":\""`hostname -I`"\"})"} > filename.json

我建议的最后一种方法是在执行后修改脚本的输出。以下命令将完全满足您的需要(但第一行将以相同的方式处理,并将向其添加一个空字符串,为避免这种情况,您可以尝试输出重定向的第三种方法):

echo $(sed '$ s/,/)}/' <<< $(awk -F ":" 'BEGIN {OFS=""; print "({";}  { print "\"",$1,"\":\"",$2,"\",";} ' filename.json) | tr '\n' ' ') > filename.json

此处使用 awk 为每个字段添加引号(-F 标志表示“字段分隔符”),OFS 设置为空字符串以避免多余的空格。然后 sed 删除最后一个逗号(我知道使用 awk 也可以,但在我看来,这种方法看起来简单得多)。tr 用于将所有换行符替换为空格。输出重定向到同一个文件。开头的 echo 将换行符添加到输出的末尾。

相关内容