Learn bash shell from a linux script
Here is a resin startup script in which you can learn a lot about bash shell
#! / bin/sh / / bash shell script
#
# Linux startup script for Resin / / # comments
# chkconfig: 345 85 15
# description: Resin is a Java Web server.
# processname: wrapper.pl
#
# To install, configure this file as needed and copy init.resin
# to / etc/rc.d/init.d as resin. Then use "# / sbin/chkconfig resin reset"
#
JAVA_HOME=/usr/local/java/jdk / / = sign assignment
RESIN_HOME=/usr/local/resin
Export JAVA_HOME RESIN_HOME / / export defines global variables
JAVA=$JAVA_HOME/bin/java / / $represents a variable
#
# If you want to start the entire Resin process as a different user
# set this to the user name. If you need to bind to a protected port
# e.g. Port 80, you can't use USER, but will need to use bin/resin.
#
USER=
#
# Set to the server id to start
#
# SERVER= "- server app-a"
#
ARGS= "- resin-home $RESIN_HOME $SERVER"
If test-r / lib/lsb/init-functions; then / / judgment statement test-r / lib/lsb/init-functions condition file exists and can be read
. / lib/lsb/init-functions / /. Representative source is the same as source / lib/lsb/init-functions
Else / / if the above conditions are not met
Log_daemon_msg () {/ / function
If [- z "$1"]; then / /-z "$1" variable $1 length is 0 / / $1 the first parameter passed when the script is executed
Return 1 / / return
Fi
If [- z "$2"]; then
Echo-n "$1:" / /-n parameter output does not wrap
Return
Fi
Echo-n "$1: $2"
}
Log_end_msg () {
Execute return 1 if [- z "$1"] & & return 1 / / [- z "$1"] is true
If [$1-eq 0]; then / / compare,-eq equals
Echo "."
Else
Echo "failed!"
Fi
Return $1
}
Fi
Case "$1" in / / case Select statement
Start) / / the first parameter of the script is start
Log_daemon_msg "Starting resin" / / calls the function, passing parameters
If test-n "$USER"; then / / condition: variable USER length is not 0
Su $USER-c "$JAVA-jar $RESIN_HOME/lib/resin.jar $ARGS start" 1 > / dev/null 2 > / dev/null / / su restores identity after another user executes the command-c
Else
$JAVA-jar $RESIN_HOME/lib/resin.jar $ARGS start 1 > / dev/null 2 > / dev/null / / 1 > dev/null execution normal information output to empty file 2 > error message
Fi
Log_end_msg $? / $? The result of the last command execution 0 indicates success. For more information, please see other articles.
Stop)
Log_daemon_msg "Stopping resin"
If test-n "$USER"; then
Su $USER-c "$JAVA-jar $RESIN_HOME/lib/resin.jar $ARGS stop" 1 > / dev/null 2 > / dev/null
Else
$JAVA-jar $RESIN_HOME/lib/resin.jar $ARGS stop 1 > / dev/null 2 > / dev/null
Fi
Log_end_msg $?
Restart)
$0 stop / / $0 script name
$0 start
*) / / except for start stop restart
Echo "Usage: $0 {start | stop | restart}"
Exit 1 / / 1 indicates an error
Esac
Exit 0 / / 0 indicates no error