前面写过关于自动搭建yum的脚本,感觉不够完善。下面这个脚本在前面的基础上加上判断,如果yum已经搭建好,那么不安装;没有搭建则安装。

1[root@sv7 ~]# vim testyum.sh 脚本内容 #!/bin/bash N=yum repolist | grep 'repolist'| awk '{print $2}' if [ $((N)) -gt 0 ]; then echo "YUM源已经安装" exit elif [ $((N)) -eq 0 ];then rm -rf /etc/yum.repos.d/* echo "[110] //注意从此行开始到gpgcheck=0行都要顶头写,如果不是,那么yum文件会产生格式错误 name=110 baseurl=http://192.168.4.254/rhel7 enabled=1 gpgcheck=0 " > /etc/yum.repos.d/110.repo yum clean all &> /dev/null echo "YUM已经搭建完成" echo "共有软件包:" yum repolist | tail -1 fi [root@sv7 ~]# chmod +x /root/testyum.sh

2 测试 已经搭建测试 [root@sv7 ~]# ./testyum.sh YUM源已经安装

没有搭建测试 [root@sv7 ~]# rm -rf /etc/yum.repos.d/* [root@sv7 ~]# ./testyum.sh YUM已经搭建完成 共有软件包: repolist: 4,620