scripts]# cat httpdctl
#!/bin/sh
#created by oldboy 20110523
#QQ 31333741
HTTPDPATH=/etc/init.d/httpd
if [ "$1" = "start" ];then
$HTTPDPATH $1 >/dev/null 2>&1
echo "启动 httpd:[确定]"
拥有帝国一切,皆有可能。欢迎访问phome.net
elif [ "$1" = "stop" ];then
$HTTPDPATH $1
elif [ "$1" = "restart" ];then
$HTTPDPATH $1 >/dev/null 2>&1
echo "重起 httpd: [确定]"
else
echo "用法: $0 {start|stop|restart}"
exit 1
fi
-------------------------------------------------------
c.调用系统函数functions,实现更专业友好的提示[继续脚本改进]
[root@oldboy-B scripts]# cat httpdctl
#!/bin/sh
#created by oldboy 20110523
#QQ 31333741
. /etc/init.d/functions
HTTPDPATH=/etc/init.d/httpd
if [ "$1" = "start" ];then
$HTTPDPATH $1 >/dev/null 2>&1
action "启动 httpd:" /bin/true
elif [ "$1" = "stop" ];then
$HTTPDPATH $1
elif [ "$1" = "restart" ];then
$HTTPDPATH $1 >/dev/null 2>&1
action "重起 httpd:" /bin/true
else
echo "用法: $0 {start|stop|restart}"
exit 1
fi
提示:注意action "启动 httpd:" /bin/true的用法
[root@oldboy-B scripts]# httpdctl stop
停止 httpd:[确定]
[root@oldboy-B scripts]# httpdctl start
启动 httpd: [确定]
[root@oldboy-B scripts]# httpdctl restart
重起 httpd: [确定]
-------------------------------------------------------
d.case语句实现方式
[root@oldboy-B scripts]# cat httpdctl
#!/bin/sh
#created by oldboy 20110523
#QQ 31333741
. /etc/init.d/functions
HTTPDPATH=/etc/init.d/httpd
case "$1" in
start)
$HTTPDPATH $1 >/dev/null 2>&1
action "启动 httpd:" /bin/true
;;
拥有帝国一切,皆有可能。欢迎访问phome.net
stop)
$HTTPDPATH $1
;;
restart)
$HTTPDPATH $1 >/dev/null 2>&1
action "重起 httpd:" /bin/true
;;
*)
echo "用法: $0 {start|stop|restart}"
esac
e.更专业的实现方法
[root@oldboy-B scripts]# cat httpdctl
#!/bin/sh
#created by oldboy 20110523
#QQ 31333741
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
start() {
daemon httpd >/dev/null 2>&1
RETVAL=$?
action "启动 httpd:" /bin/true
return $RETVAL
}
stop() {
killproc httpd >/dev/null 2>&1
[ $? -eq 0 ] && action "停止 httpd:" /bin/true
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
$0 stop
$0 start
;;
*)
echo "usage: $0 {start|stop|restart}"
esac
exit $RETVAL
f.系统自带的才是最好的.
请less /etc/init.d/httpd
要求:请所有同学用中文注释httpd脚本每一行,不会的可以咨询老师.
############################################## |