参考于:
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