使用shell 检测操作系统版本信息

34 篇文章 0 订阅

思路:

使用/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"
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值