如何借助Bash脚本知道运行的平台是Ubuntu还是CentOS?


39

我知道用于检查在我的机器上运行的Linux机器名称的命令。例如:

的Ubuntu

cat /etc/version

CentOS的

cat /etc/issue

如何从终端获取输出并进行比较以查看它是UBUNTU还是CENTOS,然后执行以下命令?

apt-get install updates 

要么

yum update

Ubuntu 14.04

cat /etc/issue

2
欢迎来到Ask Ubuntu。拜托,您能抽出一些时间阅读某人回答我的问题该怎么办?
西尔文·皮诺

Answers:


53

不幸的是,没有确定版本的简单方法。大多数主要的发行版正朝着他们使用一个系统/etc/os-release来存储这些信息。大多数现代发行版还包含这些lsb_release工具,但默认情况下并不总是安装这些工具。因此,这里有一些可以使用的方法:

  1. 采用 /etc/os-release

    awk -F= '/^NAME/{print $2}' /etc/os-release
  2. 使用lsb_release工具(如果有)

    lsb_release -d | awk -F"\t" '{print $2}'
  3. 使用应该适用于大多数发行版的更复杂的脚本:

    # Determine OS platform
    UNAME=$(uname | tr "[:upper:]" "[:lower:]")
    # If Linux, try to determine specific distribution
    if [ "$UNAME" == "linux" ]; then
        # If available, use LSB to identify distribution
        if [ -f /etc/lsb-release -o -d /etc/lsb-release.d ]; then
            export DISTRO=$(lsb_release -i | cut -d: -f2 | sed s/'^\t'//)
        # Otherwise, use release info file
        else
            export DISTRO=$(ls -d /etc/[A-Za-z]*[_-][rv]e[lr]* | grep -v "lsb" | cut -d'/' -f3 | cut -d'-' -f1 | cut -d'_' -f1)
        fi
    fi
    # For everything else (or if above failed), just use generic identifier
    [ "$DISTRO" == "" ] && export DISTRO=$UNAME
    unset UNAME
    
  4. 解析以下版本信息gcc

    CentOS 5.x

    $ gcc --version
    gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-54)
    Copyright (C) 2006 Free Software Foundation, Inc.
    

    CentOS 6.x版

    $ gcc --version
    gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-3)
    Copyright (C) 2010 Free Software Foundation, Inc.
    

    Ubuntu 12.04

    $ gcc --version
    gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
    Copyright (C) 2011 Free Software Foundation, Inc.
    

    Ubuntu 14.04

    $ gcc --version
    gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2
    Copyright (C) 2013 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    

这基本上是直接复制自@slm 在这里对我的问题的好答案


“不幸的是,没有确定名称的简单方法。” <— 事实并非如此。 例如,席尔文·皮诺Sylvain Pineau)展示了一种简单的单行代码,它通过遵循Python来可靠地查询当前平台类型-大多数平台(包括Ubuntu)上已预装了该代码。Shell脚本绝不应尝试使用此答案中实现的这类低级,非便携式测试来手动查询平台元数据。
塞西尔·库里

默认情况下,Debian中未安装Gawk。
Ville Laitila '18

@VilleLaitila恐怕与这里无关,因为此站点仅与Ubuntu有关。但是这里没有gawk特有的内容,因此awkmawk或者其他实现也将同样有效。
terdon

使用lsb_release以下方法更具体:lsb_release -d | awk -F"\t" '{print $2}' | awk -F " " '{print $1}'
NerdOfCode

@NerdOfCode如果您只需要发布号,请执行do lsb_release -d | awk '{print $3}'grep -oP 'VERSION="\K\S+' /etc/os-release,而无需双管awk。
terdon

25

您不需要bash来执行此类任务,并且我建议您使用高级方法来避免处理诸如/etc/version/etc/issue等文件(我在13.10上没有/ etc / version)。

所以我的建议是改用以下命令:

python -mplatform | grep -qi Ubuntu && sudo apt-get update || sudo yum update

python 平台模块将在两个系统上都可以工作,其余命令将检查python是否返回Ubuntu并运行apt-getelse yum


添加-igrep可能会有帮助。
coanor

8

这是一个简单的答案,我发现仅凭文件的存在即可在所有版本的Ubuntu / CentOS / RHEL上使用(当然,如果有人在您的Ubuntu盒中随机放置/ etc / redhat-release等,则不是故障保护):

if [ -f /etc/redhat-release ]; then
  yum update
fi

if [ -f /etc/lsb-release ]; then
  apt-get update
fi

在我的ubuntu上没有/ etc / lsb-release
noonex

3
我不会设为-1,但是在至少一种情况下,此答案不正确:/etc/lsb-releaseCloudLinux(CentOS)6.8上存在,因此它将同时返回yumapt-get
dhaupin

5

使用Chef完成这些任务。;-)

在Chef中,可以使用以下platform?方法:

if platform?("redhat", "centos", "fedora")
  # Code for only Red Hat Linux family systems.
end

要么:

if platform?("ubuntu")
  # Code for only Ubuntu systems
end

要么:

if platform?("ubuntu")
  # Do Ubuntu things
end

要么:

if platform?("freebsd", "openbsd")
  # Do BSD things
end

1
虽然致力于提供卓越的至低级别的外壳为中心的脆弱非解决方案(例如lsb_release/etc/os-release),该解决方案具有需要厨师的明显的缺点-一个重量级的基础设施即代码(IAC)的框架不是默认情况下在大多数平台上,并要求使用的安装它自己的特定领域语言(DSL)。总之,大多数用户可能只是想推迟到Python,这默认安装在大多数平台(包括Ubuntu)和接口以及与shell脚本。
塞西尔·库里

5

检查内核名称中的Ubuntu:

if [  -n "$(uname -a | grep Ubuntu)" ]; then
    sudo apt-get update && sudo apt-get upgrade 
else
    yum update
fi  

4
 apt-get -v &> /dev/null && apt-get update
 which yum &> /dev/null && yum update

如果只有两个发行版,则可以将其缩短:

apt-get -v &> /dev/null && apt-get update || yum update

yum -v在CentOS中以某种方式返回非零值,因此请which改用它,
当然,如果未which安装,则应考虑方案。


2

以下脚本应说明它是否是Ubuntu。如果不是,而您拥有的唯一其他选择是CentOS,则应在else子句中使用它:

dist=`grep DISTRIB_ID /etc/*-release | awk -F '=' '{print $2}'`

if [ "$dist" == "Ubuntu" ]; then
  echo "ubuntu"
else
  echo "not ubuntu"
fi

1
可以使用该lsb_release工具简化该过程,该工具应该读出相同的文件。lsb_release -i报告Distributor ID: UbuntuDistributor ID: CentOS在这些情况下。
慢性病

@chronitis:是的,可以肯定的是
Jobin

我认为Centos上默认不提供此功能
Sylvain Pineau 2014年

1

/etc/os-release在子shell中执行并回显其值:

if [ "$(. /etc/os-release; echo $NAME)" = "Ubuntu" ]; then
  apt-get install updates 
else
  yum update
fi

0

我会用python

if ! python -c "exec(\"import platform\nexit ('centos' not in platform.linux_distribution()[0].lower())\")" ; then
   echo "It is not CentOS distribution, ignoring CentOS setup"
   exit 0
fi

0

使用此命令可在CentOS,Ubuntu和Debian中使用: grep "^NAME=" /etc/os-release |cut -d "=" -f 2 | sed -e 's/^"//' -e 's/"$//'

在Debian中产生Debian GNU/Linux,在Ubuntu中产生Ubuntu,在CentOS中产生CentOS Linux

使用grep,cut和sed而不是gawk的好处很明显:Debian默认没有安装gawk,因此您不能依赖于随机的Debian盒。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.