问题
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..
回答1:
$ lsb_release -i
Distributor ID: Fedora
$ lsb_release -i | cut -f 2-
Fedora
回答2:
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
回答3:
try this one:
awk '/^ID=/' /etc/*-release | awk -F'=' '{ print tolower($2) }'
回答4:
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"
回答5:
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
).
回答6:
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"
回答7:
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
来源:https://stackoverflow.com/questions/29581754/how-to-find-linux-distribution-name-using-shell-script