自动创建YUM源

 

#!/bin/bash
#该程序可以根据用户填写的YUM源自动写入相应的配置文件
#如果要多个Yum 源,请拷贝 /etc/yum.repos.d/rhel.repo 自行修改
#注意:该脚本会删除原有的YUM配置!如果不需要删除请更改相关Comand!
#作者 yjh100@Foxmail.com

function rootness(){
if [[ $EUID -ne 0 ]]; then
echo "Error:This script must be run as root!" 1>&2
exit 1
fi
}

function mcdrom(){
#echo -e " \033[0m"
echo -e "Please input your Cdrom|Http|Ftp path: \033[0m"
read -p "(Default use Cdrom path: /dev/cdrom/): " DEC
[ -z "$DEC" ] && DEC="/dev/cdrom"
#{DEC:=/dev/cdrom} &> /dev/null
#以上两种方案都可以实现回车赋默认值
URL=`echo "${DEC}" | grep -Eio "http|ftp"`
case ${URL} in
http|ftp)
echo "http,ftp"
cdrfile
;;
*)
echo "-----------Please input your mount path------------------------"
echo "The file is created automatically if it doesn't already exist. "
read -p "(Default : /mnt):" MPATH
#${MPATH:=/mnt} &> /dev/null
[ -z "$MPATH" ] && MPATH="/mnt/"

if [ -d ${MPATH} ]; then
umount ${DEC} &> /dev/null
#mount ${DEC} ${MPATH}
echo -e "${DEC}\t${MPATH}\tiso9660\tdefaults\t0\t0" | tee -a /etc/fstab
mount -a
checkCD
cdrfile
else
mkdir -p ${MPATH}
umount ${DEC} &> /dev/null
# mount ${DEC} ${MPATH}
echo -e "${DEC}\t${MPATH}\tiso9660\tdefaults\t0\t0" | tee -a /etc/fstab
mount -a
checkCD
cdrfile
fi

;;
esac
}

function checkCD(){
if [ ! -s ${MPATH}/*.repo ]; then
echo "Error ,Please check your configuration(e:in cdrom)"
exit 1
fi

}

function cdrfile(){
if [ ! -z ${MPATH} ];then
BAURL=file://${MPATH}
else
BAURL=${DEC}
fi
echo "Do you want to enable GPG-check? "
echo "If GPG-check is preferred,Please input the name!"
read -p "(pressing return unenable): " GC
if [ -z "$GC" ];then
GCN=0
else
GCN=1
GCF="${GC}"
fi
echo -e "Please wait a moment after submitting, while our OS sends you the repor \033[0m"
rm -fr /etc/yum.repos.d/*
yum clean all &> /dev/null
touch /etc/yum.repos.d/rhel.repo
cat >> /etc/yum.repos.d/rhel.repo <<EOF
[rhel_yum]
gpgcheck = ${GCN}
enabled = 1
baseurl = ${BAURL}
gpgkey = ${GCF}
name = Yum copy of ${BAURL}
EOF

#ENA=`yum repolist all | grep rhel | awk '{printf $(NF-1)}' | sed 's/://g'`
#NUM1=`yum repolist all | grep rhel | awk '{printf $(NF)}' | sed 's/,//g'`
#ENA1=enabled
#echo $ENA $NUM1
yum makecache &> /dev/null
if [[ $? == 0 ]]; then
echo -e 'Yum success! Enjoy ! \033[0m'
else
echo ' Error ,Please check your configuration! (e: in yum)'
fi
}
rootness
mcdrom
#---------------------------------------------------------------

上一篇
下一篇