This script is used in the common shell syntax of the work, the trivial grammar together can help to deal with work, liberation of manpower.
The main common shell knowledge points:
Determine if the parameters exist and determine the number of parameters
declaring functions
Judging string equality
The judgment string contains
Determine array contents and arrays, and loop arrays
If multi-conditional syntax
Sed
The script is recorded in order to record some basic grammar, the future can be forgotten to scroll. The basic grammar is used properly, and the work of operation and maintenance is better assisted. The script only applies to some special requirements.
#!/bin/bash
# 2017/1/11
#适用添加m站点的脚本. The way a domain name is changed to a directory agent.
List= ' echo ' | cut-d.-f1 '
Lister= ' echo ' | cut-d.-f2 '
Oldnip= ' echo ' | Sed ' s/\///g '
nip= ($oldnip)
Port= "$"
host=$ (hostname)
#判断是否在nginx服务器主机上执行, and two different variables were declared due to directory inconsistencies
if [$HOST = "tnginx101"];then
Route= "/app/nginx"
elif [$HOST = "tnginx102"];then
Route= "/usr/local/nginx"
Else
echo "$HOST not Nginx proxy"
Exit 2
Fi
directory= "/app/shell/mtemplate/msite"
Template= "/app/shell/mtemplate"
mproxy= "$ROUTE/conf/directory"
#判断参数个数是否正确
If [-z] && [z-$] && [-Z $];then
echo "Please input three arguments"
echo "Usage: $basename content.m.mallcoo.cn 9001 web101/web102"
Exit 2
Fi
If [$#-ne 3];then
echo "Input arguments error"
Exit 2
Fi
if [!-D $MPROXY];then
echo "not exist $MPROXY"
Exit 3
Fi
if [!-D $DIRECTORY]; then
echo "not exist" $DIRECTORY ""
Exit 2
Fi
Upstream_http () {
if [$LISTER = "M"];then
Cat $TEMPLATE/upstream_template > $DIRECTORY/server-port.conf
Sed-i ' s/template_template_http/' $LIST ' _m_http/g ' $DIRECTORY/server-port.conf
if [${#nip [@]}-GT 1];then
For i in ${nip[*]};d o
Sed-i '/upstream/a\ server ' $i ': ' $PORT ' weight=2 max_fails=2 fail_timeout=30s; ' $DIRECTORY/server-port.conf
Done
elif [${#nip [@]}-eq 1];then
Sed-i '/upstream/a\ server ' $nip ': ' $PORT ' weight=2 max_fails=2 fail_timeout=30s; ' $DIRECTORY/server-port.conf
Else
echo "Please confirm it"
Exit 2
Fi
Cat $DIRECTORY/server-port.conf >> $MPROXY/server-port.conf
Fi
#匹配域名 The second section contains the characters for the API. such as content.api.mallcoo.cn
if [[$LISTER =~ "API"]];then
Cat $TEMPLATE/upstream_template > $DIRECTORY/server-port.conf
Sed-i ' s/template_template_http/' $LIST ' _api_http/g ' $DIRECTORY/server-port.conf
if [${#nip [@]}-GT 1];then
For i in ${nip[*]};d o
Sed-i '/upstream/a\ server ' $i ': ' $PORT ' weight=2 max_fails=2 fail_timeout=30s; ' $DIRECTORY/server-port.conf
Done
elif [${#nip [@]}-eq 1];then
Sed-i '/upstream/a\ server ' $nip ': ' $PORT ' weight=2 max_fails=2 fail_timeout=30s; ' $DIRECTORY/server-port.conf
Else
echo "Please confirm it"
Exit 2
Fi
Cat $DIRECTORY/server-port.conf >> $MPROXY/server-port.conf
Fi
}
#追加 location_http
Location_http () {
if [$LISTER = "M"];then
Cat $TEMPLATE/location_template > $DIRECTORY/http.conf
Sed-i ' s/mould\/mould/a\/' $LIST '/g ' $DIRECTORY/http.conf
Sed-i ' s/template_template_http/' $LIST ' _m_http/g ' $DIRECTORY/http.conf
Cat $DIRECTORY/http.conf >> $MPROXY/http.conf
Fi
if [[$LISTER =~ "API"]];then
Cat $TEMPLATE/location_template > $DIRECTORY/http.conf
Sed-i ' s/mould\/mould/api\/' $LIST '/g ' $DIRECTORY/http.conf
Sed-i ' s/template_template_http/' $LIST ' _api_http/g ' $DIRECTORY/http.conf
Cat $DIRECTORY/http.conf >> $MPROXY/http.conf
Fi
}
Upstream_http
Location_http
This article is from the "Meng Sir accumulation" blog, please be sure to keep this source http://mengsir.blog.51cto.com/2992083/1891151
shell--record an advanced level of personal script