如何判断Linux系统的具体发行版本信息


=Start=

缘由:

不同的Linux发行版有不同的「包管理工具」,所以有时同一个软件对应的安装包名称和安装命令也都不一样(甚至同一发行版的不同版本之间也会有所不同),如果需要写类似于 LNMP 等兼容各Linux系统的一键安装工具时,是需要提前判断Linux系统的具体发行版信息的,然后根据对应的版本信息进行操作。这里我根据以往的一些经验记录一下常见Linux系统发行版的判断方法,作为备忘和参考。

正文:

准确判断Linux系统的发行版需要对LFS和各Linux发行版足够熟悉才行,下面是我碰到的一些常用的Linux发行版及对应包管理工具:

  • Fedora / RedHat / CentOS
    $ yum/rpm
  • Gentoo
    $ emerge
  • Debian / Ubuntu
    $ apt-get/dpkg
  • Arch
    $ pacman

根据自己之前的一些经验,一般情况下可以根据如下3个文件来判断Linux系统的具体发行版本信息

  • /etc/*release
  • /etc/*version
  • /etc/*issue*
一个简单的Linux系统发行版本检测脚本:
function check_os_type()
{
    local line

    line=`head -n 1 /etc/issue`
    if echo $line|grep "[Cc]ent[Oo][Ss]" >/dev/null; then
        br_os_type=1
    elif echo $line|grep "[Rr]ed.Hat.Enterprise" >/dev/null; then
        br_os_type=2
    elif echo $line|grep "[Uu]buntu" >/dev/null; then
        br_os_type=3
    elif echo $line|grep "[Dd]ebian" >/dev/null; then
        br_os_type=4
    elif echo $line|grep "[Ff]edora" >/dev/null; then
        br_os_type=5
    else
        echo -e "unknown os type - $line is not supported."
        exit 0
    fi

    echo -e "target os type - $line"
    echo $br_os_type
}
一个较为通用的Linux系统版本信息检测脚本:
#!/bin/bash
# Detects which OS and if it is Linux then it will detect which Linux Distribution.
# https://www.novell.com/coolsolutions/feature/11251.html

OS=`uname -s`
REV=`uname -r`
MACH=`uname -m`

if [ "${OS}" = "SunOS" ] ; then
    OS=Solaris
    ARCH=`uname -p`
    OSSTR="${OS} ${REV}(${ARCH} `uname -v`)"
elif [ "${OS}" = "AIX" ] ; then
    OSSTR="${OS} `oslevel` (`oslevel -r`)"
elif [ "${OS}" = "Linux" ] ; then
    KERNEL=`uname -r`
    if [ -f /etc/redhat-release ] ; then
        DIST='RedHat'
        PSUEDONAME=`cat /etc/redhat-release | sed s/.*\(// | sed s/\)//`
        REV=`cat /etc/redhat-release | sed s/.*release\ // | sed s/\ .*//`
    elif [ -f /etc/SUSE-release ] ; then
        DIST=`cat /etc/SUSE-release | tr "\n" ' '| sed s/VERSION.*//`
        REV=`cat /etc/SUSE-release | tr "\n" ' ' | sed s/.*=\ //`
    elif [ -f /etc/mandrake-release ] ; then
        DIST='Mandrake'
        PSUEDONAME=`cat /etc/mandrake-release | sed s/.*\(// | sed s/\)//`
        REV=`cat /etc/mandrake-release | sed s/.*release\ // | sed s/\ .*//`
    elif [ -f /etc/debian_version ] ; then
        DIST="Debian `cat /etc/debian_version`"
        REV=""
    fi

    if [ -f /etc/UnitedLinux-release ] ; then
        DIST="${DIST}[`cat /etc/UnitedLinux-release | tr "\n" ' ' | sed s/VERSION.*//`]"
    fi

    OSSTR="${OS} ${DIST} ${REV}(${PSUEDONAME} ${KERNEL} ${MACH})"
fi

echo ${OSSTR}
参考链接:

=END=


《 “如何判断Linux系统的具体发行版本信息” 》 有 3 条评论

回复 hi 取消回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注