http://www.linux-tips-and-tricks.de

How to find out which Linux distribution a bash script runs on PDF Print E-mail
I just faced a problem I thought should be easy to solve. I want 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 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
 
But 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 following regular expression:
ls /etc/*[-_]{release,version} 
 
Finally  I wrote following script. 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
 
It 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 13.0.0.0.0
# mandriva               Mandriva 2009.1
# debian                   Knoppix 6.2
# linuxmint                Mint 8
 
The the actual list of tested distributions see header comment in source code.
 
References:
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 alsa-info.sh:
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}`
 
 
 
Last Updated ( Mittwoch, 10 Februar 2010 )
 
< Prev   Next >