20.16/20.17 shell中的函数
20.18 shell中的数组
20.19 告警系统需求分析
20.16 shell中的函数(上)

函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这个小单元的名字即可。
格式: function 函数的名字 { command }

函数必须要放在最前面

定义函数的时候最好不要与shell的表达语句冲突

案列1 这个函数用来打印你的参数的

用来打印参数

#!/bin/bash
function input() {
echo $1 $2 $# $0
}
input 1 a b

下面来写函数
[root@aming-01 ~]# cd shell
[root@aming-01 shell]# cd aming
[root@aming-01 aming]# vi fun1.sh

[root@aming-01 aming]# vim fun1.sh

#!/bin/bash
function inp(){
echo $1 $2 $3 $0 $#
}

inp 1 a 2
~




:wq 
函数,可以直接写在脚本内,相当于直接调用 內建变量 $1 第一个参数 $2 第二个参数 … ~ $# 参数名字 $0 总共有几个参数

下面来执行下该函数脚本

[root@aming-01 aming]# vim fun1.sh
[root@aming-01 aming]# sh fun1.sh
1 a 2 fun1.sh 3
[root@aming-01 aming]# 
再来修改下
[root@aming-01 aming]# !vi
vim 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 nuber of par is $#"
}

inp b a 2 3 adf
~


:wq 
[root@aming-01 aming]# vim fun1.sh
[root@aming-01 aming]# sh fun1.sh
1 a 2 fun1.sh 3
[root@aming-01 aming]# !vi
vim fun1.sh
[root@aming-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 nuber of par is 5
[root@aming-01 aming]#

可以把参数放到外面
[root@aming-01 aming]# vi 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 nuber of par is $#"
}

inp $1 $2 $3
~



:wq
这里的 $1 $2 $3表示fun1的 第一个参数,fun1的第二个参数,fun1的第三个参数
来看下输出1个函数的结果
[root@aming-01 aming]# sh fun1.sh 1
The first par is 1
The second par is 
The third par is 
the scritp name is fun1.sh
the nuber of par is 1
[root@aming-01 aming]# 
20.17 shell中的函数(下)

示例2
用于定义加法的函数
在shell里面需要优先定义函数,如果想要调用函数的时候再去定义,就会出错
[root@aming-01 aming]# vim fun2.sh

#!/bin/bash
sum() {
s=$[$1+$2]
echo $s
}

sum 1 10

~


:wq 
下面来执行下,就是第一个参数和第二个参数相加,最后把这个值打印出来
[root@aming-01 aming]# vim fun2.sh
[root@aming-01 aming]# sh fun2.sh
11
[root@aming-01 aming]# sh -x fun2.sh

  • sum 1 10
  • s=11
  • echo 11
    11
    [root@aming-01 aming]# cat fun2.sh
    #!/bin/bash
    sum() {
    s=$[$1+$2]
    echo $s
    }

sum 1 10
[root@aming-01 aming]# 
示例3
显示IP,输入网卡的名字,然后显示网卡的IP
[root@aming-01 aming]# vim fun3.sh

#!/bin/bash
ip()
{
ifconfig |grep -A1 "$1 "|tail -1 |awk '{print $2}'|awk -F ':' '{parint $2}'
}
~


:wq 
先来执行下一段命令
[root@aming-01 aming]# ifconfig |grep -A1 "ens33"
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.202.130 netmask 255.255.255.0 broadcast 192.168.202.255

ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.202.150 netmask 255.255.255.0 broadcast 192.168.202.255
[root@aming-01 aming]# 
为了表示一个进准判断加上-w
[root@aming-01 aming]# ifconfig |grep -w -A1 "ens33"
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.202.130 netmask 255.255.255.0 broadcast 192.168.202.255

ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.202.150 netmask 255.255.255.0 broadcast 192.168.202.255
[root@aming-01 aming]# 
实际上也不好使, ens33:空格然后再是网卡信息 和 ens33:0 后面多了个0 ,那就在帅选后面加个空格试试
[root@aming-01 aming]# ifconfig |grep -A1 "ens33: "
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.202.130 netmask 255.255.255.0 broadcast 192.168.202.255

截取前面的inet的那一段
[root@aming-01 aming]# ifconfig |grep -A1 "ens33: "|grep 'inet'
inet 192.168.202.130 netmask 255.255.255.0 broadcast 192.168.202.255
[root@aming-01 aming]# 
再需要的时ip地址,所以ip地址那一段时属于这一段的 第2段
[root@aming-01 aming]# ifconfig |grep -A1 "ens33: "|grep 'inet' |awk '{print $2}'
192.168.202.130
[root@aming-01 aming]# 
或者
[root@aming-01 aming]# ifconfig |grep -A1 "ens33: "|awk '/inet/ {print $2}'
192.168.202.130
[root@aming-01 aming]# 
最终我们获得了这个网卡的ip
回到脚本
[root@aming-01 aming]# !vi
vim fun3.sh

#!/bin/bash
ip()
{
ifconfig |grep -A1 "$1: "|awk '/inet/ {print $2}'
}

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

~




:wq 
来测试下,就可以了
[root@aming-01 aming]# vim fun3.sh
[root@aming-01 aming]# sh -x fun3.sh

  • read -p 'Please input the eth name: ' eth
    Please input the eth name: ens33
  • ip ens33
  • ifconfig
  • grep -A1 'ens33: '
  • awk '/inet/ {print $2}'
    192.168.202.130
    [root@aming-01 aming]# sh -x fun3.sh
  • read -p 'Please input the eth name: ' eth
    Please input the eth name: ens37
  • ip ens37
  • grep -A1 'ens37: '
  • awk '/inet/ {print $2}'
  • ifconfig
    192.168.142.147
    [root@aming-01 aming]# 
    20.18 shell中的数组

定义数组 a=(1 2 3 4 5);
打印数组的值
echo ${a[]} 等同于 ${a[@]} 显示整个数组 //理解为 打印 $a 里面的所有东西, @、都表示所有内容
定义一个数组
[root@aming-01 aming]# b=(1 2 3)
数组的值怎么去打印?echo ${b[@]} @可以改为
[root@aming-01 aming]# echo ${b[@]}
1 2 3
[root@aming-01 aming]# echo ${b[
]}
1 2 3
[root@aming-01 aming]# 
查看其中某一个元素的值,计算机里面很多都是从0开始的
echo ${b[1]} 读取第二个元素,数组从0开始
[root@aming-01 aming]# echo ${b[1]}
2
[root@aming-01 aming]# echo ${b[2]}
3
[root@aming-01 aming]# echo ${b[0]}
1
[root@aming-01 aming]# 
还可以获取数组的个数 在之前的基础上加了一个#
[root@aming-01 aming]# echo ${#b[@]}
3
[root@aming-01 aming]# 
数组赋值,元素的赋值,更改覆盖
比如定义第四个数组的值
[root@aming-01 aming]# b[3]=a
[root@aming-01 aming]# echo ${b[@]}
1 2 3 a
[root@aming-01 aming]# b[3]=aaa
[root@aming-01 aming]# echo ${b[@]}
1 2 3 aaa
[root@aming-01 aming]# 
数组的删除,可以删除一个值,还可以删除整个数组的值
[root@aming-01 aming]# unset b[3]
[root@aming-01 aming]# echo ${b[@]}
1 2 3
[root@aming-01 aming]# unset b
[root@aming-01 aming]# echo ${b[@]}

[root@aming-01 aming]# 
数组的分片
先把a定义一个seq1到10
[root@aming-01 aming]# a=(seq 1 10)
[root@aming-01 aming]# echo ${a[@]}
1 2 3 4 5 6 7 8 9 10
[root@aming-01 aming]# 
比如我只想 截取4-7这段
实际上它是从第三个元素开始,往后面截4个,最开始是0

4 5 6 7
[root@aming-01 aming]# 
现在反着截,从倒数第三个开始,截取2个
0-3 表示 倒数第3个,

8 9
[root@aming-01 aming]# 
数组的替换
比如把8换成6 echo ${a[@]/8/6}
[root@aming-01 aming]# echo ${a[@]/8/6}
1 2 3 4 5 6 7 6 9 10
[root@aming-01 aming]# 
也可以定义a的数组
[root@aming-01 aming]# a=(${a[@]/8/6})
[root@aming-01 aming]# echo ${a[@]}
1 2 3 4 5 6 7 6 9 10
[root@aming-01 aming]# 
20.19 告警系统需求分析

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