linux基础脚本 yum仓库的简单自动配置

#!/bin/bsah
 
if [ "$UID" != "0"  ];then                 //用系统变量$UID来判断用户身份是不是root
        echo "You are not a root user"           //如果不是退出脚本
        exit 1    
fi
ls /dev/sr0 &>/dev/null                  //查看光驱是否存在
if [ $? -ne 0 ];then                    //如果不存在退出脚本
        echo "You have no CD-ROM"
        exit 2
fi
fdisk -l /dev/sr0 | grep "Disk" &>/dev/null        //查看光驱中是否有光盘
if [ $? -ne 0 ] ;then                    //如果没有退出脚本
        echo "You have no disk"
        exit 3
fi
df -T | grep "iso9660" &>/dev/null            //查看光盘有没有被光驱自动挂载
if [ "$?" ==  "0" ];then                  //如果被自动挂载就卸载
        umount /dev/sr0
fi
if [ ! -d /yum ];then                    //检查挂载目录有没有创建
        mkdir -p /yum                      //如果没有就创建
fi
grep "^/dev/sr0" /etc/fstab | grep "/yum" &>/dev/null    //查看/etc/fstab有没有开机自动挂载的条目
if [ $? -ne 0 ];then                      //如果没有就追加
        echo -e "/dev/sr0 /yum iso9660 defaults 0 0" >>/etc/fstab
fi
mount -a &>/dev/null                  //测试自动挂载
if [ $? -ne 0 ];then                    //查看挂载是否成功
        echo "Mount error ! Please check."         //如果失败就退出
        exit 5
fi
ls /yum | grep "repodata" &>/dev/null          //检查挂载的光盘是否为Linux光盘
if [ "$?" !=  "0"  ];then                    //如果不是就退出
        echo "You don't have a linux disk"
        exit 6
fi
piece="/etc/yum.repos.d/"               //定义变量
bakpiece="/etc/yum.repos.d/bak/"
mkdir -p ${bakpiece}                 //创建备份文件夹
mv ${piece}*.repo  ${bakpiece} &>/dev/null      //将旧的配置文件全部移动到备份文件夹
newyum=yum_`date +%F_%T`.repo  
touch ${piece}${newyum}               //创建新的yum仓库配置文件
echo "[yumrepo]" >${piece}${newyum}         //编写简单的配置文件
echo "name=myyum" >>${piece}${newyum}
echo "baseurl=file:///yum" >>${piece}${newyum}
echo "gpgcheck=0" >>${piece}${newyum}
(LANG=en_US.UTF-8;yum repolist all | grep "myyum" | grep -q  "enabled")     //检查yum是否配置完成
if [ $? -ne 0 ];then                                    //如果失败就退出
        cat <<-EOF
        Please check your option
        EOF
        exit 7
else
        echo "Yum repo  is  ready"
fi
原文地址:https://www.cnblogs.com/knightysa/p/9267553.html