将yum源更换为阿里的源(脚本)

#!/bin/bash##########################################Function: update source#Usage: bash update_source.sh#Author: Customer service department#Company: Alibaba Cloud Computing#Version: 5.0#########################################check_os_release(){while truedoos_release=$(grep "Red Hat Enterprise Linux Server release" /etc/issue 2>/dev/null)os_release_2=$(grep "Red Hat Enterprise Linux Server release" /etc/redhat-release 2>/dev/null)if [ "$os_release" ] && [ "$os_release_2" ]thenif echo "$os_release"|grep "release 5" >/dev/null 2>&1thenos_release=redhat5echo "$os_release"elif echo "$os_release"|grep "release 6" >/dev/null 2>&1thenos_release=redhat6echo "$os_release"elseos_release=""echo "$os_release"fibreakfios_release=$(grep "Aliyun Linux release" /etc/issue 2>/dev/null)os_release_2=$(grep "Aliyun Linux release" /etc/aliyun-release 2>/dev/null)if [ "$os_release" ] && [ "$os_release_2" ]thenif echo "$os_release"|grep "release 5" >/dev/null 2>&1thenos_release=aliyun5echo "$os_release"elif echo "$os_release"|grep "release 6" >/dev/null 2>&1thenos_release=aliyun6echo "$os_release"elif echo "$os_release"|grep "release 7" >/dev/null 2>&1thenos_release=aliyun7echo "$os_release"elseos_release=""echo "$os_release"fibreakfios_release_2=$(grep "CentOS" /etc/*release 2>/dev/null)if [ "$os_release_2" ]thenif echo "$os_release_2"|grep "release 5" >/dev/null 2>&1thenos_release=centos5echo "$os_release"elif echo "$os_release_2"|grep "release 6" >/dev/null 2>&1thenos_release=centos6echo "$os_release"elif echo "$os_release_2"|grep "release 7" >/dev/null 2>&1thenos_release=centos7echo "$os_release"elseos_release=""echo "$os_release"fibreakfios_release=$(grep -i "ubuntu" /etc/issue 2>/dev/null)os_release_2=$(grep -i "ubuntu" /etc/lsb-release 2>/dev/null)if [ "$os_release" ] && [ "$os_release_2" ]thenif echo "$os_release"|grep "Ubuntu 10" >/dev/null 2>&1thenos_release=ubuntu10echo "$os_release"elif echo "$os_release"|grep "Ubuntu 12.04" >/dev/null 2>&1thenos_release=ubuntu1204echo "$os_release"elif echo "$os_release"|grep "Ubuntu 12.10" >/dev/null 2>&1thenos_release=ubuntu1210echo "$os_release"elif echo "$os_release"|grep "Ubuntu 14.04" >/dev/null 2>&1thenos_release=ubuntu1204echo "$os_release" elseos_release=""echo "$os_release"fibreakfios_release=$(grep -i "debian" /etc/issue 2>/dev/null)os_release_2=$(grep -i "debian" /proc/version 2>/dev/null)if [ "$os_release" ] && [ "$os_release_2" ]thenif echo "$os_release"|grep "Linux 6" >/dev/null 2>&1thenos_release=debian6echo "$os_release"elif echo "$os_release"|grep "Linux 7" >/dev/null 2>&1thenos_release=debian7echo "$os_release"elseos_release=""echo "$os_release"fibreakfios_release=$(grep -i "opensuse" /etc/issue 2>/dev/null)os_release_2=$(grep -i "opensuse" /etc/*release 2>/dev/null)if [ "$os_release" ] && [ "$os_release_2" ]thenif echo "$os_release"|grep "openSUSE 13.1" >/dev/null 2>&1thenos_release=opensuse1301echo "$os_release"elseos_release=""echo "$os_release"fibreakfibreakdone}modify_aliyun5_yum(){wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.reposed -i 's/\$releasever/5/' /etc/yum.repos.d/CentOS-Base.repowget -qO /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-5.repoyum clean metadatayum makecachecd ~}modify_rhel5_yum(){wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repowget -qO /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-5.repoyum clean metadatayum makecachecd ~}modify_rhel6_yum(){wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repowget -qO /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repoyum clean metadatayum makecachecd ~}modify_rhel7_yum(){wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repowget -qO /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repoyum clean metadatayum makecachecd ~}update_ubuntu10_apt_source(){echo -e "\033[40;32mBackup the original configuration file,new name and path is /etc/apt/sources.list.back.\n\033[40;37m"cp -fp /etc/apt/sources.list /etc/apt/sources.list.backcat > /etc/apt/sources.list <<EOF#ubuntudeb http://cn.archive.ubuntu.com/ubuntu/ maverick main restricted universe multiversedeb-src http://cn.archive.ubuntu.com/ubuntu/ maverick main restricted universe multiverse#163deb http://mirrors.163.com/ubuntu/ maverick main universe restricted multiversedeb-src http://mirrors.163.com/ubuntu/ maverick main universe restricted multiversedeb http://mirrors.163.com/ubuntu/ maverick-updates universe main multiverse restricteddeb-src http://mirrors.163.com/ubuntu/ maverick-updates universe main multiverse restricted#lupaworlddeb http://mirror.lupaworld.com/ubuntu/ maverick main universe restricted multiversedeb-src http://mirror.lupaworld.com/ubuntu/ maverick main universe restricted multiversedeb http://mirror.lupaworld.com/ubuntu/ maverick-security universe main multiverse restricteddeb-src http://mirror.lupaworld.com/ubuntu/ maverick-security universe main multiverse restricteddeb http://mirror.lupaworld.com/ubuntu/ maverick-updates universe main multiverse restricteddeb http://mirror.lupaworld.com/ubuntu/ maverick-proposed universe main multiverse restricteddeb-src http://mirror.lupaworld.com/ubuntu/ maverick-proposed universe main multiverse restricteddeb http://mirror.lupaworld.com/ubuntu/ maverick-backports universe main multiverse restricteddeb-src http://mirror.lupaworld.com/ubuntu/ maverick-backports universe main multiverse restricteddeb-src http://mirror.lupaworld.com/ubuntu/ maverick-updates universe main multiverse restrictedEOFapt-get update}update_ubuntu1204_apt_source(){echo -e "\033[40;32mBackup the original configuration file,new name and path is /etc/apt/sources.list.back.\n\033[40;37m"cp -fp /etc/apt/sources.list /etc/apt/sources.list.backcat > /etc/apt/sources.list <<EOF#12.04deb http://mirrors.aliyun.com/ubuntu/ precise main restricted universe multiversedeb http://mirrors.aliyun.com/ubuntu/ precise-security main restricted universe multiversedeb http://mirrors.aliyun.com/ubuntu/ precise-updates main restricted universe multiversedeb http://mirrors.aliyun.com/ubuntu/ precise-proposed main restricted universe multiversedeb http://mirrors.aliyun.com/ubuntu/ precise-backports main restricted universe multiversedeb-src http://mirrors.aliyun.com/ubuntu/ precise main restricted universe multiversedeb-src http://mirrors.aliyun.com/ubuntu/ precise-security main restricted universe multiversedeb-src http://mirrors.aliyun.com/ubuntu/ precise-updates main restricted universe multiversedeb-src http://mirrors.aliyun.com/ubuntu/ precise-proposed main restricted universe multiversedeb-src http://mirrors.aliyun.com/ubuntu/ precise-backports main restricted universe multiverseEOFapt-get update}update_ubuntu1210_apt_source(){echo -e "\033[40;32mBackup the original configuration file,new name and path is /etc/apt/sources.list.back.\n\033[40;37m"cp -fp /etc/apt/sources.list /etc/apt/sources.list.backcat > /etc/apt/sources.list <<EOF#12.10deb http://mirrors.aliyun.com/ubuntu/ quantal main restricted universe multiversedeb http://mirrors.aliyun.com/ubuntu/ quantal-security main restricted universe multiversedeb http://mirrors.aliyun.com/ubuntu/ quantal-updates main restricted universe multiversedeb http://mirrors.aliyun.com/ubuntu/ quantal-proposed main restricted universe multiversedeb http://mirrors.aliyun.com/ubuntu/ quantal-backports main restricted universe multiversedeb-src http://mirrors.aliyun.com/ubuntu/ quantal main restricted universe multiversedeb-src http://mirrors.aliyun.com/ubuntu/ quantal-security main restricted universe multiversedeb-src http://mirrors.aliyun.com/ubuntu/ quantal-updates main restricted universe multiversedeb-src http://mirrors.aliyun.com/ubuntu/ quantal-proposed main restricted universe multiversedeb-src http://mirrors.aliyun.com/ubuntu/ quantal-backports main restricted universe multiverseEOFapt-get update}update_ubuntu1404_apt_source(){echo -e "\033[40;32mBackup the original configuration file,new name and path is /etc/apt/sources.list.back.\n\033[40;37m"cp -fp /etc/apt/sources.list /etc/apt/sources.list.backcat > /etc/apt/sources.list <<EOF#14.04deb http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiversedeb http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiversedeb http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiversedeb http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiversedeb http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiversedeb-src http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiversedeb-src http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiversedeb-src http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiversedeb-src http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiversedeb-src http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverseEOFapt-get update}update_debian6_apt_source(){echo -e "\033[40;32mBackup the original configuration file,new name and path is /etc/apt/sources.list.back.\n\033[40;37m"cp -fp /etc/apt/sources.list /etc/apt/sources.list.backcat > /etc/apt/sources.list <<EOF#debian6deb http://mirrors.aliyun.com/debian/ squeeze main non-free contribdeb http://mirrors.aliyun.com/debian/ squeeze-proposed-updates main non-free contribdeb-src http://mirrors.aliyun.com/debian/ squeeze main non-free contribdeb-src http://mirrors.aliyun.com/debian/ squeeze-proposed-updates main non-free contribEOFapt-get update}update_debian7_apt_source(){echo -e "\033[40;32mBackup the original configuration file,new name and path is /etc/apt/sources.list.back.\n\033[40;37m"cp -fp /etc/apt/sources.list /etc/apt/sources.list.backcat > /etc/apt/sources.list <<EOF#debian7deb http://mirrors.aliyun.com/debian/ wheezy main non-free contribdeb http://mirrors.aliyun.com/debian/ wheezy-proposed-updates main non-free contribdeb-src http://mirrors.aliyun.com/debian/ wheezy main non-free contribdeb-src http://mirrors.aliyun.com/debian/ wheezy-proposed-updates main non-free contribEOFapt-get update}update_opensuse_source(){mv /etc/zypp/repos.d/* /tmp/zypper addrepo -f http://mirrors.aliyun.com/opensuse/distribution/13.1/repo/oss/ openSUSE-13.1-Osszypper addrepo -f http://mirrors.aliyun.com/opensuse/distribution/13.1/repo/non-oss/ openSUSE-13.1-Non-Osszypper addrepo -f http://mirrors.aliyun.com/opensuse/update/13.1/ openSUSE-13.1-Update-Osszypper addrepo -f http://mirrors.aliyun.com/opensuse/update/13.1-non-oss/ openSUSE-13.1-Update-Non-Osszypper addrepo -f http://mirrors.aliyun.com/opensuse/distribution/13.1/repo/oss/ openSUSE-13.1-Oss-aliyunzypper addrepo -f http://mirrors.aliyun.com/opensuse/distribution/13.1/repo/non-oss/ openSUSE-13.1-Non-Oss-aliyun zypper addrepo -f http://mirrors.aliyun.com/opensuse/update/13.1/ openSUSE-13.1-Update-Oss-aliyunzypper addrepo -f http://mirrors.aliyun.com/opensuse/update/13.1-non-oss/ openSUSE-13.1-Update-Non-Oss-aliyun}####################Start####################check lock file ,one time only let the script run one time LOCKfile=/tmp/.$(basename $0)if [ -f "$LOCKfile" ]thenecho -e "\033[1;40;31mThe script is already exist,please next time to run this script.\n\033[0m"exitelseecho -e "\033[40;32mStep 1.No lock file,begin to create lock file and continue.\n\033[40;37m"touch $LOCKfilefi#check userif [ $(id -u) != "0" ]thenecho -e "\033[1;40;31mError: You must be root to run this script, please use root to install this script.\n\033[0m"rm -rf $LOCKfileexit 1fiecho -e "\033[40;32mStep 2.Begin to check the OS issue.\n\033[40;37m"os_release=$(check_os_release)if [ "X$os_release" == "X" ]thenecho -e "\033[1;40;31mThe OS does not identify,So this script is not executede.\n\033[0m"rm -rf $LOCKfileexit 0elseecho -e "\033[40;32mThis OS is $os_release.\n\033[40;37m"fiecho -e "\033[40;32mStep 3.Begin to modify the source configration file and update.\n\033[40;37m"case "$os_release" inaliyun5)modify_aliyun5_yum;;redhat5|centos5)modify_rhel5_yum;;redhat6|centos6|aliyun6)modify_rhel6_yum;;centos7|aliyun7)modify_rhel7_yum;;ubuntu10)update_ubuntu10_apt_source;;ubuntu1204)update_ubuntu1204_apt_source;;ubuntu1210)update_ubuntu1210_apt_source;;ubuntu1404)update_ubuntu1404_apt_source;;debian6)update_debian6_apt_source;;debian7)update_debian7_apt_source;;opensuse1301)update_opensuse_source;;esacecho -e "\033[40;32mSuccess,exit now!\n\033[40;37m"rm -rf $LOCKfile
(0)

相关推荐