0

Get System Information

Here is my script:

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

Thanks in advance for your help!

Lauren Yim
  • 12,700
  • 2
  • 32
  • 59
Flowdrw
  • 3
  • 3

2 Answers2

0

Store all the echo statements in one variable or yet file

this return an array of json

cat /path/to_your-text file  | jq  --raw-input .  | jq --slurp 
Jatin Mehrotra
  • 9,286
  • 4
  • 28
  • 67
0

Here is a pure bash option. escape takes care of " in either key or value. member generates a key: value pair, and members separate members with comma:

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

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

members() {
    local sep=''
    echo -n "{"
    while read -d $'\0' 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
Allan Wind
  • 23,068
  • 5
  • 28
  • 38
  • Hi Sir @Allan Wind. Sorry for the late reply, this is the output when I executed your script. How can I arranged it by order ( Manufacturer,Productname,Version) and how can I removed the space between version's output and manufacturer, there is a gap in between them. {"Version":"P01-B0L ","Manufacturer":"Acer","Product Name":"Veriton X490G"}. – Flowdrw Apr 26 '21 at 01:08
  • JSON doesn't impose an order of object keys. If you need the values ordered you may want an array instead of object. In this implementation, order of keys is determined by how keys are accessed with the loop ""${!a[@]}" so you can either sort it the way to you or just hard-code the order you want `for k in Manufacturer "Product Name" Version`. The value you obtain from bios_version contain a space so you can read the value like this `sed 's/ *$//g' /sys/class/dmi/id/bios_version` instead of with `cat`. If you want to strip all suffix in member change "$2" to "${2%%*( )}". – Allan Wind Apr 27 '21 at 02:29
  • Thanks! sed command instead of cat works. Sorry if I have so many questions. I don't understand your last statement (ff you want to strip all suffix in member change "$2" to "${2%%*( )}". Where exactly I will put it on the member function? thank you so much for your help – Flowdrw Apr 28 '21 at 00:42
  • Fixing the input like you did you the sed is the right approach. If you want to fix it on output, change the member function to read: `member() { echo -en "\"$(escape "$1")\":\"$(escape ${2%%*( )}")\"\x00"; }` – Allan Wind Apr 29 '21 at 03:09
  • I will do hard coding so it will list the order I want. What is the first step to do it? like this in your comment (for k in Manufacturer "Product Name" Version.) Thank you so much – Flowdrw Apr 30 '21 at 07:59
  • What have you tried so far to solve your issue? – Allan Wind Apr 30 '21 at 17:37