如何将我的输出转换为 JSON 然后保存它 as.json 文件

How can I convert my output to JSON then save it as.json file

获取系统信息

这是我的脚本:

#!/bin/bash
    
    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 }' 
                  /proc/cpuinfo | uniq | sed -e 's/^[ \t]*//'`
    
    echo -e "Memory:\t" `dmidecode -t 17 | grep "Size.*MB" | awk '{s+=} 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" })

在此先感谢您的帮助!

将所有回显语句存储在一个变量或文件中

这个return一个json

的数组
cat /path/to_your-text file  | jq  --raw-input .  | jq --slurp 

这里是一个纯粹的bash选项。 escape 处理键或值中的 "。member 生成一个键:值对,members 用逗号分隔成员:

escape() {
    echo -n "${1/\"/\\"}"
}

member() {
    echo -en "\"$(escape "")\":\"$(escape "")\"\x00"
}

members() {
    local sep=''
    echo -n "{"
    while read -d $'[=10=]' member
    do
        echo -n "${sep}$member"
        sep=,
    done
    echo -n "}"
}

declare -A a=(
    [Manufacturer]=`cat /sys/class/dmi/id/chassis_vendor`
    [Product Name]=`cat /sys/class/dmi/id/product_name`
    [Version]=`cat /sys/class/dmi/id/bios_version`
)
for k in "${!a[@]}"
do
    member "$k" "${a[$k]}"
done | members > as.JSON