shell脚本示例
在这篇文章中我将给大家几个脚本的示例:
示例一:
#!/bin/bash
echo "This script enable you to make dvd yum! and you should insert you iso in dvdrom "
yum clean all
echo "First it will creat a library!"
umount /dev/cdrom > /dev/null #先确定光驱中没有其他光盘
umount /dev/cdrom > /dev/null #为了确认卸载掉光盘,最好卸载两次。如在物理机中可省去这两步
echo "this will create a dir /media/cdrom."
mkdir /media/cdrom > /dev/null
mount /dev/cdrom /media/cdrom > /dev/null
rm -rf /etc/yum.repos.d/auto.repo > /dev/null
cat >>/etc/yum.repos.d/auto.repo<<EOF
# Main rhel5 server #以下内容为写到auto.repo文件中的yum配置文件
[base]
name=Instructor Server Repository
baseurl=file:///media/cdrom/Server
gpgcheck=0
# This one is needed for xen packages
[VT]
name=Instructor VT Repository
baseurl=file:///media/cdrom/VT
gpgcheck=0
# This one is needed for clustering packages
[Cluster]
name=Instructor Cluster Repository
baseurl=file:///media/cdrom/Cluster
gpgcheck=0
# This one is needed for cluster storage (GFS, iSCSI target, etc...) packages
[ClusterStorage]
name=Instructor ClusterStorage Repository
baseurl=file:///media/cdrom/ClusterStorage
gpgcheck=0
EOF
yum repolist #这一步验证yum是否成功
RESULT=`echo $?`
if [ $RESULT -eq 0 ]
echo "Now you can user the yum to install any package free! ^_^ "
else
echo "You hava other problem ."
fi
这个脚本的作用是方便那些在虚拟机中使用linux系统,自动化配置yum,让使用者能够通过仅执行这个脚本就可以使用yum安装包。在执行脚本前需要先将镜像加载到虚拟机的光驱中,而且这个脚本并没有把包同步到虚拟系统上,节省了硬盘的空间(毕竟两个多G呢),但在使用时必须将光盘挂载到 /media/cdrom 下使用命令:mount /dev/cdrom /media/cdrom 。
示例二
#!/bin/bash
NETDIR="/etc/sysconfig/network-scripts"
echo "$NETDIR"
read -p "Which type do want to choice(static/dhcp)?" NETTYPE
cp $NETDIR/ifcfg-eth0 $NETDIR/ifcfg-eth1
if [ $NETTYPE == 'dhcp' ];then
grep "^BOOTPROTO" $NETDIR/ifcfg-eth1 > /dev/null
RESULT1=`echo $?`
if [ $RESULT1 -eq 0 ];then
sed -i 's@BOOTPROTO=.*@BOOTPROTO=dhcp@' $NETDIR/ifcfg-eth1
else
echo "BOOTPROTO=dhcp" >> $NETDIR/ifcfg-eth1
fi
service network restart
else
grep "^BOOTPROTO" $NETDIR/ifcfg-eth1 > /dev/null
RESULT1=`echo $?`
if [ $RESULT1 -eq 0 ];then
sed -i 's@BOOTPROTO=.*@BOOTPROTO=static@' $NETDIR/ifcfg-eth1
else
echo "BOOTPROTO=static" >> $NETDIR/ifcfg-eth1
fi
read -p "Input the ipaddress: " IPADDRESS
grep "^IPADDR" $NETDIR/ifcfg-eth1 > /dev/null
RESULT2=`echo $?`
if [ $RESULT2 -eq 0 ];then
sed -i "s@IPADDR=.*@IPADDR=$IPADDRESS" $NETDIR/ifcfg-eth1
else
echo "IPADDR=$IPADDRESS" >> $NETDIR/ifcfg-eth1
fi
read -p "Input netmask: " INETMASK
grep "^NETMASK" $NETDIR/ifcfg-eth1 > /dev/null
RESULT3=`echo $?`
if [ $RESULT3 -eq 0 ];then
sed -i "s@NETMASK=.*@NETMASK=$INETMASK" $NETDIR/ifcfg-eth1
else
echo "NETMASK=$INETMASK" >> $NETDIR/ifcfg-eth1
fi
read -p "Input gateway: " IGATEWAY
grep "^GATEWAY" $NETDIR/ifcfg-eth1 > /dev/null
RESULT4=`echo $?`
if [ $RESULT4 -eq 0 ];then
sed -i "s@GATEWAY=.*@GATEWAY=$IGATEWAY" $NETDIR/ifcfg-eth1
else
echo "GATEWAY=$IGATEWAY" >> $NETDIR/ifcfg-eth1
fi
read -p "If start onboot(yes/no) ? " IONBOOT
grep "^ONBOOT" $NETDIR/ifcfg-eth1 > /dev/null
RESULT5=`echo $?`
if [ $RESULT5 -eq 0 ];then
sed -i "s@ONBOOT=.*@ONBOOT=$IONBOOT@" $NETDIR/ifcfg-eth1
else
echo "ONBOOT=$IONBOOT" >> $NETDIR/ifcfg-eth1
fi
service network restart
fi
# grep "^" $NETDIR/ifcfg-eth1 > /dev/null
# RESULT1=`echo $?`
这个脚本的作用是:创建一个配置文件eth1让后让你输入网络配置信息,对于获取IP地址的方法如果输入dhcp则不需再输入其它内容,如果static则详细的输入每一项内容,但并没有对输入数据的类型判断,因为对数据的判断对初学者比较难,我就不再写了。
以上两个例子仅供大家参考。