#!/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
# 一鍵執行切換yum:wget www.eisc.cn/file/shell/centos-yum.sh ; sh centos-yum.sh
# 感謝網友提供yum解決方案:https://developer.aliyun.com/article/748336
# 阿裡巴巴開源鏡像站,鏡像倉庫: https://developer.aliyun.com/mirror/
# 阿裡雲linux系統鏡像: https://mirrors.aliyun.com/centos-vault/