#!/bin/bash
###这是第一版 第二版本请看 #https://blog.51cto.com/junhai/2139145
###记录第一次写的脚本
dir=/etc/yum.repos.d/
ftp=ftp://127.0.0.1/pub                      
iso=/home/iso/CentOS-7-x86_64-DVD-1804.iso   
echo "-----------------------------------"
echo "Input $0 1 install ftp"
echo "Input $0 2 install iso"
echo "Input $0 3 install aliyun Centos-5.repo"
echo "Input $0 4 install aliyun Centos-6.repo"
echo "Input $0 5 install aliyun Centos-7.repo"
echo "Input $0 6 install 163 CentOS5-Base-163.repo"
echo "Input $0 7 install 163 CentOS6-Base-163.repo"
echo "Input $0 8 install 163 CentOS7-Base-163.repo"
echo "-----------------------------------"

echo "Please Correct Input [1-8] Begin install"
expr 1 + $1 >>/dev/null||exit 

if [ $1 -z ] >/dev/null 2>&1;then 
   echo "Input $1 error"
   exit
elif [ $1 -lt 1 ];then
   echo "Input $1 error"
   exit
elif [ $1 -gt 8 ];then
   echo "Input $1 error"
   exit
else
   echo "Shell begin run"
   echo "-----------------------------------"
fi

echo "Begin backup  $dir"
mkdir -p $dir/backup
mv -f $dir/*.repo $dir/backup  
echo "Backup yum files success" 
echo "Backup yum directory "$dir"backup"
	
echo "-----------------------------------"

if [ $1 -eq 1 ];then  
cat >  $dir/base.repo << EOF
[base]
name=base
baseurl=$ftp
enabled=1
gpgcheck=0  
EOF

elif [ $1 -eq 2 ];then
        if [ ! -d  "/yumiso" ] ;then
              mkdir -p /yumiso
          else 
          	echo "/yumiso directory already exists"
        fi
mount -o loop $iso /yumiso
cat > $dir/base.repo <<EOF
[base]
name=base
baseurl=file:///yumiso
enabled=1
gpgcheck=0
EOF


elif [ $1 -eq 3 ];then
cd $dir
wget http://mirrors.aliyun.com/repo/Centos-5.repo
elif [ $1 -eq 4 ];then
cd $dir
wget http://mirrors.aliyun.com/repo/Centos-6.repo
elif [ $1 -eq 5 ];then
cd $dir
wget http://mirrors.aliyun.com/repo/Centos-7.repo
elif [ $1 -eq 6 ];then
cd $dir
wget http://mirrors.163.com/.help/CentOS5-Base-163.repo
elif [ $1 -eq 7 ];then
cd $dir
wget http://mirrors.163.com/.help/CentOS6-Base-163.repo
elif [ $1 -eq 8 ];then
cd $dir
wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
fi
 
echo "-----------------------------------"
echo "Begin clean yum all cache"
echo "-----------------------------------"
yum clean all
yum makecache 
echo "-----------------------------------"
echo "Yum file install complete"
echo "-----------------------------------"

上课需要切换老师的YUM源 第一次写脚本