How to find Linux distribution name using shell script?

I am writing a shell script in which I need the name of the current operating system to make it common. How:

if [ $Operating_System == "CentOS" ] then echo "CentOS"; # Do this elif [ $Operating_System == "Ubuntu" ] then echo "Ubuntu"; # Do that else echo "Unsupported Operating System"; fi 

How will this be possible? Using regex in lsb_release -a command or something else?

Thanks..

+5
source share
7 answers
 $ lsb_release -i Distributor ID: Fedora $ lsb_release -i | cut -f 2- Fedora 
+7
source

You can get information from lsb_release :

 echo "$(lsb_release -is)" 

i stands for distributor identifier.

s means short.

For example, It shows Ubuntu instead of Distributor Id: Ubuntu

There are other options:

-r: release

-d: description

-c: codeename

-a: all

You can get this information by running lsb_release --help or man lsb_release

+3
source

try the following:

 awk '/^ID=/' /etc/*-release | awk -F'=' '{ print tolower($2) }' 
+1
source
 DISTRO=$( cat /etc/*-release | tr [:upper:] [:lower:] | grep -Poi '(debian|ubuntu|red hat|centos|nameyourdistro)' | uniq ) if [ -z $DISTRO ]; then DISTRO='unknown' fi echo "Detected Linux distribution: $DISTRO" 
+1
source

For almost all linux distributions, cat /etc/issue will do the trick.

Edit: Obviously, no solution can be applied to all distributions, as distributions can do as they see fit.

Further clarification: it is not guaranteed that it works - nothing happens - but, in my experience, this is the method that most often works. In fact, this is the only method that works sequentially ( lsb_release , which was mentioned here, often produces command not found ).

0
source

Here is what I get:

 #!/bin/bash dist=$(tr -s ' \011' '\012' < /etc/issue | head -n 1) check_arch=$(uname -m) echo "[$green+$txtrst] Distribution Name: $dist" 
0
source

I would use uname -a

 robert@debian :/tmp$ uname -a Linux debian 3.2.0-4-686-pae #1 SMP Debian 3.2.65-1+deb7u2 i686 GNU/Linux 
-3
source

All Articles