您的位置:首页 > 运维架构 > Shell

shell script自动搭建简单的yum

2013-03-04 17:23 295 查看
#!/bin/sh

#Shell name:config_yum.sh

#Program

# config for yum-pack

#

#Author:pero

#Email: perofu.com@gmail.com

#History

# 2012/07/15

#Usage:vi config_yum.sh ; :set ff=unix

#

#If you have any opinion please contact me

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:.

export PATH

if [ $UID -ne 0 ]

then

echo "Run as root"

exit 1

fi

pack=/mnt/Packages

pack1=/mnt/CentOS

CD=$(mount | gawk '/\/dev\/sr0/ {print $1}')

odir=$(mount | gawk '/\/dev\/sr0/ {print $3}')

#mount CDROM

if [ -n $CD ]&&[ -n $odir ]

then

umount /mnt &> /dev/null

echo "now,mounting IOS to the /mnt"

mount /dev/cdrom /mnt &> /dev/null

echo "mount is ok"

else

mount /dev/cdrom /mnt &> /dev/null

echo "mount is ok"

fi

if [ -d $pack ]||[ -d $pack1 ]

then

read -t 25 -p "Please input a Dir-name for yum_pack [/yum]:" yum_path

if [ -n $yum_path ] #create dir

then

mkdir -p $yum_path &> /dev/null

echo "dir is created"

else

yum_path=/yum

mkdir -p $yum_path &> /dev/null

echo "dir is created"

fi

#cp CD to dir

if [ -d $yum_path ]

then

echo "please wait a min"

cp -a /mnt/* $yum_path

echo "cp is ok"

else

echo "$yum_path is not exist"

exit 77 #if dir is not exist ,quit the shell

fi

cd /etc/yum.repos.d/

rm -rf /etc/yum.repos.d/*

#create yum config file

read -t 30 -p "Please input a name for yum-config-file [dvd.repo],do not use 'red hat' :" filename

if [ -n $filename ]

then

touch $filename.repo

echo "file was created"

else

filename=dvd

touch $filename.repo

echo "file was created"

fi

echo "[rhel-source]" >> $filename.repo

echo "name=Red Hat" >> $filename.repo

echo "baseurl=file://$yum_path" >> $filename.repo

echo "enabled=1" >> $filename.repo

echo "gpgcheck=0" >> $filename.repo

echo "gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release" >> $filename.repo

echo "yum-config-file was fanish"

else

echo "Please input a CD-ROM and try again or connect the writer!!!"

fi

#new added

yum install createrepo -y &> /dev/null

if [ $? -ne "0" ]

then

exit 77

fi

xml=$(ls ${yum_path}/repodata/ |grep comp |grep -v gz)

cp ${yum_path}/repodata/${xml} /tmp/

rm -rf ${yum_path}/repodata/

createrepo -g /tmp/${xml} ${yum_path}

if [ $? -eq 0 ]

then

echo "all of yum is ok..."

else

echo "please check again..."

fi

rm -rf /tmp/${xml}

本文出自 “无咎” 博客,请务必保留此出处http://perofu.blog.51cto.com/6061242/1144588
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: