learning shell script

#!/bin/sh

## java env
export JAVA_HOME=/usr/local/java/jdk1.7.0_72
export JRE_HOME=$JAVA_HOME/jre

## service name
APP_NAME=user

SERVICE_DIR=/home/wusc/edu/service/$APP_NAME
SERVICE_NAME=edu-service-$APP_NAME
JAR_NAME=$SERVICE_NAME\.jar
PID=$SERVICE_NAME\.pid

cd $ SERVICE_DIR

case "$1" in

    start)
        nohup $JRE_HOME/bin/java -Xms256m -Xmx512m -jar $JAR_NAME >/dev/null 2>&1 &
        echo $! > $SERVICE_DIR/$PID
        echo "=== start $SERVICE_NAME"
        ;;

    stop)
        kill `cat $SERVICE_DIR/$PID`
        rm -rf $SERVICE_DIR/$PID
        echo "=== stop $SERVICE_NAME"

        sleep 5
		##
		## edu-service-aa.jar
		## edu-service-aa-bb.jar
        P_ID=`ps -ef | grep -w "$SERVICE_NAME" | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "=== $SERVICE_NAME process not exists or stop success"
        else
            echo "=== $SERVICE_NAME process pid is:$P_ID"
            echo "=== begin kill $SERVICE_NAME process, pid is:$P_ID"
            kill -9 $P_ID
        be
        ;;

    restart)
        $0 stop
        sleep 2
        $0 start
        echo "=== restart $SERVICE_NAME"
        ;;

    *)
        ## restart
        $0 stop
        sleep 2
        $0 start
        ;;

esac
exit 0




On the shell:
0 means standard input
1 means standard output
2 means standard error output
> The default is standard output redirection, same as 1>
2>&1 means redirect standard error output to standard output.
&>file means redirect standard output Both output and standard error output are redirected to the file file


Variable description:
$$
PID of Shell itself (ProcessID)
$!
The PID of the background Process that Shell last ran
$?
The end code (return value) of the last command to run
$-
use List of Flags set by the Set command
$* List
of all parameters. For example, when "$*" is enclosed in """, all parameters are output in the form of "$1 $2 ... $n".
$@
A list of all parameters. For example, when "$@" is enclosed in """, "$1" "$2" ... "$n" output all parameters in the form of .
$#
The number of parameters added to the shell
$0
The file name of the shell itself
$1~$n
are added to each parameter value of the shell. $1 is the first parameter, $2 is the second parameter... .
Let's write a simple script first, and then explain the meaning of each variable after execution
# touch variable
# vi variableThe
script content is as follows:
#!/bin/sh
echo "number:$#"
echo "scname:$0"
echo "first :$1"
echo "second:$2"
echo "argume:$@"
Save and exit
Give the script execution Permissions
# chmod +x variable
execute script
# ./variable aa bb
number:2
scname:./variable
first: aa
second:bb
argume:aa bb
You can see by displaying the result:
$# is the number of parameters passed to the script
$0 is the name of the script itself
$1 is the first parameter
passed to the shell script $2 is the second parameter passed to the shell script
$@ is a list of all parameters passed to the script



Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326566698&siteId=291194637