linux - bash命令中redhat 与 debian系统的识别
PHPz
PHPz 2017-04-17 16:31:23
[Linux讨论组]
PHPz
PHPz

学习是最好的投资!

全部回复(3)
黄舟

参考一下How can I get distribution name and version number in a simple shell script?

其中被采纳的答案

ARCH=$(uname -m | sed 's/x86_//;s/i[3-6]86/32/')

if [ -f /etc/lsb-release ]; then
    . /etc/lsb-release
    OS=$DISTRIB_ID
    VER=$DISTRIB_RELEASE
elif [ -f /etc/debian_version ]; then
    OS=Debian  # XXX or Ubuntu??
    VER=$(cat /etc/debian_version)
elif [ -f /etc/redhat-release ]; then
    # TODO add code for Red Hat and CentOS here
    ...
else
    OS=$(uname -s)
    VER=$(uname -r)
fi

稍微改改这段代码就可以了

ringa_lee

如果系统可能性只有redhat和debian的话,可以执行yum --version然后判断执行结果。yum执行成功的话就redhar,不然就得采用上面那位同学的方法了。

黄舟

look at this:

#!/bin/bash

DEBIAN='Debian'
REDHAT='rhel'

function os_type() {
    OS=$(lsb_release -ds 2>/dev/null || cat /etc/*release 2>/dev/null | head -n1 || uname -om)    
    if [[ $OS == *Debian* ]]; then
        OS_TYPE='Debian'
        echo "Debian  GNU/Linux distributions"
    elif [[ $OS == *rhel* ]]; then
        OS_TYPE='Redhat'
        echo "Redhat GNU/Linux distributions"
    else
        echo "$OS"
    fi

}

os_type
echo "$OS_TYPE"
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号