1. 程式人生 > >shell中的函數、shell的數組、告警系統需求分析

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

pri 腳本 格式 定制 分享 個數 主程 密碼 參數

shell中的函數

函數就是把一段代碼整理到了一個小單元中,並給這個小單元起一個名字,當用到這段代碼時直接調用這個小單元的名字即可。

格式: function f_name() {
 ?? ?? ?? ?? ?? ?? ? command? ?? ?? ?? ? 
        }
函數必須要放在最前面
  • 腳本示例1(用來定義函數打印參數)
[root@garytao-01 aming]# vim fun1.sh
[root@garytao-01 aming]# cat fun1.sh 
#!/bin/bash
function inp(){
    echo $1 $2 $3 $0 $#
}

inp 1 a 2
[root@garytao-01 aming]# sh fun1.sh 
1 a 2 fun1.sh 3
[root@garytao-01 aming]# sh -x fun1.sh 
+ inp 1 a 2
+ echo 1 a 2 fun1.sh 3
1 a 2 fun1.sh 3

[root@garytao-01 aming]# vi fun1.sh 
[root@garytao-01 aming]# cat fun1.sh 
#!/bin/bash
function inp(){
    echo "The first par is $1"
    echo "The second par is $2"
    echo "The third par is $3"
    echo "the scritp name is $0"
    echo "the number of par is $#"
}

inp b a 2 3 adf
[root@garytao-01 aming]# sh fun1.sh 
The first par is b
The second par is a
The third par is 2
the scritp name is fun1.sh
the number of par is 5
[root@garytao-01 aming]# sh -x fun1.sh 
+ inp b a 2 3 adf
+ echo ‘The first par is b‘
The first par is b
+ echo ‘The second par is a‘
The second par is a
+ echo ‘The third par is 2‘
The third par is 2
+ echo ‘the scritp name is fun1.sh‘
the scritp name is fun1.sh
+ echo ‘the number of par is 5‘
the number of par is 5
[root@garytao-01 aming]# 
  • 腳本示例2(用來定義一個加法的函數)
[root@garytao-01 aming]# vim fun2.sh
[root@garytao-01 aming]# cat fun2.sh 
#!/bin/bash
sum() {
    s=$[$1+$2]
    echo $s
}

sum 1 2
[root@garytao-01 aming]# sh fun2.sh 
3
[root@garytao-01 aming]# sh -x fun2.sh 
+ sum 1 2
+ s=3
+ echo 3
3
[root@garytao-01 aming]# 
  • 腳本示例3(用來定義顯示IP)
[root@garytao-01 aming]# vim fun3.sh 
[root@garytao-01 aming]# cat fun3.sh 
#!/bin/bash
ip()
{
    ifconfig |grep -A1 "$1: "|awk ‘/inet/ {print $2}‘
}

read -p "Please input the eth name: " eth
ip $eth
[root@garytao-01 aming]# sh fun3.sh 
Please input the eth name: ens33
172.16.111.100
[root@garytao-01 aming]# sh -x fun3.sh 
+ read -p ‘Please input the eth name: ‘ eth
Please input the eth name: ens33
+ ip ens33
+ grep -A1 ‘ens33: ‘
+ awk ‘/inet/ {print $2}‘
+ ifconfig
172.16.111.100

shell中的數組

1.shell中的數組1
  • 定義數組 a=(1 2 3 4 5); echo ${a[@]}
  • echo ${#a[@]} 獲取數組的元素個數
  • echo ${a[2]} 讀取第三個元素,數組從0開始
  • echo ${a[*]} 等同於 ${a[@]} 顯示整個數組
    數組賦值
  • a[1]=100; echo ${a[@]}
  • a[5]=2; echo ${a[@]} 如果下標不存在則會自動添加一個元素
    數組的刪除
  • unset a; unset a[1]
[root@garytao-01 aming]# b=(1 2 3)
[root@garytao-01 aming]# echo ${b[@]}
1 2 3
[root@garytao-01 aming]# echo ${b[*]}
1 2 3
[root@garytao-01 aming]# echo ${b[1]}
2
[root@garytao-01 aming]# echo ${b[2]}
3
[root@garytao-01 aming]# echo ${b[0]}
1
[root@garytao-01 aming]# echo ${#b[@]}
3
[root@garytao-01 aming]# b[3]=a
[root@garytao-01 aming]# echo ${b[*]}
1 2 3 a
[root@garytao-01 aming]# b[3]=aaa
[root@garytao-01 aming]# echo ${b[*]}
1 2 3 aaa
[root@garytao-01 aming]# unset b[3]  //刪除數組
[root@garytao-01 aming]# echo ${b[*]}
1 2 3
[root@garytao-01 aming]# unset b
[root@garytao-01 aming]# echo ${b[*]}

[root@garytao-01 aming]# 
2.shell中的數組2
  • 數組分片
  • a=(seq 1 5)
  • echo ${a[@]:0:3} 從第一個元素開始,截取3個
  • echo ${a[@]:1:4} 從第二個元素開始,截取4個
  • echo ${a[@]:0-3:2} 從倒數第3個元素開始,截取2個
    數組替換
  • echo ${a[@]/3/100}
  • a=(${a[@]/3/100})
[root@garytao-01 aming]# a=(`seq 1 10`)
[root@garytao-01 aming]# echo ${a[*]}
1 2 3 4 5 6 7 8 9 10
[root@garytao-01 aming]# echo ${a[@]:3:4}
4 5 6 7
[root@garytao-01 aming]# echo ${a[@]:0-3:2}
8 9
[root@garytao-01 aming]# echo ${a[@]/8/6}
1 2 3 4 5 6 7 6 9 10
[root@garytao-01 aming]# a=(${a[@]/8/6})
[root@garytao-01 aming]# echo ${a[@]}
1 2 3 4 5 6 7 6 9 10
[root@garytao-01 aming]# 

告警系統需求分析

1.shell項目-告警系統
  • 需求:使用shell定制各種個性化告警工具,但需要統一化管理、規範化管理。
  • 思路:指定一個腳本包,包含主程序、子程序、配置文件、郵件引擎、輸出日誌等。
  • 主程序:作為整個腳本的入口,是整個系統的命脈。
  • 配置文件:是一個控制中心,用它來開關各個子程序,指定各個相關聯的日誌文件。
  • 子程序:這個才是真正的監控腳本,用來監控各個指標。
  • 郵件引擎:是由一個python程序來實現,它可以定義發郵件的服務器、發郵件人以及發件人密碼
  • 輸出日誌:整個監控系統要有日誌輸出。

  • 要求:我們的機器角色多種多樣,但是所有機器上都要部署同樣的監控系統,也就說所有機器不管什麽角色,整個程序框架都是一致的,不同的地方在於根據不同的角色,定制不同的配置文件。

技術分享圖片

bin下是主程序;
conf下是配置文件;
shares下是各個監控腳本;
mail下是郵件引擎;
log下是日誌。

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