Functions, arrays, and alarm system requirements analysis in shell

1. Functions in the shell
• A function is to organize a piece of code into a small unit, and give this small unit a name. When using this code, you can directly call the name of this small unit.
Format: function f_name() {
                      command
             }
function must be first

Example 1: print parameters, $1 indicates the first parameter, $0 indicates the file name $# indicates the number of parameters
#!/bin/bash
function inp(){ #define 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 alk 3 bm
  Script running result
[root@lz01 shell]# sh hs1.sh
The first par is a
The second par is l
The third par is k
The scritp name is hs1.sh
The number of par is 5

Example 2: Defining a function for addition
#!/bin/bash
sum() {
    s=$[$1+$2]
    echo $s
}

sum 10 20

Example 3: Enter the name of the network card and display the IP address of the network card
#!/bin/bash
ip()
{
    ifconfig |grep -A1 "$1: " |awk '/inet/ {print $2}'
}

read -p "Please input the eth name: " eth
ip $eth


2. Arrays in the shell
•Define array a=(1 2 3 4 5); echo ${a[@]}
• echo ${a[2]} reads the third element, the array starts at 0
• echo ${a[*]} is equivalent to ${a[@]} prints the entire array
[root@lz01 shell]# a=(1 2 3 4 5)
[root@lz01 shell]# echo ${a[@]}
1 2 3 4 5
[root@lz01 shell]# echo ${a[*]}
1 2 3 4 5
[root@lz01 shell]# echo ${a[0]}
1
[root@lz01 shell]# echo ${a[2]}
3
• echo ${#a[@]} to get the number of elements in the array
[root@lz01 shell]# echo ${#a[@]}
5
• Array assignment and modification, if the subscript does not exist, an element is automatically added
[root@lz01 shell]# a[5]=ss
[root@lz01 shell]# echo ${a[@]}
1 2 3 4 5 ss
[root@lz01 shell]# a[5]=nn
[root@lz01 shell]# echo ${a[@]}
1 2 3 4 5 nn
• To delete an array, use unset
[root@lz01 shell]# unset a[5]
[root@lz01 shell]# echo ${a[@]}
1 2 3 4 5
[root@lz01 shell]# unset a
[root@lz01 shell]# echo ${a[@]}

• Array sharding
   echo ${a[@]:3:4} starts from the first element, intercepts 3
   echo ${a[@]:0-3:2} starts from the 3rd last element, intercepts 2
[root@lz01 shell]# echo ${a[@]}
1 2 3 4 5 6 7 8 9 10
[root@lz01 shell]# echo ${a[@]:3:4}
4 5 6 7
[root@lz01 shell]# echo ${a[@]:0-3:2}
8 9
• Array replacement
   echo ${a[@]/8/ss} replace 8 with ss
[root@lz01 shell]# echo ${a[@]/8/ss}
1 2 3 4 5 6 7 ss 9 10
• a=(${a[@]/8/cc})   也可以直接复制
[root@lz01 shell]# a=(${a[@]/8/cc})
[root@lz01 shell]# echo ${a[@]}
1 2 3 4 5 6 7 cc 9 10

三、告警系统需求分析
•需求:使用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下是日志。








Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324897088&siteId=291194637