How can I convert my output to .JSON and saves it as .json file

Get System info

#!/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`

I Want to display my output like this

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

1 Answer

Is it your real task or a very simplified? If real, you can just rewrite this script with quotes and curly braces, it's not a lot of work. Mention the missed closing double quote in third code line.

#!/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`"\"})"

To redirect this output you may use several approaches. First - you can just use redirection when calling the script like

script.sh > result.json. 

Second (if you want to always use the same filename) - you can add the following line to the beginning of the script:

exec > filename.json

The third way is to surround your desired output with curly braces and redirect this part:

#!/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

The last way I'd like to propose is to modify output of your script after execution. The following command will do exactly what you need (but first line will be processed the same way and an empty string will be added to it, to avoid it you can try third method of output redirecting):

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

Awk here is used to add quotes to each field (-F flag means 'field separator'), OFS is set to empty string to avoid extra spaces. Then sed removes last comma (I know it's possible with awk too, but this approach looks much simpler, IMO). tr is used to replace all newline characters with spaces. Output is redirected to the same file. echo at the beginning adds newline to the end of output.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like