如何编写有效确定发行版名称的脚本?

inu*_*aze 13 distros linux

我看到了这篇关于找出安装了什么发行版的所有不同方法的帖子,所以我正在尝试编写一个脚本来尝试所有这些方法。可能的命令包括:

$ cat /etc/lsb-release 
$ cat /etc/issue 
$ dmesg | head -1
$ cat /proc/version 
$ cat /etc/slackware-version 
$ cat/etc/debian-verion 
Run Code Online (Sandbox Code Playgroud)

我试着写这样的东西(我通常说西班牙语,所以它是西班牙语):

function Nombre_SO()
{

    DistroName="Linux"
    if [ $DistroName = Linux ] ;
    then

# Debian
    debian=`cat /etc/debian_version | cut -d " " -f01 | tr '[:upper:]' '[:lower:]'`
    if [ "$debian" = "debian" || "squeeze/sid" || "lenny" ]; 
        then
        DistroName="debian"
        else
        echo "Esto no es debian"
    fi

# Slackware
    slackware=`cat /etc/slackware-version | cut -d " " -f01` | tr '[:upper:]' '[:lower:]'`
    if [ "$slackware" = "slackware" || "slackware-x86_64" ];
    then
        DistroName="slackware" 
    else
    echo "Esto no es Slackware"
}
Run Code Online (Sandbox Code Playgroud)

有人可以帮我合并所有其他方式来获得发行版的名称吗?

hmo*_*liu 13

每个发行版(尽管 lsb 努力)使用或可能使用(甚至可能缺少)/etc/ 中的不同文件来声明其名称和版本。

您应该在脚本中为每个条件添加一个条件。还要考虑到某些发行版源自其他主要发行版,并且可能会或可能不会调整其版本文件。

如果您不想重新发明轮子,您可以使用其他人的工作来实现您的目标。比如python中模块平台就有猜测分布的方法:

Help on function linux_distribution in module platform:

linux_distribution(distname='', version='', id='', supported_dists=('SuSE', 'debian', 'fedora', 'redhat', 'centos', 'mandrake', 'mandriva', 'rocks', 'slackware', 'yellowdog', 'gentoo', 'UnitedLinux', 'turbolinux'), full_distribution_name=1)
    Tries to determine the name of the Linux OS distribution name.

    The function first looks for a distribution release file in
    /etc and then reverts to _dist_try_harder() in case no
    suitable files are found.

    supported_dists may be given to define the set of Linux
    distributions to look for. It defaults to a list of currently
    supported Linux distributions identified by their release file
    name.

    If full_distribution_name is true (default), the full
    distribution read from the OS is returned. Otherwise the short
    name taken from supported_dists is used.

    Returns a tuple (distname,version,id) which default to the
    args given as parameters.
Run Code Online (Sandbox Code Playgroud)

例如:

In [1]: import platform

In [2]: platform.linux_distribution()
Out[2]: ('Ubuntu', '11.10', 'oneiric')
Run Code Online (Sandbox Code Playgroud)