Arrays have been used before, and the following describes a practical function of their work
background of the work that appears :
The company test environment of Tomcat WebApps has 3 projects, if you press the usual release script, you need to write a single publish script for each project, I feel too much trouble, I would like to be able to use a script to solve the 3 projects, the first is to define three variables in the script to get the corresponding package name, After the upload of the directory to judge, if the package exists to publish, although the implementation of the process is a bit cumbersome, and the definition of the variable is written dead, in the future if there is a need to re-use the need for additional modifications, it is troublesome, the recent review of the array when the function of the array, It is found that using arrays with a For loop only takes a few lines to solve the problem, and future reuse does not require a modification of the script, only a few variables to replace.
actual script
#!/bin/bash#author by chawan#date 2017/05/18# requires the current user to have the right to execute the script, you can set the # script function in the/etc/sudoers: 1, The normal turn off Tomcat, and then the PID to prevent Tomcat does not really shut down, 2, if Tomcat set up the automatic deployment of the if ture process, if not, then the If false process, 3, view the log. #NAME =test #取包名TOMCAT =/tomcat #tomcat家目录WEB = $TOMCAT/webapps # War Package storage directory pid= ' ps aux|grep -v grep|grep "$TOMCAT \>" |awk ' {print $2} ' # Take the piddate= ' date +%y-%m-%d-%h:%m:%s ' #定义时间戳 that launches TOMCAT to save the old package logs= $TOMCAT/logs # Define log directory old=/opt/old_$name #定义存旧包的目录变量WAR =/opt/war_$name #定义存放新包的目录变量declare -a war_array # Defines an array to hold the package name under the WAR path war_array= ($ (ls $WAR)) #tomcat的stop函数: Stop and Clear Cache Function killtomcat () { sh $TOMCAT/bin/shutdown.sh cd $TOMCAT/work/catalina rm -rf localhost && echo "Cache is cleaned" &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;#&NBSP;&NBSP,} #查看日志看是否发布成功function taillogs () { tail -f $LOGS/catalina.out #}# Defining function Functions: Auto release Function autodeploy () { deploy_cp /etc/init.d/tomcat8080 start deploy_mv} #定义功能函数, the top half of the release, will require a new package to be removed from the old package, The legacy package is migrated to the old directory, and the new package is copied to the Web directory under function deploy_cp () { for (i=0;i<${#war_ array[*]};i++)) &NBSP;&NBSP;&NBSP;&NBSP;DO&NBSP;&NBsp; cd $WEB && rm -rf ${war_array[$i]:1:3} && echo "${war_array[$i]} file is deleted" mv -f ${war_array[$i]} $OLD &> /dev/null & & echo "${war_array[$i]} is moved $OLD" cp $WAR/${war_array[$i]} $WEB && echo ${war_array[$i]} package is moved to $WEB " done} #定义功能函数, the next part of the release, adds the old package time stamp to record the time of the publication. Delete the new package under the war path for the next upload function deploy_mv () { for ((n=0;n<${#war_array [*]} ; n++)) do cd $OLD && echo "dir change to $OLD" cp ${ war_array[$n]} ${war_array[$n]} $DATE .war && echo "old ${war_array[$n]} package mv over" cd $WAR && rm -f ${war_array[$n]} && echo "in $WAR ' s ${war_array[$n]} is deleted" done}function deploy () {Clearkilltomcatautodeploytaillogs}deploy
Script Explanation :
The above defines 8 variables ,an array ,6 functions implemented according to the directory of existing packages, to publish;
8 Variables :
The file name of the package, the path of Tomcat, the path of the WebApps, the PID of Tomcat, the timestamp, the path of the log, the path where the new package was uploaded, and the path where the old package was located are defined separately.
an array :
Package name for uploading a new package
6 Functions :
Killtomcat function
function Killtomcat () {SH $TOMCAT/bin/shutdown.sh cd $TOMCAT/work/catalina rm-rf Lo Calhost && echo "Cache is cleaned" #}
Used to close Tomcat and clean up the cache
Taillogs function
function Taillogs () {tail-f $LOGS/catalina.out #}
To see if the publication was successful
function Deploy_cp () {for (i=0;i<${#war_array [*]};i++)] do CD $WEB && rm-rf ${war_array[$i]:1:3 } && echo "${war_array[$i]} file is deleted" Mv-f ${war_array[$i]} $OLD &>/dev/null && Ech O "${war_array[$i]} is moved $OLD" CP $WAR/${war_array[$i]} $WEB && echo ' ${war_array[$i]} package is moved To $WEB ' done}
DEPLOY_CP function
function Deploy_cp () {for (i=0;i<${#war_array [*]};i++)] do CD $WEB && rm-rf ${war_array[$i]:1:3 } && echo "${war_array[$i]} file is deleted" Mv-f ${war_array[$i]} $OLD &>/dev/null && Ech O "${war_array[$i]} is moved $OLD" CP $WAR/${war_array[$i]} $WEB && echo ' ${war_array[$i]} package is moved To $WEB ' done}
This function mainly consists of 4 steps:
1, traverse the upload package file, get the uploaded package name;
2, will now WebApps under the same name as the uploaded package folder deleted;
RM-RF ${war_array[$i]:1:3}
This step is to delete the Unpacked folder with the same name as the package, assuming my package name is Abc.war,
${war_array[$i]} the content is Abc.war, and I want to delete the previously unpacked ABC folder, by defining a new variable can naturally be implemented, but the array has the function of slicing, that is, the contents of the arrays are cut, so we can ${war_ array[$i]:1:3} to achieve ABC acquisition, the specific concept of the previous array of articles
http://chawan.blog.51cto.com/9179874/1851443
3, the WebApps under the same name with the upload of the war package into the old package folder;
4. Copy the uploaded new package to the WebApps path.
DEPLOY_MV function
function Deploy_mv () {for ((n=0;n<${#war_array [*]};n++)) does CD $OLD && echo "dir change to $OLD" CP ${war_array[$n]} ${war_array[$n]} $DATE. War && Echo ' Old ${war_array[$n '} package MV-over "CD $WAR && rm-f ${war_array[$n]} && echo "in $WAR ' s ${war_array[$n]} was deleted" Done}
This function also contains 3 steps:
1, traverse the upload package file, get the uploaded package name;
2. Add a timestamp for the war package just copied to the old package folder, while preserving the original war package for backup and rollback;
3, the new package will be uploaded to remove the next upload easily.
Autodeploy function
function Autodeploy () {deploy_cp/etc/init.d/tomcat8080 start DEPLOY_MV}
The role of this function: the core process of implementing a release
Deploy function
function Deploy () {clearkilltomcatautodeploytaillogs}
The function: Implement the complete publishing process
Summary :
Above just introduced the actual function of the array, the current work because my shell script is not much, so the use of the array is still very jerky, the above example is the result of thinking, although it seems very young, but it is after all, it is also through their own thinking. Write a blog here to record a bit. Big God look, a smile and can be, novice may reference, practice array.
This article is from "Zhang Fan-it's fantasy drifting" blog, please be sure to keep this source http://chawan.blog.51cto.com/9179874/1957162
Bash real-world application of arrays in work (test environment release script)