[toc]
shell脚本判断系统类型
1.场景说明
在生产环境中服务器的系统类型可能会有多种,例如有CentOS、Ubuntu、Debian等等,在做批量操作(如shell脚本、ansible等)的时候就需要对服务器等系统类型做判断,然后执行不同的命令,例如执行安装命令,CentOS系统执行 yum
命令,Ubuntu、Debian执行 apt
命令
2.判断方法
这里以常用的Ubuntu和CentOS为例
2.1 CentOS
方法一 查看文件
/etc/redhat-release
$ cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
/etc/centos-release
$ cat /etc/centos-release
CentOS Linux release 7.8.2003 (Core)
/etc/issue
# CentOS6中会显示具体版本,CentOS7中显示如下
$ cat /etc/issue
\S
Kernel \r on an \m
/etc/os-release
$ cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
方法二 通过命令
命令 lsb_release
,通过 yum -y install redhat-lsb-core
安装
$ lsb_release -a
LSB Version: :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.8.2003 (Core)
Release: 7.8.2003
Codename: Core