思路:
使用/etc/os-release中的信息,
同时调用uname -?得到内核信息
测试与ubuntu20和centos7.9
function checkOS() {
echo "{"
strList=''
if [[ -e /etc/os-release ]]; then
source /etc/os-release
strList=${strList}"\"ID\": \"$ID\", "
strList=${strList}"\"VERSION_ID\" : \"$VERSION_ID\", "
strList=${strList}"\"NAME\": \"$NAME\", "
strList=${strList}"\"VERSION\": \"$VERSION\","
strList=${strList}"\"PRETTY_NAME\": \"$PRETTY_NAME\", "
fi
kernel_name=`uname -s`
kernel_vers=`uname -v`
kernel_real=`uname -r`
kernel_arch=`uname -p`
kernel_os=`uname -o`
strList=${strList}"\"kernel\": \"$kernel_name\", "
strList=${strList}"\"kernel_ver\": \"$kernel_vers\", "
strList=${strList}"\"kernel_rel\": \"$kernel_real\", "
strList=${strList}"\"kernel_arch\": \"$kernel_arch\", "
strList=${strList}"\"kernel_os\": \"$kernel_os\", "
var=${strList%??}
echo ${var}
echo "}"
}
checkOS
ubuntu20.04结果
{
"ID":"ubuntu",
"VERSION_ID":"20.04",
"NAME":"Ubuntu",
"VERSION":"20.04.4 LTS (Focal Fossa)",
"PRETTY_NAME":"Ubuntu 20.04.4 LTS",
"kernel":"Linux",
"kernel_ver":"#49~20.04.1-Ubuntu SMP Wed May 18 18:44:28 UTC 2022",
"kernel_rel":"5.13.0-44-generic",
"kernel_arch":"x86_64",
"kernel_os":"GNU/Linux"
}
centos7.9 结果:
{
"ID":"centos",
"VERSION_ID":"7",
"NAME":"CentOS Linux",
"VERSION":"7 (Core)",
"PRETTY_NAME":"CentOS Linux 7 (Core)",
"kernel":"Linux",
"kernel_ver":"#1 SMP Tue Nov 30 15:51:32 UTC 2021",
"kernel_rel":"3.10.0-1160.49.1.el7.x86_64",
"kernel_arch":"x86_64",
"kernel_os":"GNU/Linux"
}