因故需獲取GNU/Linux系統的系統版本信息,但對於同一發行版,其各個Release版本的獲取方式不盡相同,例如CentOS。執行rpm -q --qf可以獲取 centos-release/etc/*-release文件中的內容,通過設置--qfflag 可提取需要的數據。

GNU/Linux发行版包含RHEL/CentOSDebianUbuntu

Preparation

各個GNU/Linux發行版操作系統環境由Docker創建。Docker的介紹與安裝可參考本人之前Blog Docker Instroduction and Installation on CentOS 7,此處不贅述。

Docker Hub地址爲 https://hub.docker.com/,以下爲各發行版的官方Image:

Distribution Docker Hub
CentOS https://hub.docker.com/_/centos/
Debian https://hub.docker.com/_/debian/
Ubuntu https://hub.docker.com/_/ubuntu/
[flying@lempstacker ~]$ docker images | awk 'match($1,/centos|debian|ubuntu|opensuse|debian/){print $1,$2}' | sort
centos 5
centos 6.8
centos 7
debian jessie
debian squeeze
debian wheezy
opensuse 13.2
opensuse latest
ubuntu precise
ubuntu trusty
ubuntu xenial
ubuntu yakkety
ubuntu zesty
[flying@lempstacker ~]$

運行容器命令示例(其中的參數此處不做解釋)

#centos:7
docker run --rm -ti -h centos7 centos:7 bash

#debian:jessie
docker run --rm -ti -h jessie debian:jessie bash

CentOS

此處列出的版本有:7.2.15116.86.76.65.11

在最小化安裝的操作系統中,默認沒有hostnamectllsb_release等命令,故而要獲取詳細的操作系統信息,較穩妥的方式是通過/etc/*-release文件提取。但相關文件在不同的版本中未必存在,現列出如下:

file 7 6.8 6.7 6.6 5
/etc/centos-release Yes Yes Yes Yes No
/etc/os-release Yes No No No No
/etc/redhat-release Yes Yes Yes Yes Yes
/etc/system-release Yes Yes Yes Yes No

5個版本同時存在的文件是/etc/redhat-release

通過命令

rpm -q -f /etc/*-release

知這些文件輸出相同內容

#CentOS Linux release 7.2.1511 (Core)
centos-release-7-2.1511.el7.centos.2.10.x86_64

#CentOS release 6.8 (Final)
centos-release-6-8.el6.centos.12.3.x86_64

#CentOS release 6.7 (Final)
centos-release-6-7.el6.centos.12.3.x86_64

#CentOS release 6.6 (Final)
centos-release-6-6.el6.centos.12.2.x86_64

#CentOS release 5.11 (Final)
centos-release-5-11.el5.centos

通過ls -lh命令查看

#CentOS Linux release 7.2.1511 (Core)
[root@centos7 /]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@centos7 /]# ls -lh /etc/*-release
-rw-r--r-- 1 root root  38 Dec  9  2015 /etc/centos-release
-rw-r--r-- 1 root root 393 Dec  9  2015 /etc/os-release
lrwxrwxrwx 1 root root  14 Sep  6 13:59 /etc/redhat-release -> centos-release
lrwxrwxrwx 1 root root  14 Sep  6 13:59 /etc/system-release -> centos-release
[root@centos7 /]#


#CentOS release 6.8 (Final)
[root@centos68 /]# cat /etc/redhat-release
CentOS release 6.8 (Final)
[root@centos68 /]# ls -lh /etc/*-release
-rw-r--r-- 1 root root 27 May 18 19:47 /etc/centos-release
lrwxrwxrwx 1 root root 14 Jun  2 13:43 /etc/redhat-release -> centos-release
lrwxrwxrwx 1 root root 14 Jun  2 13:43 /etc/system-release -> centos-release
[root@centos68 /]#


#CentOS release 6.7 (Final)
[root@centos67 /]# cat /etc/redhat-release
CentOS release 6.7 (Final)
[root@centos67 /]# ls -lh /etc/*release
-rw-r--r-- 1 root root 27 Aug  3  2015 /etc/centos-release
lrwxrwxrwx 1 root root 14 Aug 19  2015 /etc/redhat-release -> centos-release
lrwxrwxrwx 1 root root 14 Aug 19  2015 /etc/system-release -> centos-release
[root@centos67 /]#

#CentOS release 6.6 (Final)
[root@centos66 /]# cat /etc/redhat-release
CentOS release 6.6 (Final)
[root@centos66 /]# ls -lh /etc/*-release
-rw-r--r-- 1 root root 27 Oct 23  2014 /etc/centos-release
lrwxrwxrwx 1 root root 14 Mar  4  2015 /etc/redhat-release -> centos-release
lrwxrwxrwx 1 root root 14 Mar  4  2015 /etc/system-release -> centos-release
[root@centos66 /]#


#CentOS release 5.11 (Final)
[root@centos5 /]# cat /etc/redhat-release
CentOS release 5.11 (Final)
[root@centos5 /]# ls -lh /etc/*-release
-rw-r--r-- 1 root root 28 Sep 19  2014 /etc/redhat-release
[root@centos5 /]#

可看到 5 之後的版本增加了文件/etc/centos-release/etc/system-release,而文件redhat-releasesystem-release變成 centos-release 的符號鏈接。

7 中增加了文件/etc/os-release,此文件在Debian/Ubunut發行版的各個版本中皆存在。

rpm –qf

通過rpm -q --qf可以獲取 centos-release/etc/*-release文件中的內容,參數--qfflag 可通過命令

yum info centos-release

查看

#CentOS Linux release 7.2.1511 (Core)
Installed Packages
Name        : centos-release
Arch        : x86_64
Version     : 7
Release     : 2.1511.el7.centos.2.10
Size        : 35 k
Repo        : installed
From repo   : CentOS
Summary     : CentOS Linux release file
License     : GPLv2
Description : CentOS Linux release files


#CentOS release 6.8 (Final)
Installed Packages
Name        : centos-release
Arch        : x86_64
Version     : 6
Release     : 8.el6.centos.12.3
Size        : 35 k
Repo        : installed
From repo   : CentOS
Summary     : CentOS release file
License     : GPLv2
Description : CentOS release files


#CentOS release 6.7 (Final)
Installed Packages
Name        : centos-release
Arch        : x86_64
Version     : 6
Release     : 7.el6.centos.12.3
Size        : 35 k
Repo        : installed
Summary     : CentOS release file
License     : GPLv2
Description : CentOS release files


#CentOS release 6.6 (Final)
Installed Packages
Name        : centos-release
Arch        : x86_64
Version     : 6
Release     : 6.el6.centos.12.2
Size        : 34 k
Repo        : installed
From repo   : CentOS
Summary     : CentOS release file
License     : GPLv2
Description : CentOS release files


#CentOS release 5.11 (Final)
Installed Packages
Name       : centos-release
Arch       : x86_64
Epoch      : 10
Version    : 5
Release    : 11.el5.centos
Size       : 45 k
Repo       : installed
Summary    : CentOS release file
License    : GPL
Description: CentOS release files

Concrete Extract Command

通過如下命令提取需要的參數值

#release-info
rpm -q -f /etc/centos-release     #centos-release-7-2.1511.el7.centos.2.10.x86_64
rpm -q -f /etc/redhat-release     #centos-release-7-2.1511.el7.centos.2.10.x86_64

#Name
rpm -q --qf "%{name}" -f /etc/redhat-release    #centos-release

#Arch
rpm -q --qf "%{arch}" -f /etc/redhat-release    #x86_64

#Version
rpm -q --qf "%{version}" -f /etc/redhat-release     #7

#Release
rpm -q --qf "%{release}" -f /etc/redhat-release     #2.1511.el7.centos.2.10

#Size
rpm -q --qf "%{size}" -f /etc/redhat-release        #36019

#License
rpm -q --qf "%{license}" -f /etc/redhat-release     #GPLv2

#Summary
rpm -q --qf "%{summary}" -f /etc/redhat-release     #CentOS Linux release file

#Description
rpm -q --qf "%{summary}" -f /etc/redhat-release     #CentOS Linux release file

Via Shell Script

通過Shell腳本進行測試

#!/bin/bash
#Wirter: lempstacker
#Blog: https://lempstacker.com/
#Date: 2016.10.18 17:10 Tue Asia/Shanghai
#Note: Just Used For RedHat/CentOS Distribution

# font color
c_red='\e[31;1m'
c_end='\e[0m'

# check file exists or not

# if [[ -f /etc/os-release ]]; then
#     release_file='/etc/os-release'
# elif [[ -f /etc/centos-release ]]; then
#     release_file='/etc/centos-release'
# elif [[ -f /etc/redhat-release ]]; then
#     release_file='/etc/redhat-release'
# fi

[[ -f /etc/centos-release ]] && release_file='/etc/centos-release' || release_file='/etc/redhat-release'


#distrubution
distro=$(cat "$release_file")

#release info
releaseInfo=$(rpm -q -f "$release_file")

#Name
name=$(rpm -q --qf "%{name}" -f "$release_file")

#Arch
arch=$(rpm -q --qf "%{arch}" -f "$release_file")

#Version
version=$(rpm -q --qf "%{version}" -f "$release_file")

#Release
release=$(rpm -q --qf "%{release}" -f "$release_file")

#Size
size=$(rpm -q --qf "%{size}" -f "$release_file")

#License
license=$(rpm -q --qf "%{license}" -f "$release_file")

#Summary
summary=$(rpm -q --qf "%{summary}" -f "$release_file")

#Description
description=$(rpm -q --qf "%{description}" -f "$release_file")


#Output
len=15
printf "%"$len"s $c_red%s$c_end\n" 'Distro:' "$distro"
printf "%"$len"s $c_red%s$c_end\n" 'Name:' "$name"
printf "%"$len"s $c_red%s$c_end\n" 'Arch:' "$arch"
printf "%"$len"s $c_red%s$c_end\n" 'Version:' "$version"
printf "%"$len"s $c_red%s$c_end\n" 'Release:' "$release"
printf "%"$len"s $c_red%s$c_end\n" 'Size:' "$size"
printf "%"$len"s $c_red%s$c_end\n" 'License:' "$license"
printf "%"$len"s $c_red%s$c_end\n" 'Summary:' "$summary"
printf "%"$len"s $c_red%s$c_end\n" 'Description:' "$description"

#Script End

執行結果如下

#CentOS Linux release 7.2.1511 (Core)
[root@centos7 /]# bash /tmp/distro.sh
      Distro: CentOS Linux release 7.2.1511 (Core)
        Name: centos-release
        Arch: x86_64
     Version: 7
     Release: 2.1511.el7.centos.2.10
        Size: 36019
     License: GPLv2
     Summary: CentOS Linux release file
 Description: CentOS Linux release files
[root@centos7 /]#


#CentOS release 6.8 (Final)
[root@centos68 /]# bash /tmp/distro.sh
      Distro: CentOS release 6.8 (Final)
        Name: centos-release
        Arch: x86_64
     Version: 6
     Release: 8.el6.centos.12.3
        Size: 35634
     License: GPLv2
     Summary: CentOS release file
 Description: CentOS release files
[root@centos68 /]#


#CentOS release 6.7 (Final)
[root@centos67 /]# bash /tmp/distro.sh
      Distro: CentOS release 6.7 (Final)
        Name: centos-release
        Arch: x86_64
     Version: 6
     Release: 7.el6.centos.12.3
        Size: 35634
     License: GPLv2
     Summary: CentOS release file
 Description: CentOS release files
[root@centos67 /]#


#CentOS release 6.6 (Final)
[root@centos66 /]# bash /tmp/distro.sh
      Distro: CentOS release 6.6 (Final)
        Name: centos-release
        Arch: x86_64
     Version: 6
     Release: 6.el6.centos.12.2
        Size: 34769
     License: GPLv2
     Summary: CentOS release file
 Description: CentOS release files
[root@centos66 /]#


#CentOS release 5.11 (Final)
[root@centos5 /]# bash /tmp/distro.sh
      Distro: CentOS release 5.11 (Final)
        Name: centos-release
        Arch: x86_64
     Version: 5
     Release: 11.el5.centos
        Size: 45731
     License: GPL
     Summary: CentOS release file
 Description: CentOS release files
[root@centos5 /]#

Other Ways

#系統版本詳情
hostnamectl | awk -v FS=": " '$0~/Operating System/{print $2}'      #CentOS Linux 7 (Core)
sed -r -n 's@"@@g;s@^PRETTY_NAME=(.*)@\1@p' /etc/os-release       #CentOS Linux 7 (Core)

#GNU/Linux發行版
hostnamectl | awk -v FS=" " '$0~/Operating System/{print $3}'       #CentOS
sed -r -n 's@"@@g;s@^NAME=(.*)@\1@p' /etc/os-release     #CentOS Linux
sed -r -n 's@"@@g;s@^ID=(.*)@\1@p' /etc/os-release      #centos

#系統版本名稱
sed -r -n 's@^VERSION="(.*)"@\1@p' /etc/os-release      #7 (Core)

#具體Release版本號
sed -r -n 's@"@@g;s@^VERSION_ID=(.*)@\1@p' /etc/os-release        #7

Debian

DebianUbuntu的各Release版本 * 都含有文件/etc/os-release,通過命令sed可提取相關參數值; * 都含有文件/etc/debian_versionDebian 中顯示的是Release號,Ubuntu 中顯示的則是stretch/sid;

#Debian jessie
root@jessie:/# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 8 (jessie)"
NAME="Debian GNU/Linux"
VERSION_ID="8"
VERSION="8 (jessie)"
ID=debian
HOME_URL="http://www.debian.org/"
SUPPORT_URL="http://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
root@jessie:/#

#Debian wheezy
root@wheezy:/# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 7 (wheezy)"
NAME="Debian GNU/Linux"
VERSION_ID="7"
VERSION="7 (wheezy)"
ID=debian
ANSI_COLOR="1;31"
HOME_URL="http://www.debian.org/"
SUPPORT_URL="http://www.debian.org/support/"
BUG_REPORT_URL="http://bugs.debian.org/"
root@wheezy:/#

#Ubuntu yakkety
root@yakkety:/# cat /etc/os-release
NAME="Ubuntu"
VERSION="16.10 (Yakkety Yak)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.10"
VERSION_ID="16.10"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="http://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=yakkety
UBUNTU_CODENAME=yakkety
root@yakkety:/#

#Ubuntu xenial
root@xenial:/# cat /etc/os-release
NAME="Ubuntu"
VERSION="16.04.1 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.1 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
UBUNTU_CODENAME=xenial
root@xenial:/#

#Ubuntu trusty
root@trusty:/# cat /etc/os-release
NAME="Ubuntu"
VERSION="14.04.5 LTS, Trusty Tahr"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 14.04.5 LTS"
VERSION_ID="14.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
root@trusty:/#

#Ubuntu precise
root@precise:/# cat /etc/debian_version
wheezy/sid
root@precise:/# cat /etc/os-release
NAME="Ubuntu"
VERSION="12.04.5 LTS, Precise Pangolin"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu precise (12.04.5 LTS)"
VERSION_ID="12.04"
root@precise:/#

Concrete Extract Command

通過如下命令提取需要的參數值


#PRETTY_NAME
sed -r -n 's@"@@g;s@^PRETTY_NAME=(.*)@\1@p' /etc/os-release     #Debian GNU/Linux 8 (jessie)

#NAME
sed -r -n 's@"@@g;s@^NAME=(.*)@\1@p' /etc/os-release     #Debian GNU/Linux

#VERSION_ID
sed -r -n 's@"@@g;s@^VERSION_ID=(.*)@\1@p' /etc/os-release        #8

#VERSION
sed -r -n 's@^VERSION="(.*)"@\1@p' /etc/os-release      #8 (jessie)

#ID
sed -r -n 's@"@@g;s@^ID=(.*)@\1@p' /etc/os-release      #debian

#For Ubuntu
#ID_LIKE
sed -r -n 's@"@@g;s@^ID_LIKE=(.*)@\1@p' /etc/os-release      #debian

Via Shell Script

通過Shell腳本進行測試

#!/bin/bash
#Wirter: lempstacker
#Blog: https://lempstacker.com/
#Date: 2016.10.18 17:50 Tue Asia/Shanghai
#Note: Just Used For Debian/Ubuntu Distribution

# font color
c_red='\e[31;1m'
c_end='\e[0m'

# check file exists or not
release_file='/etc/os-release'

#PRETTY_NAME
pretty_name=$(sed -r -n 's@"@@g;s@^PRETTY_NAME=(.*)@\1@p' "$release_file")

#NAME
name=$(sed -r -n 's@"@@g;s@^NAME=(.*)@\1@p' "$release_file")

#VERSION_ID
version_id=$(sed -r -n 's@"@@g;s@^VERSION_ID=(.*)@\1@p' "$release_file")

#VERSION
version=$(sed -r -n 's@"@@g;s@^VERSION=(.*)@\1@p' "$release_file")

#ID
id=$(sed -r -n 's@"@@g;s@^ID=(.*)@\1@p' "$release_file")

#For Ubuntu
#ID_LIKE
id_like=$(sed -r -n 's@"@@g;s@^ID_LIKE=(.*)@\1@p' "$release_file")

#Output
len=12
printf "%"$len"s $c_red%s$c_end\n" 'PRETTY_NAME:' "$pretty_name"
printf "%"$len"s $c_red%s$c_end\n" 'NAME:' "$name"
printf "%"$len"s $c_red%s$c_end\n" 'VERSION_ID:' "$version_id"
printf "%"$len"s $c_red%s$c_end\n" 'VERSION:' "$version"
printf "%"$len"s $c_red%s$c_end\n" 'ID:' "$id"
printf "%"$len"s $c_red%s$c_end\n" 'ID_LIKE:' "$id_like"

#Script End

執行結果如下

#Debian jessie
root@jessie:~# bash /tmp/distro.sh
PRETTY_NAME: Debian GNU/Linux 8 (jessie)
       NAME: Debian GNU/Linux
 VERSION_ID: 8
    VERSION: 8 (jessie)
         ID: debian
    ID_LIKE:
root@jessie:~#


#Debian wheezy
root@wheezy:/# bash /tmp/distro.sh
PRETTY_NAME: Debian GNU/Linux 7 (wheezy)
       NAME: Debian GNU/Linux
 VERSION_ID: 7
    VERSION: 7 (wheezy)
         ID: debian
    ID_LIKE:
root@wheezy:/#


#Ubuntu yakkety
root@yakkety:/# bash /tmp/distro.sh
PRETTY_NAME: Ubuntu 16.10
       NAME: Ubuntu
 VERSION_ID: 16.10
    VERSION: 16.10 (Yakkety Yak)
         ID: ubuntu
    ID_LIKE: debian
root@yakkety:/#


#Ubuntu xenial
root@xenial:/# bash /tmp/distro.sh
PRETTY_NAME: Ubuntu 16.04.1 LTS
       NAME: Ubuntu
 VERSION_ID: 16.04
    VERSION: 16.04.1 LTS (Xenial Xerus)
         ID: ubuntu
    ID_LIKE: debian
root@xenial:/#


#Ubuntu trusty
root@trusty:/# bash /tmp/distro.sh
PRETTY_NAME: Ubuntu 14.04.5 LTS
       NAME: Ubuntu
 VERSION_ID: 14.04
    VERSION: 14.04.5 LTS, Trusty Tahr
         ID: ubuntu
    ID_LIKE: debian
root@trusty:/#


#Ubuntu precise
root@precise:/# bash /tmp/distro.sh
PRETTY_NAME: Ubuntu precise (12.04.5 LTS)
       NAME: Ubuntu
 VERSION_ID: 12.04
    VERSION: 12.04.5 LTS, Precise Pangolin
         ID: ubuntu
    ID_LIKE: debian
root@precise:/#

Other Ways

#系統版本詳情
hostnamectl | awk -v FS=": " '$0~/Operating System/{print $2}'      #Debian GNU/Linux 8 (jessie)

#GNU/Linux發行版
hostnamectl | awk -v FS=" " '$0~/Operating System/{print $3}'       #Debian

Extract OS&OSRELEASE Via Shell Script

此腳本用於提取系統發行版、版本號等信息,可通用於RedHat和Debian系。

腳本下載 GitHub

#!/bin/env bash
#Target: Detection GNU/Linux Distribution Info
#Note: Used For RedHat/CentOS/Fedora, Debian/Ubuntu, SLES/OpenSUSE Distribution
#Writer: LempStacker
#Date: Oct 19, 2016 10:45 Wed Asia/Shanghai
#Reconfiguration Date: Feb 23, 2017 14:50~17:01 Asia/Shanghai

printf "Usage: Check Operation System Info On GNU/Linux!\n"

#Docker Script https://get.docker.com/

# ID - distroName - ubuntu
# ID_LIKE - familyName - debian
# VERSION_ID - distroVersionID - 16.10
# PRETTY_NAME(Deian/OpenSUSE) / Version(Ubuntu) - distroFullName - 16.10 (Yakkety Yak)

# sles - SUSE Linux Enterprise Server

########  0. Initialization Setting  ########
c_red='\e[31;1m'    # font color
c_blue='\e[34m'     # font color
c_end='\e[0m'       # font color
len=16              # printf str width


########  1. Logic Processing  ########

# CentOS 5, CentOS 6, Debian 6 has no file /etc/os-release
if [[ -f /etc/os-release ]]; then
    releaseFile='/etc/os-release'
    distroName=$(sed -r -n '/^ID=/{s@"@@g;s@.*=(.*)@\1@p}' "$releaseFile")     #debian ubuntu opensuse sles
    distroVersionID=$(sed -r -n '/^VERSION_ID=/{s@"@@g;s@.*=(.*)@\1@p}' "$releaseFile")    #8 16.10

    [[ -f /etc/redhat-release ]] && distroFullName=$(cat /etc/redhat-release) || distroFullName=$(sed -r -n '/^PRETTY_NAME=/{s@"@@g;s@.*=(.*)@\1@p}' "$releaseFile")

    # Debian,SUSE無參數 ID_LIKE, 只有ID
    if [[ -n $(sed -n '/^ID_LIKE=/p' "$releaseFile") ]]; then
        familyName=$(sed -r -n '/^ID_LIKE=/{s@"@@g;s@.*=(.*)@\1@p}' "$releaseFile")
    else
        familyName="$distroName"
    fi  #End if

    # GNU/Linux發行版官網
    [[ -n $(sed -n '/^HOME_URL=/p' "$releaseFile") ]] && officialSite=$(sed -r -n '/^HOME_URL=/{s@"@@g;s@.*=(.*)@\1@p}' "$releaseFile") || officialSite=''

elif [[ -f /etc/redhat-release ]]; then
    releaseFile='/etc/redhat-release'
    distroName=$(rpm -q --qf "%{name}" -f "$releaseFile")   # centos-release, fedora-release
    distroName=${distroName%%-*}    # centos, fedora
    distroFullName=$(cat "$releaseFile")    #CentOS Linux release 7.3.1611 (Core)
    distroVersionID=$(sed -r -n 's@.* ([[:digit:].]*) .*@\1@p' "$releaseFile") # 5.11, 6.8, 7.3.1611
    familyName='RedHat'   # family is rhel (RedHat)
    officialSite=''

elif [[ -f /etc/debian_version ]]; then
    # Debian 6
    releaseFile='/etc/debian_version'
    distroVersionID=$(cat "$releaseFile")
    if [[ -f /etc/issue.net ]]; then
        distroFullName=$(cat /etc/issue.net) #Debian GNU/Linux 6.0
        distroName=${distroFullName%% *}
        distroName=${distroName,,}
    fi
    familyName='debian'
    officialSite=''

else
    printf "Sorry, Fail To Detect OS Info!\n" "$osReleasePretty"
    unset c_red
    unset c_blue
    unset c_end
    unset len
    exit 1
fi

case "$familyName" in
    debian )
        familyName='Debian' #Ubuntu從12.04開始皆有ID_LIKE
        ;;
    suse|opensuse )
        familyName='SUSE'
        ;;
    rhel|"rhel fedora"|fedora|centos )
        familyName='RedHat'
        ;;
esac

printf "%"$len"s $c_red%s$c_end\n" 'distroFullName:' "$distroFullName"
printf "%"$len"s $c_red%s$c_end\n" 'distroName:' "$distroName"
printf "%"$len"s $c_red%s$c_end\n" 'distroVersion:' "$distroVersionID"
printf "%"$len"s $c_red%s$c_end\n" 'familyName:' "$familyName"
[[ -n "$officialSite" ]] && printf "%"$len"s $c_red%s$c_end\n" 'officialSite:' "$officialSite"


########  7.Unset Viriables & Remove Temp File  ########
unset c_red
unset c_blue
unset c_end
unset len
unset releaseFile
unset distroFullName
unset distroName
unset distroVersionID
unset familyName
unset officialSite

#Script End

執行結果如下

#Red Hat Enterprise Linux Server release 6.3 (Santiago)
bash-4.1# bash /tmp/distro.sh
 distroFullName: Red Hat Enterprise Linux Server release 6.3 (Santiago)
     distroName: redhat
  distroVersion: 6
bash-4.1#

#Red Hat Enterprise Linux Server release 6.0 (Santiago)
[root@redhat60 ~]# bash /tmp/distro.sh
 distroFullName: Red Hat Enterprise Linux Server release 6.0 (Santiago)
     distroName: redhat
  distroVersion: 6
[root@redhat60 ~]#

#CentOS Linux release 7.2.1511 (Core)
[root@centos7 /]# bash /tmp/distro.sh
 distroFullName: CentOS Linux release 7.2.1511 (Core)
     distroName: centos
  distroVersion: 7
[root@centos7 /]#

#CentOS release 6.8 (Final)
[root@centos6 /]# bash /tmp/distro.sh
 distroFullName: CentOS release 6.8 (Final)
     distroName: centos
  distroVersion: 6
[root@centos6 /]#

#Fedora release 24 (Twenty Four)
[root@fedora24 /]# bash /tmp/distro.sh
 distroFullName: Fedora release 24 (Twenty Four)
     distroName: fedora
  distroVersion: 24
[root@fedora24 /]#

#Debian jessie
root@jessie:/# bash /tmp/distro.sh
 distroFullName: Debian GNU/Linux 8 (jessie)
     distroName: debian
  distroVersion: 8
root@jessie:/#

#Ubuntu yakkety
root@yakkety:/# bash /tmp/distro.sh
 distroFullName: Ubuntu 16.10
     distroName: ubuntu
  distroVersion: 16.10
root@yakkety:/#

References

Change Logs

  • 2016.10.18 17:58 Tue Asia/Shanghai
    • 初稿完成
  • 2016.10.19 10:53 Wed Asia/Shanghai
    • 添加Extract OS&OSRELEASE Via Shell Script通用腳本
  • 2017.02.23 17:23 Thu Asia/Shanghai
    • Shell Script重構,只更新Script部分,其餘內容未作更改

  • Note Time: 2016.10.18 17:58 Tue
  • Note Location: Asia/Shanghai
  • Writer: lempstacker