顯然文件裡的KERNEL_NAME的值就是判斷完成的操作系統,具體分析該值是如何得到的。
就是通過命令“uname –s | tr ‘[a-z]’ ‘[A-Z]’得到的,不過作者在此處把小寫換成了大寫。 接下來分析根據不同操作系統,怎麼獲得不同的DISTRO值,代碼如下:
if [ X"${KERNEL_NAME}" == X'LINUX' ]; then
# Directory of RC scripts.
export DIR_RC_SCRIPTS='/etc/init.d'
if [ -f /etc/redhat-release ]; then
# RHEL/CentOS
export DISTRO='RHEL'
# Get distribution version
if grep '\ 6' /etc/redhat-release &>/dev/null; then
# version 6.x
export DISTRO_VERSION='6'
elif grep '\ 7' /etc/redhat-release &>/dev/null; then
# version 7.x
export DISTRO_VERSION='7'
else
export UNSUPPORTED_RELEASE='YES'
fi
# Get distribution name as DISTRO_CODENAME
if grep '^Red' /etc/redhat-release &>/dev/null; then
# RHEL
export DISTRO_CODENAME='rhel'
elif grep '^CentOS' /etc/redhat-release &>/dev/null; then
# CentOS
export DISTRO_CODENAME='centos'
elif grep '^Scientific' /etc/redhat-release &>/dev/null; then
# Scientific Linux
export DISTRO_CODENAME='scientific'
else
export UNSUPPORTED_RELEASE='YES'
fi
elif [ -f /etc/lsb-release ]; then
# Ubuntu
export DISTRO='UBUNTU'
# Ubuntu version number and code name:
# - 14.04: trusty
# - 15.04: vivid
export DISTRO_ID="$(grep 'DISTRIB_ID' /etc/lsb-release | awk -F'=' '{print $2}')"
export DISTRO_VERSION="$(grep 'DISTRIB_RELEASE' /etc/lsb-release | awk -F'=' '{print $2}')"
export DISTRO_CODENAME="$(grep 'DISTRIB_CODENAME' /etc/lsb-release | awk -F'=' '{print $2}')"
# Unsupported releases: 12.x, 13.x, 14.10
if echo "${DISTRO_VERSION}" | grep -E '^(12|13|14\.10)' &>/dev/null ; then
export UNSUPPORTED_RELEASE='YES'
fi
elif [ -f /etc/debian_version ]; then
# Debian
export DISTRO='DEBIAN'
# Get major release version number
export DISTRO_VERSION="$(cat /etc/debian_version)"
# Set distro code name and unsupported releases.
if grep '^7' /etc/debian_version &>/dev/null || \
grep -i '^wheezy' /etc/debian_version &>/dev/null; then
export DISTRO_VERSION='7'
export DISTRO_CODENAME='wheezy'
elif grep '^8' /etc/debian_version &>/dev/null || \
grep -i '^jessie' /etc/debian_version &>/dev/null; then
export DISTRO_VERSION='8'
export DISTRO_CODENAME='jessie'
else
export UNSUPPORTED_RELEASE='YES'
fi
# Override settings.
export SHELL_NOLOGIN='/usr/sbin/nologin'
else
export UNSUPPORTED_RELEASE='YES'
fi
elif [ X"${KERNEL_NAME}" == X'FREEBSD' ]; then
export DISTRO='FREEBSD'
export DISTRO_VERSION="$(uname -r |awk -F'[.-]' '{print $1}')"
# Directory of RC scripts.
export DIR_RC_SCRIPTS='/usr/local/etc/rc.d'
export PYTHON_BIN='/usr/local/bin/python'
# Unsupported releases: 7, 8.
if echo "${DISTRO_VERSION}" | grep '^[78]' &>/dev/null ; then
export UNSUPPORTED_RELEASE='YES'
fi
export SHELL_BASH='/usr/local/bin/bash'
# Default password scheme.
export DEFAULT_PASSWORD_SCHEME='BCRYPT'
elif [ X"${KERNEL_NAME}" == X'OPENBSD' ]; then
export DISTRO='OPENBSD'
export DISTRO_VERSION="$(uname -r)"
# Directory of RC scripts.
export DIR_RC_SCRIPTS='/etc/rc.d'
export RC_CONF_LOCAL='/etc/rc.conf.local'
export SHELL_BASH='/usr/local/bin/bash'
export PYTHON_BIN='/usr/local/bin/python'
# Unsupported release: 5.6 and earlier versions.
if echo "${DISTRO_VERSION}" | grep '^5.[123456]' &>/dev/null ; then
export UNSUPPORTED_RELEASE='YES'
fi
# Default password scheme.
export DEFAULT_PASSWORD_SCHEME='BCRYPT'
else
# Not support *BSD and other distrobutions yet.
echo "Error: Your OS is not supported yet."
exit 255
fi
代碼比較長,不過脈絡比較清晰,首先根據KERNEL_NAME的值判斷是哪種操作系統,作者給出的判斷是3種,分別是LINUX OPENBSD FREEBSD , 也就是該腳本只能在這3種平台上進行部署,如果需要自己擴展的話,就可以在增加新的判斷,最後如果3種都不是的話,就直接返回255,如下代碼:
echo "Error: Your OS is not supported yet."
exit 255
接下來分析當KERNEL_NAME的值為LINUX,腳本做了什麼,首先定義了一個變量
export DIR_RC_SCRIPTS='/etc/init.d'
linux中一般啟動腳本都是放在這個目錄裡的,LINUX本身也有很多版本,因此作者在此處又進行多種類型的判斷,判斷的依據文件分別是/etc/redhat-release /etc/lsb-release /etc/debian_version這3種文件分別對應rhel/centos ubuntu debian , 從而也說明在LINUX的版本中,只支持這3種,其他的LINUX版本是不支持的,最後作者返回一句代碼:
定義了一個UNSUPPORTED_RELEASE變量,在後續的代碼應該會調用這個變量來判斷是否支持。
針對這3種LINUX,作者又具體細分到不同的小版本中,首先看rhel/centos 系列,下面代碼:
首先根據存在redhat-release文件,定義了DISTRO的值
if [ -f /etc/redhat-release ]; then
export DISTRO='RHEL'
這樣在get_all.sh的腳本裡調用時就有了判斷依據,接著判斷具體的版本,
if grep '\ 6' /etc/redhat-release &>/dev/null; then
# version 6.x
export DISTRO_VERSION='6'
elif grep '\ 7' /etc/redhat-release &>/dev/null; then
# version 7.x
export DISTRO_VERSION='7'
else
export UNSUPPORTED_RELEASE='YES'
fi
顯然只支持6和7兩個版本,也就是安裝時,如果不是這2個版本的話,就會返回unsupport_release的值為YES了。
rhel還有兩個個反編譯版本即centos ,Scientific Linux, 因此作者又定義了一個變量來區別 DISTRO_CODENAME, 代碼如下:
if grep '^Red' /etc/redhat-release &>/dev/null; then
# RHEL
export DISTRO_CODENAME='rhel'
elif grep '^CentOS' /etc/redhat-release &>/dev/null; then
# CentOS
export DISTRO_CODENAME='centos'
elif grep '^Scientific' /etc/redhat-release &>/dev/null; then
# Scientific Linux
export DISTRO_CODENAME='scientific'
else
export UNSUPPORTED_RELEASE='YES'
fi
顯然在/etc/redhat-release文件裡,不同的發行版本有不同的關鍵字,這樣關於紅帽系的操作系統,作者就已經區分完畢了。
接下來關於ubuntu的判斷就簡單多了,看圖
根據/etc/lsb-release的文件是否存在,賦值DISTRO為UBUNTU,然後判斷具體版本,看代碼
export DISTRO_ID="$(grep 'DISTRIB_ID' /etc/lsb-release | awk -F'=' '{print $2}')"
export DISTRO_VERSION="$(grep 'DISTRIB_RELEASE' /etc/lsb-release | awk -F'=' '{print $2}')"
export DISTRO_CODENAME="$(grep 'DISTRIB_CODENAME' /etc/lsb-release | awk -F'=' '{print $2}')"
# Unsupported releases: 12.x, 13.x, 14.10
if echo "${DISTRO_VERSION}" | grep -E '^(12|13|14\.10)' &>/dev/null ; then
export UNSUPPORTED_RELEASE='YES'
fi
從程序可以看出UBUNTU的ID , RELEASE , CODENAME的值都在/etc/lsb-release的文件裡,用grep 和awk匹配後,賦予不同變量的值。
用grep 篩選出系統版本為12,13,14.10的都為不支持版本,目前支持的版本為14.04,15.04
接下來判斷的就是DEBIAN了,同樣是判斷文件,如圖:
文件存在,則DISTRO的值為DEBIAN,接下來判斷版本號:
# Get major release version number
export DISTRO_VERSION="$(cat /etc/debian_version)"
cat /etc/debian_version 一個命令就可以得到具體版本,比rhel更容易判斷
接下來對具體版本進行判斷,見代碼:
# Set distro code name and unsupported releases.
if grep '^7' /etc/debian_version &>/dev/null || \
grep -i '^wheezy' /etc/debian_version &>/dev/null; then
export DISTRO_VERSION='7'
export DISTRO_CODENAME='wheezy'
elif grep '^8' /etc/debian_version &>/dev/null || \
grep -i '^jessie' /etc/debian_version &>/dev/null; then
export DISTRO_VERSION='8'
export DISTRO_CODENAME='jessie'
else
export UNSUPPORTED_RELEASE='YES'
fi
顯然只支持DEBIAN的7,8兩個版本,到此DISTRO的值和操作系統的判斷結束,如果想新增特殊操作系統的話或者自己定義的話,在此處加入自己的代碼即可。