Better way to check linux version?

2019-04-13 16:37发布

What I currently have. Is this the best way? Have a script with lots of functions and need a way to know what OS is running before running each function.

CHECK_architecture()
{
    architecture=`uname -m`
    if [ "$architecture" != "x86_64" ] && [ "$architecture" != "ia64" ]; then
        architecture="x86"
    else
        architecture="x86_64"
    fi
}
CHECK_distro()
{
    DISTRO="";
    if [ `uname -r | egrep '(6.2-RELEASE|6.1-RELEASE|5.5-RELEASE|6.1-STABLE|5.4-RELEASE|6.0-RELEASE|5.3-RELEASE|4.10-RELEASE|4.11-RELEASE)'` ]; then
        DISTRO="FreeBSD";
        $BIN_ECHO " System is running FreeBSD"
    elif [ -f /etc/debian_version ];  then 
           $BIN_ECHO " System is running Debian Linux"
           DISTRO=DEBIAN;
    elif [ -f /etc/SuSE-release ]; then
           $BIN_ECHO " System is running SuSE Linux"
           DISTRO=SUSE;
    elif [ -f /etc/fedora-release ]; then
           $BIN_ECHO " System is running Fedora Linux"  
          DISTRO=FEDORA;
    elif [ -f /etc/redhat-release ]; then
           $BIN_ECHO " System is running Red Hat Linux"
           DISTRO=REDHAT;
    else 
        $BIN_ECHO -e " no supported distribution found running "
    exit 1
fi
}

5条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-04-13 16:50

There isn't an absolutely reliable way to check the Linux distribution and its version.

$ head -n1 /etc/issue
查看更多
ゆ 、 Hurt°
3楼-- · 2019-04-13 16:52

You are looking for the config.guess utility. It will reliably determine the architecture and OS, and give you a standardized moniker which many other tools use. It will not tell you precisely which Linux distribution you have, but you should not need that information -- please explain what you are using it for, and I can give further advice.

查看更多
老娘就宠你
4楼-- · 2019-04-13 16:54

Probably most correct and easiest way is to follow Free Standards Group, and use lsb-release: http://linux.die.net/man/1/lsb_release

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-04-13 17:06

From the Linux Standard Base article at wikipedia:

The Linux Standard Base (LSB) is a joint project by several Linux distributions under the organizational structure of the Linux Foundation to standardize the software system structure, including the filesystem hierarchy, used with Linux operating system. The LSB is based on the POSIX specification, the Single UNIX Specification, and several other open standards, but extends them in certain areas.

According to the LSB: The goal of the LSB is to develop and promote a set of open standards that will increase compatibility among Linux distributions and enable software applications to run on any compliant system even in binary form. In addition, the LSB will help coordinate efforts to recruit software vendors to port and write products for Linux Operating System.

If you are using some LSB compliant distribution (and you should), just man lsb_release:

 $ lsb_release -a
 No LSB modules are available.
 Distributor ID: Ubuntu
 Description:    Ubuntu 8.04.4 LTS
 Release:        8.04
 Codename:       hardy


 $ lsb_release -a
 LSB Version:    :core-3.1-amd64:core-3.1-ia32:core-3.1-noarch:graphics-3.1-amd64:graphics-3.1-ia32:graphics-3.1-noarch
 Distributor ID: CentOS
 Description:    CentOS release 5.5 (Final)
 Release:        5.5
 Codename:       Final
查看更多
倾城 Initia
6楼-- · 2019-04-13 17:08

Actually, it depends on the type of linux OS you are running. To me, best way to get the version of linux you are on is from /etc/redhat-release if you are on Redhat. For other,

    Redhat: Test for /etc/redhat-release, check contents
    Debian: Test for /etc/debian_version, check contents
    Mandriva: Test for /etc/version, check contents
    Slackware: Test for /etc/slackware-version, check contents

Generally speaking, check for /etc/*-release and /etc/*-version

查看更多
登录 后发表回答