這個(gè)問(wèn)題看起來(lái)好像很簡(jiǎn)單,其實(shí)深究起來(lái)還有點(diǎn)復(fù)雜
網(wǎng)上有人說(shuō)可以用 uname -a 進(jìn)行查看蛙婴,那我們來(lái)看看到底能不能使用這個(gè)作為判別依據(jù)
//各大發(fā)行版的 uname -a 輸出
CentOS8.2
Linux localhost.localdomain 4.18.0-193.el8.x86_64 #1 SMP Fri May 8 10:59:10 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
Fedora34
Linux localhost-live 5.11.12-300.fc34.x86_64 #1 SMP Wed Apr 7 16:31:13 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
Debian9.6
Linux 4.9.0-8-amd64 #1 SMP Debian 4.9.130-2 (2018-10-27) x86_64 GNU/Linux
OpenSUSE15.3
Linux localhost.localdomain 5.3.18-57-default #1 SMP Wed Apr 28 10:54:41 UTC 2021 (ba3c2e9) x86_64 x86_64 x86_64 GNU/Linux
Ubuntu20.04
Linux ubuntu 5.8.0-59-generic #66~20.04.1-Ubuntu SMP Thu Jun 17 11:14:10 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
除了 Debian 和 Ubuntu 粗井,其他發(fā)行版并沒(méi)有輸出特別明顯的標(biāo)識(shí)。由此可見(jiàn),uname -a 的輸出并不足以用來(lái)標(biāo)識(shí)各大發(fā)行版
那么還有其他的方式來(lái)識(shí)別嗎浇衬?有的懒构,那就是 cat /proc/version ,我們來(lái)看看這個(gè)的輸出
CentOS8.2
Linux version 4.18.0-193.el8.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 8.3.1 20191121 (Red Hat 8.3.1-5) (GCC)) #1 SMP Fri May 8 10:59:10 UTC 2020
Fedora34
Linux version 5.11.12-300.fc34.x86_64 (mockbuild@bkernel01.iad2.fedoraproject.org) (gcc (GCC) 11.0.1 20210324 (Red Hat 11.0.1-0), GNU ld version 2.35.1-41.fc34) #1 SMP Wed Apr 7 16:31:13 UTC 2021
Debian9.6
Linux version 4.9.0-8-amd64 (debian-kernel@lists.debian.org) (gcc version 6.3.0 20170516 (Debian 6.3.0-18+deb9u1) ) #1 SMP Debian 4.9.130-2 (2018-10-27)
Ubuntu20.04
Linux version 5.8.0-59-generic (buildd@lcy01-amd64-022) (gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0, GNU ld (GNU Binutils for Ubuntu) 2.34) #66~20.04.1-Ubuntu SMP Thu Jun 17 11:14:10 UTC 2021
OpenSUSE15.3
Linux version 5.3.18-57-default (geeko@buildhost) (gcc version 7.5.0 (SUSE Linux)) #1 SMP Wed Apr 28 10:54:41 UTC 2021 (ba3c2e9)
可以看到耘擂,Red Hat系的發(fā)行版都會(huì)有 Red Hat 的標(biāo)識(shí)胆剧,而Debian系的都會(huì)有對(duì)應(yīng)的發(fā)行版關(guān)鍵詞,那么如果 Red Hat系該怎么區(qū)分發(fā)行版呢醉冤?答案就是 cat /etc/redhat-release 赞赖,所有Red Hat系的發(fā)行版都有這個(gè)文件,并且還可以顯示版本號(hào)
所以整體邏輯就是冤灾,先使用 cat /proc/version 查看是Red Hat系還是Debian系前域。如果是Red Hat系,再使用 cat /etc/redhat-release 查看具體的發(fā)行版和版本號(hào)
下面是一個(gè)可以識(shí)別發(fā)行版的 shell 腳本
pkg=""
get_release_pkg(){
if [[ $(cat /proc/version | grep -i "Red Hat") != "" ]]
then
pkg="yum"
elif [[ $(cat /proc/version | grep -i "Ubuntu" ) != "" ]] || [[ $(cat /proc/version | grep -i "Debian" ) != "" ]]
then
pkg="apt"
elif [[ $(cat /proc/version | grep -i "SUSE") != "" ]]
then
pkg="zypper"
fi
}
get_release_pkg
echo ${pkg}