linux centos6/7/8自动 切换yum源

2021-10-11 10:21:29 浏览数 (3)

代码语言:javascript复制
#!/bin/bash
centos6d(){
release=`cat /etc/redhat-release  |grep release | awk -F" " '{print $3}' | awk -F"." '{print $1}'` ; echo "centos 版本为: $release "
case "$release" in
"6") echo " 当前匹配centos 版本为 6"
repo="/etc/yum.repos.d/centos6-epel.repo"
if [ ! -e $repo ]; then                 # -e 判断文件存在;参考:Shell if 条件判断文件或目录
yum install -y wget
cd /etc/yum.repos.d/
mkdir bak
mv * bak
wget work.eisc.cn/ruanjian/rpm/yumrepo/centos6-epel.repo
wget work.eisc.cn/ruanjian/rpm/yumrepo/centos6.repo
yum clean all                           # 清除原有缓存
yum makecache                      # 生成新的缓存
                                                # make cache [me?k]  [ka?]  制作 缓存{ka xi}
yum  list                                       # 校验yum
cd
else
echo "yum已经切换"
fi
;;
*) echo "低版本未匹配到,正在匹配红帽版本配置yum !!!"
redhat8d
;;
esac
}
centos7(){
repo="/etc/yum.repos.d/epel-7.repo"
if [ ! -e $repo ]; then                 # -e 判断文件存在;参考:Shell if 条件判断文件或目录
yum install -y wget
cd /etc/yum.repos.d/
mkdir bak
mv * bak
wget work.eisc.cn/ruanjian/rpm/yumrepo/Centos-7.repo
wget work.eisc.cn/ruanjian/rpm/yumrepo/epel-7.repo
# wget http://mirrors.aliyun.com/repo/Centos-7.repo
# wget http://mirrors.aliyun.com/repo/epel-7.repo
                                                # 移动两个文件到 目录
#wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
#sed -i  's/$releasever/7/g' /etc/yum.repos.d/CentOS-Base.repo
                                                # yum源没有对应版本包匹配;修改yum为centos7
yum clean all                           # 清除原有缓存
yum makecache                      # 生成新的缓存
                                                # make cache [me?k]  [ka?]  制作 缓存{ka xi}
yum  list                                       # 校验yum
cd
else
echo "yum已经切换"
fi
}
centos8(){
repo="/etc/yum.repos.d/Centos-8.repo"
if [ ! -e $repo ]; then
cd /etc/yum.repos.d/
mkdir bak
mv * bak
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo
#sed -i 's/$releasever/8/g' /etc/yum.repos.d/CentOS-Base.repo
yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
wget -O /etc/yum.repos.d/epel8.repo work.eisc.cn/ruanjian/rpm/yumrepo/epel8.repo
#sed -i 's|^#baseurl=https://download.fedoraproject.org/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
#sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
yum clean all #清理缓存
yum makecache #更新源
else
echo "yum已经切换"
fi
}
redhat8d(){
release=`cat /etc/redhat-release  |grep release | awk -F" " '{print $6}' | awk -F"." '{print $1}'` ; echo "redhat 版本为: $release "
case "$release" in
"8") echo " 当前匹配redhat 版本为 8"
repo="/etc/yum.repos.d/Centos-8.repo"
if [ ! -e $repo ]; then
cd /etc/yum.repos.d/
mkdir bak
mv * bak
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo
#sed -i 's/$releasever/8/g' /etc/yum.repos.d/CentOS-Base.repo
yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
wget -O /etc/yum.repos.d/epel8.repo work.eisc.cn/ruanjian/rpm/yumrepo/epel8.repo
#sed -i 's|^#baseurl=https://download.fedoraproject.org/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
#sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
yum clean all ; yum makecache  ; yum list
else
echo "yum已经切换"
fi
;;
*) echo "红帽版本未匹配到,退出!!!"
;;
esac
}
release=`cat /etc/redhat-release  |grep release | awk -F" " '{print $4}' | awk -F"." '{print $1}'` ; echo "centos 版本为: $release "
case "$release" in
"6") echo " 当前匹配centos 版本为 6"
centos6
;;
"7") echo " 当前匹配centos 版本为 7"
centos7
;;
"8") echo "当前匹配centos 版本为 8"
centos8
;;
*) echo "您的centos过于老旧,正在匹配低版本"
centos6d
;;
esac
# centos 6 切换yum: https://help.aliyun.com/document_detail/193569.htm
# wget www.eisc.cn/file/shell/centos-yum.sh ; sh centos-yum.sh
# 感谢网友提供yum解决方案:https://developer.aliyun.com/article/748336

0 人点赞