Linux的shell编程前奏之基础技能实战四

一>开发sehll脚本实现为服务器临时配置多个IP,并且可以随时撤销配置的所有IP。IP的地址范围为:10.0.2.1~10.0.2.16,其中10.0.2.10不能配置。

#!/bin/sh
[ -f /etc/init.d/functions ] && . /etc/init.d/functions               #加载functions函数
RETVAL=0
op(){
if [ "$1" == "del" ]
then
list=`echo {16..1}`
else
list=`echo {1..16}`
fi
for ip in $list
do
if [$ip -eq 10]
then
continue
fi
ip addr $1 10.0.2.$ip/24 dev eth0 label eth0:$ip &>/dev/null
RETVAL=$?
if [ RETVAL -eq 0 ]
then
action "$1 $ip" /bin/true#提示成功
else#提示失败
action "$1 $ip" /bin/false
fi
done
return $RETVAL
}
case "$1" in 
start)
op add
RETVAL=$?
;;
stop)
op del
RETVAL=$?
;;
restart)
op del
sleep 2
op add
RETVAL=$?
;;
*)
printf "USAGE:$0 {start|stop|restart}\n"
esac
exit $RETVAL   #携带返回值退出脚本并将该返回值返还给当前shell

总结:1>

两种配置IP的命令(ifconfig/ip)

使用ifconfig配置:

ifconfig etho:0 10.0.2.10/24 up            #添加ip

ifconfig ehh0:0 10.0.2.10/24 down   #删除ip

使用ip配置辅助ip的方法:

ip addr add 10.0.2.11/24 dev eth0 label eth0:0#添加ip

ip addr del 10.0.2.11/24 dev eth0 label eth0:0 #删除ip

2>

case "变量" in

值1)

指令...

;;

值2)

指令...

;;

*)

指令...

;;

esac


猜你喜欢

转载自blog.51cto.com/11218855/2403684