20.16/20.17 shell中的函数
shell中的函数
说明:函数就是子shell, 是一个代码段,定义完函数就可以引用它.
格式:function f_name() {
command
}
注释: function后面跟的是函数的名字,函数名字后面有中括号和花括号,花括号是具体的命令.
演示1
演示1
[root@quandong test]# cat function.sh
#! /bin/bash
function inp(){
echo $1 $2 $3 $0 $#
}
inp 1 a 2
#执行结果
[root@quandong test]# sh function.sh
1 a 2 function.sh 3
注释:$0 --> 表示脚本本身
$# --> 表示参数个数
演示1.1
说明:这脚本和上面那个是一样,只是这样会比较清晰.
[root@quandong test]# cat function1.sh
#! /bin/bash
function inp(){
echo "The first parameter is $1"
echo "The second parameter is $2"
echo "The third parameter is $3"
echo "The script name is $0"
echo "The number of parameter is $#"
}
inp a b 1 2 Hy
#执行结果
[root@quandong test]# sh function1.sh
The first parameter is a
The second parameter is b
The third parameter is 1
The script name is function1.sh
The number of parameter is 5
演示1.2
支持把参数定义外面
#! /bin/bash
function inp(){
echo "The first parameter is $1"
echo "The second parameter is $2"
echo "The third parameter is $3"
echo "The script name is $0"
echo "The number of parameter is $#"
}
inp $1 $2 //函数定义到外面
#执行结果
[root@quandong test]# sh function1.sh 2 //函数定义到外面,执行时输入都多少个参数,就显示多少个
The first parameter is 2
The second parameter is
The third parameter is
The script name is function1.sh
The number of parameter is 1 //只输入了一个参数,所以就显示1个参数
演示2
说明:此脚本的目的是获取两个参数相加的值
[root@quandong test]# vim function2.sh
#! /bin/bash
sum() {
s=$[$1+$2] //函数的第一个参数和第二个参数相加
echo $s //输出相加的结果
}
sum 1 10 //定义参数1 10
#执行结果
[root@quandong test]# sh function2.sh
11
演示3
说明:此脚本的目的是为了获取IP的
[root@quandong test]# vim function3.sh
#! /bin/bash
ip()
{
ifconfig |grep -A1 "$1"|grep 'inet'|awk '{print $2}'
}
read -q "Please input the eth name: " eth
ip $eth
#命令行的演示
1. ifconfig |grep -A1 "lo:" // 获取lo网卡第1行以及下面一行
[root@quandong ~]# ifconfig |grep -A1 "lo:"
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
2. grep 'inet' // 搜索 inet ,匹配到inet这行
[root@quandong ~]# ifconfig |grep -A1 "lo"|grep 'inet'
inet 127.0.0.1 netmask 255.0.0.0
3. |awk '{print $2}' // 匹配到inet这行,再打印第二段
[root@quandong ~]# ifconfig |grep -A1 "lo"|grep 'inet'|awk '{print $2}'
127.0.0.1
# 执行结果
[root@quandong test]# sh function3.sh
Please input the eth name: eth0
172.31.41.7
[root@quandong test]# sh function3.sh
Please input the eth name: lo
127.0.0.1
20.18 shell中的数组
数组的介绍
所谓属组就是一串字符串和一串数字,形成一个变量,把这个变量叫做属组,针对数组再做一些操作,比如说可以取属组的某一个值(数组的第一个,第二个),也可以针对属组进行分片,取数组中某几个数值,某几个元素. 数组很有用,但在使用的场景并不多,不过也需要看需求.
定义数组的格式:a=(1 2 3 4 5 ); echo ${a[*]}
a=( ) 这括号里不一定是一串数字,也可以是字母
定义数组
#命令行里定义数组
[root@quandong test]# a=(a b c)
#打印
[root@quandong test]# echo ${a[*]}
a b c
[root@quandong test]# echo ${a[@]}
a b c
说明:* 和 @ 符号得出来的结果是一样的
#查看某一个元素的值
[root@quandong test]# echo ${a[1]}
b
[root@quandong test]# echo ${a[2]}
c
[root@quandong test]# echo ${a[0]}
a
说明:方括号里面的数字表示是下标,意思元素是第几个,第1个的话是即第2个元素,第0就是第1个,它是从零开始计算的
#查看元素的个数
[root@quandong test]# echo ${#a[*]}
3
数组的赋值
[root@quandong test]# a[3]=w
[root@quandong test]# echo ${#a[*]}
4
[root@quandong test]# echo ${a[*]}
a b c w
数组的删除
#删除数组的某个元素
[root@quandong test]# unset a[3]
[root@quandong test]# echo ${a[*]}
a b c
#删除整个数组
[root@quandong test]# unset a
[root@quandong test]# echo ${a[*]}
数组分片
说明:在特殊的情况或者是复杂的需求,有可能会用到数组分片
截取数组中某几个元素
[root@quandong test]# b=(`seq 1 10`)
[root@quandong test]# echo ${b[*]}
1 2 3 4 5 6 7 8 9 10
[root@quandong test]# echo ${b[@]:3:4}
4 5 6 7
注释: :3 表示从第几个元素; :4 表示取几个元素
#反着来截取
[root@quandong test]# echo ${b[@]:0-5:2}
6 7
#数组替换
[root@quandong test]# echo ${b[@]/9/12}
1 2 3 4 5 6 7 8 12 10
说明:把9替换成12
#可以把整个数组赋值
[root@quandong test]# b=(${b[*]/9/12})
[root@quandong test]# echo ${b[*]}
1 2 3 4 5 6 7 8 12 10
20.19 告警系统需求分析
在Linux运维工作中,大部分工作都是围绕监控,虽然可以使用Zabbix监控,但是有时候Zabbix也不能完全满足所有的需求,比如有时候需要做一个比较冷门的一些监控,那Zabbix需要去写自定义脚本、传输数据等. 例如有时候服务器之间通信有点问题,没有办法从客服端到服务端通信,那没有办法把数据上报到服务端去,怎么做好呢? 可以使用shell脚本监控一下,这样的话非常自由. 实际上告警系统是一个分布式的,也就是说需要在每一台机器上放shell脚本,每一台机器都可以独立监控,不需要依赖其他的机器. 前面使用while 循环,写了一个监控系统负载的脚本,接下来写的脚本都是类似于while ,for, 或是crontab 执行任务,每分钟监控一次,可以让我们清楚的知道服务器是否正常.
很关键的地方是需要弄一个邮件系统,什么时候需要告警,总不能一分钟监测一次, 每一分钟监测到有问题都发邮件告警的话,那样很繁琐,所以要做一个告警收敛
需求:使用shell定制各种个性化告警工具,但需要统一化管理、规范化管理.
思路:指定一个脚本包,包含主程序、子程序、配置文件、邮件引擎、输出日志等
主程序:作为整个脚本的入口,是整个系统的命脉
配置文件:是一个控制中心,用它来开关各个子程序,指定各个相关联的日志文件
子程序:这个才是真正的监控脚本,用来监控各个指标
邮件引擎:是由一个python程序来实现,它可以定义邮件的服务器、发件人以及发件人密码
输出日志:整个监控系统要有日志输出