Exercise : Write a script: You can accept a parameter that uses the following form:
script.sh {Start|stop|restart|status}
If the argument is start, the empty file/var/lock/subsys/script is created and the starting script successfully is displayed. ;
If the parameter is stop, the file/var/lock/subsys/script is deleted and the Stop script finished is displayed. ;
If the parameter is restart, the file/var/lock/subsys/script is deleted and the restarting script successfully is displayed. ;
If the parameter is status, then:
If the/var/lock/subsys/script file exists, it is displayed as "script is running."
Otherwise, it is displayed as "script is stopped."
Any other parameter: "script.sh {start|stop|restart|status}" is displayed
#!/bin/bash# date: 2015-04-06# author: arvinlau# description: # version: 1.0filename= ' basename $0 ' filepath= "/var/lock/subsys/$FileName" #变量替换, using double quotes if [ $# -lt 1 ]; then echo "usage:$0 {start | restart | stop | status} " exit 5fiif [ $1 == start ]; then touch $FilePath echo "starting $FileName successfully." elif [ $1 == stop ]; then rm -f $FilePath echo "stop script finished." elif [ $1 == restart ]; then rm -f $FilePath touch $FilePath echo "restarting script Successfully. " elif [ $1 == status ]; then if [ -e $FilePath ] ; then echo "script is running." elif [ ! -e $FilePath ]; then echo "script is stopped." fielse echo "Usage:$0 {start | restart | stop | status} " exit 7fi
#虽然目前知识所写的脚本, the function is simple, but can be as far as possible to improve the actual situation, the following script, the existence of the file to judge, supplementary script is insufficient.
#!/bin/bash# date: 2015-04-06# author: arvinlau# description: # version: 1.0filename= ' basename $0 ' filepath= "/var/lock/subsys/$FileName" #变量替换, using double quotes if [ $# -lt 1 ]; then echo "usage:$0 {start | restart | stop | status} " exit 5fiif [ $1 == start ]; then if [ -e $FilePath ]; then #判断文件是否存在 echo "$FilePath is running. " else touch $FilePath echo "starting $FileName successfully." fielif [ $1 == stop ]; then if [ -e $FilePath ]; then #判断文件是否存在 rm -f $FilePath echo "stop script finished." else echo "$FilePath is Stopped. " fielif [ $1 == restart ]; then if [ ! -e $FilePath ]; then #如果文件不存在 echo "$FilePath is stopped." else rm -f $FilePath touch $FilePath echo "restarting script successfully." fielif [ $1 == status ]; then if [ -e $FilePath ]; then echo "Script is running." elif [ ! -e $FilePath ]; then echo "script is stopped." fielse echo "Usage:$0 {start | restart | stop | status} " exit 7fi
The above script is the SYSV style service script
The Redhat series can execute these service scripts in the following ways
# Service Network Start
#/etc/rc.d/init.d/network Start
#/etc/init.d/network Start
This article is from the "Arvin Lau" blog, be sure to keep this source http://64314491.blog.51cto.com/2784219/1629219
If statement of Bash condition Judgment (ii)