如何使用shell脚本查找Linux分发名称?

我正在编写一个shell脚本,我需要使用当前的操作系统名称来使其通用。 喜欢:

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

怎么可能? 在lsb_release -a命令或其他东西上应用正则expression式

谢谢..

 $ lsb_release -i Distributor ID: Fedora $ lsb_release -i | cut -f 2- Fedora 

您可以从lsb_release获取信息:

 echo "$(lsb_release -is)" 

i代表经销商身份证。

s代表短。

例如。 它显示了Ubuntu而不是Distributor Id: Ubuntu

还有其他的选择:

-r:发布

-d:描述

-c:代号

-a:全部

您可以通过运行lsb_release --helpman lsb_release来获取此信息

试试这个:

 awk '/^ID=/' /etc/*-release | awk -F'=' '{ print tolower($2) }' 

对于几乎所有的Linux发行版, cat /etc/issue都可以实现。

编辑:显然,没有解决方案可以适用于所有发行版,因为发行版可以随心所欲地做。

进一步澄清:这不能保证工作 – 什么都没有 – 但根据我的经验,这是最常用的方法。 实际上,这是一致的唯一方法( lsb_release ,在这里提到,经常产生command not found )。

 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" 

我会用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