How to write shell scripts?

This is the back-end small class of the monastery. Each article is shared from

[background introduction] [knowledge analysis] [common problems] [solutions] [coding practice] [extended thinking] [more discussion] [References]

Eight aspects of in-depth analysis of back-end knowledge / skills. This article shares:

[how to write shell script?]

Hello, I am the 13th student of Shenzhen Branch of it Academy. I am an honest, pure and kind java programmer. Today, I'd like to share with you the knowledge points in Java task 2 on the official website of the Academy: how to write shell scripts? Try writing a simple script yourself. 1 Background: every time you release a Tomcat project, you have to repeatedly hit some commands. Before restarting tomcat, check whether the Tomcat process has stopped. If not, you have to manually kill the process. It's OK to deploy a project alone. If you deploy multiple projects at once, it's more difficult

2. Knowledge analysis 2.1 what is shell script? Shell script [1]. Shell script is similar to batch processing under Windows / DOS, that is, a program file that is pre placed into a file with various commands to facilitate one-time execution. It is mainly used for setting or management by administrators. However, it is more powerful than batch processing under windows and more efficient than programs edited by other programming programs. It uses commands under linux / Unix.

2.2 basic syntax of shell (refer to http://www.runoob.com/linux/linux-shell.html ) 1. The echo command is used to output text to the window 2 Define variable: your_ name=“”,war=1,dir="/ Root / deploywar / 1, dir = "/ root / deploywar / {war} 3. If statement: similar to that in Java, but one thing to pay attention to. If statement syntax format:

 if condition
    then
        command1 
        command2
        ...
        commandN 
    fi

Note: under this format, you must pay attention to fi 3 FAQ 1) what is a shell script? 2) How to write a simple script? 3) What is a deployment script?

4. Solution: master the basic shell syntax and, like writing code, calculate some commands through some logic to achieve the corresponding purpose.

5. Code the actual use method. Put the two shell scripts in the bin directory of tomcat, and Chmod + X gives the executable permissions to the two scripts, which can be used directly/ deploy. SH or/ restart. SH can be executed. Specific functions: 1 deploy. SH first delete the war package of the project under the webapps directory (Tomcat will automatically delete the folder extracted from the war package). 2. Shutdown close Tomcat. If it is not closed after 3S, kill the process with kill - 9. 3. Deploy the war package uploaded to the specified location to Tomcat and restart tomcat through restart.sh. the following code is attached:

#!/bin/sh
war=$1
dir="/root/deployWar/${war}"
echo "-------project file:${dir}-------"
bin=$(cd `dirname $0`; pwd)
echo "-------current directory ${bin}--------"
if [ ! -n "${war}" ]; then
    echo "-------Usage: $0 [project.war]-------"
    exit 0
fi
if [ ! -f "${dir}" ]; then
    echo "-------Error: ${dir} does not exist.--------"
    exit 0
fi
if [ ! "${war##*.}" ="war" ]; then
    echo "-------Error:${war} is not a war file.--------"
    exit 0
fi
echo "-------Deploy ${war##*/}...-------"
if [ -f "${bin}/../webapps/${war}" ]; then
    rm -rf ${bin}/../webapps/${war}
    echo "-------delete: ${bin}/../webapps/${war}------"
fi
cp -f ${dir} ${bin}/../webapps/
echo "Restart tomcat..."
exec ${bin}/restart.sh

restart. SH file content:

#!/bin/sh

bin=$(cd dirname $0; pwd)
pid=$(ps aux | grep tomcat | grep -v grep | grep -v restart | grep ${bin} | awk '{print $2}')

if [ -n "${pid}" ]; then
echo "Shutdow...."
sh ${bin}/shutdow.sh
sleep 3

pid=$(ps aux | grep tomcat | grep -v grep | grep -v restart | grep ${bin} | awk '{print $2}')
if [ -n "${pid}" ]; then
    kill -9 &{pid}
    slepp 1
fi

fi
echo "Startup..."
sh ${bin}/startup.sh
if [ "$1" = "-v" ]; then
tail -f ${bin}/../logs/catalina.out
fi

6. Extension thinking: how to deploy different services and other functions with one click, such as one click deployment to the specified rensin, jetty and Tomcat. 7. References: CSDN, Baidu Encyclopedia,

8 more discussion summary: 1) what is a shell script? Through some shell syntax and logic processing, some liunx commands are combined to complete specific functions. 2) How to write a simple script? Master the basic shell syntax and, like writing code, calculate some commands through some logic to achieve the corresponding purpose. 3) What is a deployment script? The deployment script is to deploy the war package under the specified folder to Tomcat with one click.

Warm tip: if you are a beginner of Java, please point it out. Thank you!

Skill tree It Academy

"We believe that everyone can become an engineer. From now on, find a senior brother to introduce you, control your learning rhythm, and stop being confused on the way to learning.".

Here is the skill tree In it academy, thousands of senior brothers have found their own learning route here. Learning is transparent and growth is visible. Senior brothers have 1-to-1 free guidance.

Come and learn with me ~ skill tree It Academy

"We believe that everyone can become an engineer. From now on, find a senior brother to introduce you, control your learning rhythm, and stop being confused on the way to learning.".

Here is the skill tree In it academy, thousands of senior brothers have found their own learning route here. Learning is transparent and growth is visible. Senior brothers have 1-to-1 free guidance.

Come and study with me~

Tencent Video: https://v.qq.com/x/page/x0726am2pk6.html

Ppt link video link

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>