在Unix / Linux平台上find操作系统名称和版本的最佳方法
我需要在Unix / Linux平台上find操作系统的名称和版本。 为此,我尝试了以下内容:
-
lsb_release
实用程序 -
/etc/redhat-release
或特定的文件
但似乎并不是最好的解决scheme,因为对于RHEL 7,不再支持LSB_RELEASE。
有任何方法可以在任何Unix或Linux平台上工作吗?
任何帮助将不胜感激。
这项工作适用于所有Linux环境。
#!/bin/sh cat /etc/*-release
在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 "\n" ' ' | 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 "\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}
下面的命令很好地解决了我的问题。 它给你的操作系统名称和版本。
lsb_release -a
“lsb_release”命令提供了某些Linux标准基础和特定于发行版的信息。 因此,使用“ lsb_release -a ”命令我们可以得到操作系统名称和操作系统版本。
使用perl和Linux :: Distribution ,一个老问题的最干净的解决scheme:
#!/bin/sh perl -e ' use Linux::Distribution qw(distribution_name distribution_version); my $linux = Linux::Distribution->new; if(my $distro = $linux->distribution_name()) { my $version = $linux->distribution_version(); print "you are running $distro"; print " version $version" if $version; print "\n"; } else { print "distribution unknown\n"; } '