linux-tutorial/codes/linux/sys/change-yum-repo.sh

58 lines
2.3 KiB
Bash
Raw Normal View History

2018-02-23 13:30:59 +08:00
#!/usr/bin/env bash
2019-10-24 22:00:00 +08:00
# ---------------------------------------------------------------------------------
# 控制台颜色
BLACK="\033[1;30m"
RED="\033[1;31m"
GREEN="\033[1;32m"
YELLOW="\033[1;33m"
BLUE="\033[1;34m"
PURPLE="\033[1;35m"
CYAN="\033[1;36m"
RESET="$(tput sgr0)"
# ---------------------------------------------------------------------------------
printf "${BLUE}\n"
2019-03-04 20:41:51 +08:00
cat << EOF
2018-02-23 13:30:59 +08:00
###################################################################################
2018-09-30 10:04:45 +08:00
# 本脚本用于替换 yum repo使用国内 yum 仓库,加速下载
2019-02-22 17:09:40 +08:00
# 要求:仅适用于 Linux CentOS 发行版本,并且环境必须已支持 yum 、lsb_release 命令
2019-05-07 14:29:16 +08:00
# @author: Zhang Peng
2018-02-23 13:30:59 +08:00
###################################################################################
2019-03-04 20:41:51 +08:00
EOF
2019-10-24 22:00:00 +08:00
printf "${RESET}\n"
2019-03-04 20:41:51 +08:00
2019-10-24 22:00:00 +08:00
printf "\n${GREEN}>>>>>>>>> 替换 yum repo 源开始${RESET}\n"
2018-02-23 13:30:59 +08:00
# 备份
cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
# 执行 lsb_release 命令,获取系统发行版本
2018-03-15 17:43:59 +08:00
# version=`lsb_release -r | awk '{print substr($2,1,1)}'` # 很多机器没有 lsb_release 命令
version=`cat /etc/redhat-release | awk '{print substr($4,1,1)}'`
2018-02-23 13:30:59 +08:00
# 根据发型版本选择相应 yum 镜像
2019-02-22 18:02:38 +08:00
if [[ ${version} == 5 ]]; then
2019-10-10 08:56:31 +08:00
# Cento5 已废弃,只能使用 http://vault.CentOS.org/ 替换,但由于是国外镜像,速度较慢
wget -N https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/yum/Centos-5.repo -O /etc/yum.repos.d/CentOS-Base.repo
2018-02-23 13:30:59 +08:00
2019-10-10 08:56:31 +08:00
# 根据实际发型版本情况替换
detailVersion=`lsb_release -r | awk '{print substr($2,1,3)}'`
sed -i 's/$releasever/'"${detailVersion}"'/g' /etc/yum.repos.d/CentOS-Base.repo
2018-02-23 13:30:59 +08:00
2019-10-10 08:56:31 +08:00
# 不替换下面的开关可能会出现错误Could not open/read repomd.xml
sed -i 's/enabled=1/enabled=0/g' /etc/yum.repos.d/CentOS-Media.repo
2019-02-22 18:02:38 +08:00
elif [[ ${version} == 6 ]]; then
2019-10-10 08:56:31 +08:00
wget -N https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/yum/Centos-6.repo -O /etc/yum.repos.d/CentOS-Base.repo
2019-02-22 18:02:38 +08:00
elif [[ ${version} == 7 ]]; then
2019-10-10 08:56:31 +08:00
wget -N https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/yum/Centos-7.repo -O /etc/yum.repos.d/CentOS-Base.repo
2018-02-23 13:30:59 +08:00
else
2019-10-24 22:00:00 +08:00
printf "\n${RED}版本不支持,替换 yum repo 失败${RESET}\n"
2018-02-23 13:30:59 +08:00
fi
# 更新缓存
yum clean all
yum makecache
2019-10-24 22:00:00 +08:00
printf "\n${GREEN}<<<<<<<< 替换 yum repo 源结束${RESET}\n"