天天看點

shell中的函數、數組、告警系統分析

1、#!/bin/bash

function inp(){

echo "The first parameter is $1"

echo "The second parameter is $2"

echo "The third parameter is $3"

echo "The number of parameter is $#"

echo "The script's name is $0"

}

#定義一個函數

inp a b c asd sdg

運作結果:

The first parameter is a

The second parameter is b

The third parameter is c

The number of parameter is 5

The script's name is f1.sh

2、#!/bin/bash

inp $1 $2 $3

[root@centos7 shell]# sh f1.sh a b c d

The number of parameter is 3

3、sum() {

s=$[$1+$2]

echo "$s"

sum $1 $2

[root@centos7 shell]# sh f1.sh 2 4

6、#!/bin/sh

ip(){

ifconfig |grep -A1 "$1: " |tail -1|awk '{print $2}'

read -p 'please network name: ' n

myip=<code>ip $n</code>

echo "input ip:"$myip

[root@centos7 shell]# sh ip.sh 

please network name: ens37

input ip:192.168.136.128

所謂數組,就是相同資料類型的元素按一定順序排列的集合,就是把有限個類型相同的變量用一個名字命名,在Shell中,用括号來表示數組,數組元素用“空格”符号分割開。

1、定義數組

a=(1,2,3,4,a)

2、檢視

[root@centos7 shell]# echo ${a[*]}

1,2,3,4,a

數組的分片

[root@centos7 shell]# a=(<code>seq 1 10</code>)

1 2 3 4 5 6 7 8 9 10

3、從第一個元素開始截取第三個

1 2 3

4、倒數第三個元素開始截取3個

8 9 10

5、更改元素

[root@centos7 shell]# a[1]=90

You have new mail in /var/spool/mail/root

1 90 3 4 5 6 7 8 9 10

6、新增元素

[root@centos7 shell]# a[11]=11

1 90 3 4 5 6 7 8 9 10 11

需求:使用shell定制各種個性化告警工具,但是需要統一化管理、規範化管理。

思路:指定一個腳本包,包含主程式、子程式、配置檔案、郵件引擎、輸出日志等等。

主程式:作為整個程式的入口;

配置檔案:是一個控制中心,它用來開關各個子程式,指定各個相關聯的日志檔案;

子程式:這才是真正的監控腳本,用來監控各個名額;

郵件引擎:是由一個Python程式來實作,它可以定義發郵件的伺服器、發郵件人以及發郵件密碼;

輸出日志:整個監控系統要有日志輸出。

要求:

機器的角色多種多樣,但是所有的機器上要部署同樣的監控系統,也就是說所有的機器不管什麼角色,整個程式架構是一樣的,不同的地方在于根據不同的角色定制不同的配置檔案。

程式架構:

shell中的函數、數組、告警系統分析

bin:主程式

conf:配置檔案

shares:各個監控腳本

mail:郵件引擎

log:日志

本文轉自 iekegz 51CTO部落格,原文連結:http://blog.51cto.com/jacksoner/2045919,如需轉載請自行聯系原作者