參考於:
https://blog.51cto.com/chaichuan/2069965
https://blog.csdn.net/zhan570556752/article/details/80399154
centos6和centos7版本的諸多命令不同,在維護的時候需要寫多個版本的腳本來執行太麻煩,網上衝浪一番,結合前輩們的文章整理了一下讓程序自己來判斷。方法如下:
1.查看cat /etc/centos-release
root@pts/0 # cat /etc/redhat-release|sed -r 's/.* ([0-9]+)\..*/\1/'
7
root@pts/15 # cat /etc/redhat-release|sed -r 's/.* ([0-9]+)\..*/\1/'
6
#!/bin/bash
stat=`cat /etc/redhat-release|sed -r 's/.* ([0-9]+)\..*/\1/'`
if [ $stat == 6 ];then
echo "系統版本是:"$stat
elif [ $stat == 7 ];then
echo "系統版本是:"$stat
else
echo "系統版本是:"$stat
fi
2.查看rpm -q centos-release
root@pts/0 # rpm -q centos-release|cut -d- -f3
7
root@pts/15 # rpm -q centos-release|cut -d- -f3
6
#!/bin/bash
stat=`rpm -q centos-release|cut -d- -f3`
if [ $stat == 6 ];then
echo "系統版本是:"$stat
elif [ $stat == 7 ];then
echo "系統版本是:"$stat
else
echo "系統版本是:"$stat
fi