天天看點

shell腳本中的函數,shell中的數組,shell項目-告警系統shell腳本中的函數shell中的數組shell項目-告警系統需求分析

shell腳本中的函數

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

    function f_name() {

    command

  • 示列1,列印shell的參數
    [root@akuilinux01 shell]# cat fun1.sh 
    #!/bin/bash
    function inp(){
    echo "the first par is $1"
    echo "the sedcond 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 sjkdj
    [root@akuilinux01 shell]# sh fun1.sh 
    the first par is b
    the sedcond par is a
    the third par is 2
    the scritp name is fun1.sh
    the number of par is 5
    第一個參數是b,第二個是a,第三個是2,腳本名是fun1.sh,參數個數是5.           
  • 也可以定義函數的參數
    [root@akuilinux01 shell]# cat fun1.sh 
    #!/bin/bash
    function inp(){
    echo "the first par is $1"
    echo "the sedcond par is $2"
    echo "the third par is $3"
    echo "the scritp name is $0"
    echo "the number of par is $#"
    }
    inp $1 $2 $3
    [root@akuilinux01 shell]# sh fun1.sh 1 2 3
    the first par is 1
    the sedcond par is 2
    the third par is 3
    the scritp name is fun1.sh
    the number of par is 3           
  • [root@akuilinux01 shell]# cat fun2.sh 
    #!/bin/bash
    sum(){
    s=$[$1+$2]
    echo $s
    }
    sum 1 10 
    [root@akuilinux01 shell]# sh fun2.sh 
    11           
  • 示列3,輸入網卡名,得到ip
函數的核心指令推演
[root@akuilinux01 shell]# ifconfig |grep -A1 "ens33"
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.21.128  netmask 255.255.255.0  broadcast 192.168.21.255
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.21.133  netmask 255.255.255.0  broadcast 192.168.21.255
[root@akuilinux01 shell]# ifconfig |grep -A1 "ens33: "
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.21.128  netmask 255.255.255.0  broadcast 192.168.21.255
[root@akuilinux01 shell]# ifconfig |grep -A1 "ens33: "|awk '/inet/'
        inet 192.168.21.128  netmask 255.255.255.0  broadcast 192.168.21.255
[root@akuilinux01 shell]# ifconfig |grep -A1 "ens33: "|awk '/inet/'|awk -F ' ' '{print$2}'
192.168.21.128

[root@akuilinux01 shell]# cat fun3.sh 
#!/bin/bash
ip()
{
 ifconfig |grep -A1 "$1: "|awk '/inet/'|awk -F ' ' '{print$2}' 
}
while :
do
  read -p "Please input the eth name:" eth
  n=`ifconfig|grep "$eth"`
  if [ -z "$eth" ]
    then
      echo "你必須輸入一個網卡名"
    elif [ -z "$n" ]
      then
        echo "請你輸入正确的網卡名"
        continue
    else
      break
   fi
done
ip_=`ip $eth`
if [ -z "$ip_" ]
  then
    echo "$eth:這個網卡沒有配置ip"
  else
    echo "$eth:$ip_" 
fi
[root@akuilinux01 shell]# sh fun3.sh 
Please input the eth name:
你必須輸入一個網卡名
Please input the eth name:dada
請你輸入正确的網卡名
Please input the eth name:ens33
ens33:192.168.21.128
[root@akuilinux01 shell]# sh fun3.sh 
Please input the eth name:ens37
ens37:這個網卡沒有配置ip           

shell中的數組

  • 定義數組,a=(1 2 3 4);echo ${a[@]}.這裡的數字也能寫為字元串
    [root@akuilinux01 shell]# a=(as b cd)
    [root@akuilinux01 shell]# echo ${a[@]}
    as b cd
    [root@akuilinux01 shell]# echo ${a[*]}
    as b cd           
  • echo ${a[2]} 讀取第三個元素,數組從0開始
    [root@akuilinux01 shell]# echo ${a[2]}
    cd
    [root@akuilinux01 shell]# echo ${a[1]}
    b
    [root@akuilinux01 shell]# echo ${a[0]}
    as           
  • echo ${#a[@]} 擷取數組的元素個數
    [root@akuilinux01 shell]# echo ${#a[*]}
    3           
  • 數組指派與更改,如果下标不存在則會自動添加一個元素
    [root@akuilinux01 shell]# a[5]=4
    [root@akuilinux01 shell]# echo ${#a[*]}
    4
    [root@akuilinux01 shell]# echo ${a[*]}
    as b cd 4
    [root@akuilinux01 shell]# a[1]=2
    [root@akuilinux01 shell]# echo ${a[*]}
    as 2 cd 4           
  • 數組的删除
    [root@akuilinux01 shell]# unset a[0]
    [root@akuilinux01 shell]# echo ${a[*]}
    2 cd 4
    [root@akuilinux01 shell]# unset a
    [root@akuilinux01 shell]# echo ${a[*]}           
  • 數組分片
    • 從第一個開始截取3個
    • 從倒數第三個開始截取2個
      8 9           
  • 數組替換
    [root@akuilinux01 shell]# echo ${a[@]/7/5}
    1 2 3 4 5 6 5 8 9 10
    [root@akuilinux01 shell]# a=(${a[@]/8/3})
    [root@akuilinux01 shell]# echo ${a[@]}
    1 2 3 4 5 6 7 3 9 10           

    shell項目-告警系統需求分析

  • 需求:使用shell定制各種個性化告警工具,但需要統一化管理、規範化管理。
  • 思路:指定一個腳本包,包含主程式、子程式、配置檔案、郵件引擎、輸出日志等。
  • 主程式:作為整個腳本的入口,是整個系統的命脈。
  • 配置檔案:是一個控制中心,用它來開關各個子程式,指定各個相關聯的日志檔案。
  • 子程式:這個才是真正的監控腳本,用來監控各個名額。
  • 郵件引擎:是由一個python程式來實作,它可以定義發郵件的伺服器、發郵件人以及發件人密碼
  • 輸出日志:整個監控系統要有日志輸出。
  • 要求:我們的機器角色多種多樣,但是所有機器上都要部署同樣的監控系統,也就說所有機器不管什麼角色,整個程式架構都是一緻的,不同的地方在于根據不同的角色,定制不同的配置檔案。
  • 程式架構:
    (主目錄 mon)
                                                   |
     --------------------------------------------------------------------------------------
     |                   |                         |                              |                       |
    bin             conf                  shares                      mail                  log
    [main.sh]  [mon.conf]  [load.sh 502.sh] [mail.py mail.sh]  [mon.log err.log ]
    bin下是主程式
    conf下是配置檔案
    shares下是各個監控腳本
    mail下是郵件引擎
    log下是日志。