在 Unix/Linux 平台上查找操作系统名称和版本的最佳方法 [英] Best way to find os name and version in Unix/Linux platform

查看:128
本文介绍了在 Unix/Linux 平台上查找操作系统名称和版本的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 Unix/Linux 平台上找到操作系统名称和版本.为此,我尝试了以下操作:

I need to find the OS name and version on Unix/Linux platform. For this I tried following:

  1. lsb_release 实用程序

/etc/redhat-release 或特定文件

但这似乎不是最佳解决方案,因为 RHEL 7 不再支持 LSB_RELEASE.

But it does not seem to be best solution as LSB_RELEASE support is no longer for RHEL 7.

有什么方法可以在任何 Unix 或 Linux 平台上运行吗?

Is there any way that will work on any Unix or Linux platform?

推荐答案

这适用于所有 Linux 环境.

This work fine for all Linux environment.

#!/bin/sh
cat /etc/*-release

在 Ubuntu 中:

In Ubuntu:

$ cat /etc/*-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=10.04
DISTRIB_CODENAME=lucid
DISTRIB_DESCRIPTION="Ubuntu 10.04.4 LTS"

或 12.04:

$ cat /etc/*-release

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=12.04
DISTRIB_CODENAME=precise
DISTRIB_DESCRIPTION="Ubuntu 12.04.4 LTS"
NAME="Ubuntu"
VERSION="12.04.4 LTS, Precise Pangolin"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu precise (12.04.4 LTS)"
VERSION_ID="12.04"

在 RHEL 中:

$ cat /etc/*-release
Red Hat Enterprise Linux Server release 6.5 (Santiago)
Red Hat Enterprise Linux Server release 6.5 (Santiago)

或者使用这个脚本:

#!/bin/sh
# Detects which OS and if it is Linux then it will detect which Linux
# Distribution.

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

GetVersionFromFile()
{
    VERSION=`cat $1 | tr "
" ' ' | sed s/.*VERSION.*= // `
}

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 "
" ' '| sed s/VERSION.*//`
        REV=`cat /etc/SuSE-release | tr "
" ' ' | 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 "
" ' ' | sed s/VERSION.*//`]"
    fi

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

fi

echo ${OSSTR}

这篇关于在 Unix/Linux 平台上查找操作系统名称和版本的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆