这道题目虽然不是很难,但是如果编程不熟练的话,还是有些挑战的。在这里感谢我搜索到的这位朋友,他的解题代码给我很大的参考,他的blog是:http://sohudrgon.blog.51cto.com/3088108/1586780/
写一个脚本,完成如下任务,其使用形式如下所示:
script.sh {start|stop|restart|status}
其中:
如果参数为空,则显示帮助信息,并退出脚本;
如果参数为start,则创建文件/var/lock/subsys/script,并显示“starting script successfully.”
如果参数为stop,则删除文件/var/lock/subsys/script,并显示“stop script successfully.”
如果参数为restart,则删除文件/var/lock/subsys/script并重新创建,而后显示“Restarting script successfully.”
如果参数为status,那么:
如果文件/var/lock/subsys/script存在,则显示“Script is running...”,否则则显示“Script is stopped.”
#!/bin/bash
#
shName=`basename $0`
fileName="/var/lock/subsys/$shName"
if [ $# -ne 1 ];then
echo "Usage:$shName {start|stop|restart|status}"
echo "Please try again."
exit 3
fi
if [ "$1" == "start" ];then
if ! [ -e $fileName ];then
touch $fileName && echo "Starting $fileName successfully."
else
echo "$fileName already exists, try again." && exit 4
fi
elif [ "$1" == "stop" ];then
if [ -e $fileName ];then
rm -fv $fileName && echo "Stop $fileName successfully."
else
echo "$fileName does not exist, try again." && exit 4
fi
elif [ "$1" == "restart" ];then
if [ -e $fileName ];then
rm -fv $fileName
touch $fileName && echo "Restarting $fileName successfully."
else
touch $fileName
echo "Restarting $fileName successfully."
fi
elif [ "$1" == "status" ];then
if [ -e $fileName ];then
echo "$shName is running."
else
echo "$shName is stopped."
fi
else
echo "Usage:$shName {start|stop|restart|status}"
echo "Please try again."
exit 3
fi