天天看點

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格式:

case "變量" in

值1)

指令...

;;

值2)

*)

esac