Reputation: 3753
I am writing a shell script in which I need the current operating system name to make it generic. Like:
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 it be possible? Applying regular expression on lsb_release -a
command or something else?
Thanks..
Upvotes: 11
Views: 14230
Reputation: 2064
EDIT 2: As pointed out by @Jean-Michaël Celerier, some distros prefer to add double quotes.
This example...
awk -F'=' '/^ID=/ { gsub("\"","",$2); print tolower($2) }' /etc/*-release 2> /dev/null
... and this one ...
(awk -F'=' '/^ID=/ { print tolower($2) }' /etc/*-release | tr -d '"') 2> /dev/null
... can be used to remove them.
EDIT 1: as suggested by @S0AndS0, this is slightly better:
awk -F'=' '/^ID=/ {print tolower($2)}' /etc/*-release 2> /dev/null
try this one:
awk '/^ID=/' /etc/*-release | awk -F'=' '{ print tolower($2) }'
Upvotes: 4
Reputation: 1
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"
Upvotes: 0
Reputation: 4349
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"
Upvotes: 3
Reputation: 22428
You can get the info from lsb_release
:
echo "$(lsb_release -is)"
i
stands for distributor id.
s
stands for short.
For ex. It shows Ubuntu
instead of Distributor Id: Ubuntu
There are other options:
-r : release
-d : description
-c : codename
-a : all
You can get this information by running lsb_release --help
or man lsb_release
Upvotes: 8
Reputation: 934
I'd 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
Upvotes: -3
Reputation: 1636
For almost all linux distros, cat /etc/issue
will do the trick.
Edit: Obviously, no solution can apply to all distros, as distros are free to do as they please.
Further clarification: This is not guaranteed to work - nothing is - but in my experience, this is the method that most often works. Actually, it's the only method that works consistently (lsb_release
, which was mentioned here, often produces command not found
).
Upvotes: 1
Reputation: 798446
$ lsb_release -i
Distributor ID: Fedora
$ lsb_release -i | cut -f 2-
Fedora
Upvotes: 10