天天看点

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下是日志。