Bewertung: 3 / 5

Stern aktivStern aktivStern aktivStern inaktivStern inaktiv
I just faced an issue I thought should be easy to solve. I wanted to know which Linux distribution a bash script runs on. There exists a proposal of the FSB (Free Standards Group) to implement lsb_release which delivers all the info.Unfortunately it's not implemented by all Linux distributions so I installed various distributions on VMWare to get a clue how to write code to extract the Linux distribution. Finally I wrote a bash script and python script to extract the Linux distribution.


lsb_release would be the easiest way to find out the Linux distribution. According FSG there can exist two files on a Linux distribution:
1) /etc/lsb-release
2) /etc/distribution-release
Unfortunately lsb_release is not available on all distributions and the files in /etc are not following the rules. It seems all naming variations of the config file can be found with 
ls /etc/*[-_]{release,version} 


Finally I wrote following bash script and this python script to use all information available to derive the Linux distribution. It either uses lsb_release to find out the distro or extracts the distro name from the existing /etc/*[-_]{release,version} file by using the string starting after /etc/ and ending with - or _. Example: /etc/fedora-release exists => distro is fedora
The script was tested on a lot of distributions but will also work on most other distributions. Pls let me know if you run it successfully on another Linux distro and I update the following list of tested distros:
# opensuse             openSuSE 11.0 (no lsb_release) and 11.2 (lsb_release)
# fedora                    Fedora 12
# centos                   CentOS 5.4
# kubuntu                 Kubuntu 9.10
# debian                   Debian 5.0.3
# arch                        Arch
# slackware             Slackware
# mandriva               Mandriva 2009.1
# debian                   Knoppix 6.2
# linuxmint               Mint 8
For the most recent list of tested distributions see header comment in source code.
Welche Distribution besitze ich (Which distribution do I have) : List of files in /etc/ directory used by various Linux distributions
lsb_release man page :  Man page of the Free Standards Group
/etc/release equivalents for sundry Linux (and other Unix) distributions : List of files in /etc/ directory used by various Linux distributions
Other implementations:
  • tisu suggested following single liner which is a codesnipet from the
DISTRO=`grep -ihs "buntu\|SUSE\|Fedora\|PCLinuxOS\|MEPIS\|Mandriva\|Debian\|Damn\|Sabayon\|Slackware\|KNOPPIX\|Gentoo\|Zenwalk\|Mint\|Kubuntu\|FreeBSD\|Puppy\|Freespire\|Vector\|Dreamlinux\|CentOS\|Arch\|Xandros\|Elive\|SLAX\|Red\|BSD\|KANOTIX\|Nexenta\|Foresight\|GeeXboX\|Frugalware\|64\|SystemRescue\|Novell\|Solaris\|BackTrack\|KateOS\|Pardus" /etc/{issue,*release,*version}` 
  • inxi contains a function which extracts the distro
Kommentar schreiben

*** Hinweis ***

Kommentare sind erwünscht. Aber um lästige Spamposts abweisen zu können gibt es ein paar Dinge die zu beachten sind:
  1. Kommentare mit dem Text http werden sofort zurückgewiesen mit der Meldung Sie sind nicht berechtigt den Tag zu verwenden. zz
  2. Kommentare werden manuell überprüft und es dauert deshalb in der Regel einen Tag bis sie veröffentlicht werden.

    #2 framp 2011-12-28 16:18
    You are right. But the main purpose of this script is to use a fallback mechanism if there is no lsb_release available. If it's available lsb_release is used. If every distro will install lsb_release per default this script will become obsolete.
    #1 immarvin 2011-12-28 09:07
    actually the lsb_release command has covered ur work.if u look into the lsb_release script, u can find that it get the distribution and version by reading /etc/*-[version|issue|release] file.unfortunately,the lsb is not the default base installation option for some linux distribution.