12

I saw this post on all the different ways to find out what distro is installed, so I'm trying to write a script that tries them all. The possible commands include:

$ cat /etc/lsb-release 
$ cat /etc/issue 
$ dmesg | head -1
$ cat /proc/version 
$ cat /etc/slackware-version 
$ cat/etc/debian-verion 

I tried writing something like this (I speak Spanish normally, so it's in Spanish):

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"
}

Can someone help me incorporate all the other ways to get a distro's name?

inukaze
  • 461

4 Answers4

13

Each distribution (despite of lsb efforts) use or may use (or even may lack it) a different file in /etc/ to declare what is its name and version.

You sould add a condition in your script for each one. Also take in to account that some distros are derived from other major ones and may or may not adapt their version files.

If you don't want to reinvent the wheel you can use other people work to achieve what you're looking for. For example in python in the module platform there is a method to guess the distribution:

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.

for example:

In [1]: import platform

In [2]: platform.linux_distribution()
Out[2]: ('Ubuntu', '11.10', 'oneiric')
hmontoliu
  • 1,947
  • 12
  • 11
3

The Linux Standard Base specifies a command for that:

lsb_release -si

It's not always part of the default installation, so if you want your script to work on every system, you'll have to fall back to the look-and-guess route.

  • lsb_release -si works with some distros , but not with all linux , under "ArchLinux , Slackware & Derivates" this dont work , the result is "blank" – inukaze Oct 22 '11 at 14:51
1

This is a bit of a "brute-force" method of accomplishing things, but it's quick and should, using bash, work on most distro's

ver=$(cat /etc/*{issues,release,version} 2> /dev/null)
if [[ $(echo $ver | grep DISTRIB_ID) ]]; then
    lsb_release -si
else
    echo $ver | cut -d ' ' -f 1 | sort -u | head -1
fi
frogstarr78
  • 1,002
1

If you are not afraid of additional dependencies, you can use facter for that. It gives information about distro name and version even without lsb_release installed.

rvs
  • 1,663