shell--record an advanced level of personal script

Source: Internet
Author: User

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:

    1. Determine if the parameters exist and determine the number of parameters

    2. declaring functions

    3. Judging string equality

    4. The judgment string contains

    5. Determine array contents and arrays, and loop arrays

    6. If multi-conditional syntax

    7. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.