lsb_release -a
可能是您找到此信息的最佳选择,并且能够以一致的方式做到这一点。
LSB的历史
该lsb
命令中的代表项目Linux Standards Base,该项目是Linux基金会赞助的伞形项目,旨在提供用于在各种Linux发行版上执行基本操作的通用方法。
该项目是自愿的,供应商可以作为用户参加该项目,也可以作为围绕不同模块的各种规范的促进者参与该项目,以帮助推动不同Linux发行版中的标准化。
宪章摘录
LSB工作组的核心目标是解决这两个问题。我们与主要的发行商协商后,发布了描述发行必须支持的最小API集的标准。我们还提供测试和工具,以衡量对标准的支持,并使应用程序开发人员可以针对通用集合。最后,通过我们的测试工作,我们试图防止分布之间不必要的差异。
与LSB相关的有用链接
批评
LSB存在许多问题,这些问题使Debian等发行版出现问题。RPM的强制使用为1。有关此问题的更多信息,请参见Wikipedia文章。
Novell
如果您进行搜索,则可能会看到标题相当陈旧的页面:从Novell中检测底层Linux发行版。这是我见过的几个实际的列表之一,该列表显示了几个主要发行版以及如何检测所使用的基础发行版。
摘抄
Novell SUSE /etc/SUSE-release
Red Hat /etc/redhat-release, /etc/redhat_version
Fedora /etc/fedora-release
Slackware /etc/slackware-release, /etc/slackware-version
Debian /etc/debian_release, /etc/debian_version,
Mandrake /etc/mandrake-release
Yellow dog /etc/yellowdog-release
Sun JDS /etc/sun-release
Solaris/Sparc /etc/release
Gentoo /etc/gentoo-release
UnitedLinux /etc/UnitedLinux-release
ubuntu /etc/lsb-release
该页面还包含一个方便的脚本,该脚本尝试仅使用普通uname
命令对上述内容进行整理,并提供上述文件之一。
注意:此列表已过时,但是您可以轻松地从列表中删除带有日期的发行版(例如Mandrake),并将其替换为替代版本。如果您尝试支持大量的Solaris和Linux变体,则这种类型的脚本可能是一种方法。
Linux黑手党
更多搜索将打开以下在Linuxmafia.com上维护的页面,标题为:/ etc / release相当于各式 Linux(和其他Unix)发行版的页面。这可能是迄今为止我所见过的最详尽的清单。您可以使用case / switch语句来整理此列表,并将其作为软件分发的一部分。
实际上,该页面底部有一个脚本可以完全做到这一点。因此,您可以简单地下载脚本并将其用作软件分发的第三方。
脚本
#!/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}
注意:此脚本应该看起来很熟悉,它是Novell版本的最新版本!
腿部脚本
我见过的另一种方法是滚动自己的脚本,类似于上面的Novell方法,但改用LSB。标题为“确定Linux(或UNIX)发行版本名称的通用方法”的本文介绍了一种这样的方法。
# Determine OS platform
UNAME=$(uname | tr "[:upper:]" "[:lower:]")
# If Linux, try to determine specific distribution
if [ "$UNAME" == "linux" ]; then
# If available, use LSB to identify distribution
if [ -f /etc/lsb-release -o -d /etc/lsb-release.d ]; then
export DISTRO=$(lsb_release -i | cut -d: -f2 | sed s/'^\t'//)
# Otherwise, use release info file
else
export DISTRO=$(ls -d /etc/[A-Za-z]*[_-][rv]e[lr]* | grep -v "lsb" | cut -d'/' -f3 | cut -d'-' -f1 | cut -d'_' -f1)
fi
fi
# For everything else (or if above failed), just use generic identifier
[ "$DISTRO" == "" ] && export DISTRO=$UNAME
unset UNAME
可以将这段代码包含在系统/etc/bashrc
或某些此类文件中,然后再设置环境变量$DISTRO
。
海湾合作委员会
信不信由你。另一种方法是利用gcc
。如果查询该命令,gcc --version
您将获得gcc为其构建的发行版,该发行版始终与所运行的系统相同。
软呢帽14
$ gcc --version
gcc (GCC) 4.5.1 20100924 (Red Hat 4.5.1-4)
Copyright (C) 2010 Free Software Foundation, Inc.
CentOS 5.x
$ gcc --version
gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-54)
Copyright (C) 2006 Free Software Foundation, Inc.
CentOS 6.x版
$ gcc --version
gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-3)
Copyright (C) 2010 Free Software Foundation, Inc.
Ubuntu 12.04
$ gcc --version
gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
TL; DR;
那么我应该使用哪一个呢?我倾向于使用lsb_release -a
我经常使用的任何Linux发行版(RedHat,Debian,Ubuntu等)。对于您支持不提供的系统的情况lsb_release
,与上面的脚本之一类似,我会在提供的软件分发中投入自己的力量。
更新#1:SuSE的后续行动
通过在下面的评论中与@Nils进行交谈,可以确定,无论出于何种原因,SLES11似乎都会默认取消安装LSB。这只是一个可选安装,对于提供此类关键功能的软件包而言似乎是相反的。
因此,我借此机会与OpenSuSE项目的某人联系,以了解其原因。
电子邮件摘录
Hi Rob,
I hope you don't mind me contacting you directly but I found your info here:
https://en.opensuse.org/User:Rjschwei. I participate on one of the StackExchange
sites, Unix & Linux and a question recently came up regarding the best option
for determining the underlying OS.
http://unix.stackexchange.com/questions/92199/how-can-i-reliably-get-the-operating-systems-name/92218?noredirect=1#comment140840_92218
In my answer I suggested using lsb_release, but one of the other users mentioned
that this command wasn't installed as part of SLES11 which kind of surprised me.
Anyway we were looking for some way to confirm whether this was intentionally
dropped from SLES or it was accidental.
Would you know how we could go about confirming this one way or another?
Thanks for reading this, appreciate any help and/or guidance on this.
-Sam Mingolelli
http://unix.stackexchange.com/users/7453/slm
这是罗伯的回应
Hi,
On 10/01/2013 09:31 AM, Sam Mingo wrote:
- show quoted text -
lsb_release was not dropped in SLES 11. SLES 11 is LSB certified. However, it
is not installed by default, which is consistent with pretty much every other
distribution. The lsb_release command is part of the lsb-release package.
At present almost every distribution has an entry in /etc such as
/etc/SuSE-release for SLES and openSUSE. Since this is difficult for ISVs and
others there is a standardization effort going on driven by the convergence to
systemd. The standard location for distribution information in the future will
be /etc/os-release, although Ubuntu will probably do something different.
HTH,
Robert
-- Robert Schweikert MAY THE SOURCE BE WITH YOU
SUSE-IBM Software Integration Center LINUX
Tech Lead
Public Cloud Architect
uname -s
在Linux外部应该足够(可能需要BSD)。