当我在 RHEL 6 上编写类似的 bash 脚本时,
if [ "$2" = "" ] ; then
echo vip.start: ERROR: no NIC specified
echo vip.start: Need IP and base interface name
exit 1
fi
case "$1" in
[0-9]*)
break
;;
*)
echo vip.start: ERROR: no IP address specified
echo vip.start: Need IP and base interface name
exit 1
;;
esac
# Is it already up?
#This uses the same algorithm as vip.stop
## If no conflicts, it should produce 'collisions:0
##=>For Solaris
#exists=`ifconfig -a | awk '
#$1 ~ /:/ {split($1,nic,":");
# lastif=sprintf("%s:%s",nic[1],nic[2]);}
#$2 == "'$1'" { print lastif ; exit; }
#
#'`
##=> For Linux. Retreves IP Address
exists=$(ifconfig | grep -B1 "inet addr:$1" | awk '$1!="inet" && $1!="--" {print $1}')
if [ "$exists" = "" ] ; then
ping -c 1 $1 1>/dev/null 2>&1
if [ $? -eq 0 ] ; then
echo "vip.start: ERROR: vip $1 already active elsewhere! Cannot continue!"
exit 1
fi
else
echo vip.start: NOTICE: vip already exists, as $exists
exit 0
fi
##=>This is fo Solaris
#ifconfig $2 addif $1 netmask + broadcast + up
##=>This is for generic GNU/Linux
echo "Bringing up VIP virtual interface"
ifconfig $2 $1 up
echo "VIP virtual interface has started"
exit 0
该脚本采用命令行参数,vip.start 192.168.1.1 eth0:1
然后应该打开界面。当使用 运行时bash -x vip.start 192.168.1.1 eth0:1
,它显示界面出现,但当我发出ifconfig
.然而,当我注释掉exit 0
VIP 时,脚本会退出。