一、shell函数:就是把一段代码整理到一个小单元中,并给这个小单元起一个名字,当用到这段代码的时候直接调用这个小单元的名字便可:python
#一段代码的扩展集合:是一个子shell,定义完后能够引用它:shell
格式:function后是函数的名字,而且function这个单词是能够省略掉的,花括号{}里面为具体的命令:数组
function f_name() { command }
1:案例1: 打印参数:bash
[root@localhost_01 shell]# cat function.sh #!/bin/bash function inp(){ echo $1 $2 $3 $0 $# } inp b a 2 3 adf
[root@localhost_01 shell]# sh -x function.sh + inp b a 2 3 adf + echo b a 2 function.sh 5 b a 2 function.sh 5
注释:如上函数的意义:直接写在脚本里,能够直接调用:服务器
$1:表示第一个参数:架构
$2:表示第二个参数:函数
$3:表示第三个参数:工具
$0:表示变量自己:spa
$#:表示总共有几个参数:code
案例2:用于定义加法的函数,shell中定义的函数,必需要放到上面.在调用这个函数的时候,函数尚未定义,就会报错,想要调用哪个函数,就必须在调用语句以前,先定义这个函数.
[root@localhost_01 shell]# cat funciton1.sh #!/bin/bash sum(){ s=$[$1+$2] echo $s } sum 1 10 [root@localhost_01 shell]# sh -x funciton1.sh + sum 1 10 + s=11 + echo 11 11
案例3:输入网卡的名称,而后会显示IP地址:
[root@localhost_01 shell]# cat fun3.sh #!/bin/bash ip(){ ifconfig |grep -A1 "$1:\ "|tail -1|awk '{print $2}' } read -p "Please input the eth name: " eth ip $eth
注释:grep -A1 "eth0:\ "表示过滤出匹配的行及下面的一行:
[root@localhost_01 shell]# ifconfig |grep -A1 "eth0:\ " eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.149.130 netmask 255.255.255.0 broadcast 192.168.149.255 [root@localhost_01 shell]# ifconfig |grep -A1 "eth0:\ "|tail -1 inet 192.168.149.130 netmask 255.255.255.0 broadcast 192.168.149.255 [root@localhost_01 shell]# ifconfig |grep -A1 "eth0:\ "|tail -1|awk '{print $2}' 192.168.149.130 [root@localhost_01 shell]# ifconfig |grep -A1 "eth0:\ "|awk '/inet/ {print $2}' 192.168.149.130
案例4:判断是否为本机网卡,输入的网卡是否有IP:
[root@localhost_01 shell]# cat fun4.sh #!/bin/bash ip() { a=`ifconfig|grep -A1 "$eth: "|tail -1|awk '{print $2}'` if [ -z "$a" ] then echo "没有这个网卡:" exit fi echo $a } read -p "Please input the eth name: " eth myip=`ip $eth` echo "$eth addrss $myip"
[root@localhost_01 shell]# cat fun8.sh #!/bin/bash myip() { ifconfig | grep -A1 "$1: " | awk '/inet/ {print $2}' } while : do read -p "please input the eth name:" e n=`ifconfig | grep "$e: "` if [ -z $e ] then echo You must input your eth name. elif [ -z "$n" ]; then echo $e is not exist,you need input the right eth name. continue else break fi done echo your eth is $e myip1=`myip $e` if [ -z "$myip1" ] then echo "$e doesn't have IP." else echo "$e ip address $myip1" fi
四、shell中的数组:一串字符串,一串数字造成的变量,咱们把这个变量叫作数组:
定义数组:
定义数组:a=(1 2 3 4 5);echo ${a[@]}
echo ${#a[@]} 获取数组的元素个数
echo ${a[2]} 读取第三个元素,数组从0开始:
echo ${a[*]} 显示整个数组,等同于echo ${a[a]}
[root@localhost_01 shell]# a=(1 2 3) #定义数组 [root@localhost_01 shell]# echo ${a[@]} #打印数组1 1 2 3 [root@localhost_01 shell]# echo ${a[*]} #打印数组2 1 2 3
注释:echo ${a[@]}等同于echo ${a[*]},理解为打印数组的全部内容,@和*都表示全部内容:
[root@localhost_01 shell]# echo ${a[@]} 1 2 3 [root@localhost_01 shell]# echo ${a[2]} 3
注释:表示打印第二个元素,第0个就表示第一个,第一个就表示第二个.....(数组就是从0 开始的)
获取元素的个数: echo ${#a[@]}
[root@localhost_01 shell]# echo ${a[@]} 1 2 3 [root@localhost_01 shell]# echo ${#a[@]} 3
数组赋值:素的赋值 或更改覆盖:
[root@localhost_01 shell]# echo ${a[@]} 1 2 3 [root@localhost_01 shell]# a[3]=aaa #赋值: [root@localhost_01 shell]# echo ${a[@]} 1 2 3 aaa [root@localhost_01 shell]# a[3]=100 #赋予新的值: [root@localhost_01 shell]# echo ${a[*]} 1 2 3 100
数组删除:unset b[3]
[root@localhost_01 shell]# echo ${a[*]} 1 2 3 100 [root@localhost_01 shell]# unset a[3] [root@localhost_01 shell]# echo ${a[@]} 1 2 3 [root@localhost_01 shell]# unset a [root@localhost_01 shell]# echo ${a[@]}
数组分片:能够用seq来定义数字:
[root@localhost_01 shell]# a=(`seq 1 10`) [root@localhost_01 shell]# echo ${a[@]} 1 2 3 4 5 6 7 8 9 10
截取数字4到7这四个数字: 截取数字从倒数3开始的四个数字和两个数字:
[root@localhost_01 shell]# echo ${a[@]:3:4} #截取数字4到7: 4 5 6 7 [root@localhost_01 shell]# echo ${a[@]:0-3:4} #截取数字倒数3开始的四个这数字: 8 9 10 [root@localhost_01 shell]# echo ${a[@]:0-3:2} #截取数字倒数3开始的两个这数字: 8 9
数组替换:数组的替换和sed替换相似:
数组的8替换成6:
[root@localhost_01 shell]# echo ${a[@]} 1 2 3 4 5 6 7 8 9 10 [root@localhost_01 shell]# echo ${a[@]/8/6} 1 2 3 4 5 6 7 6 9 10
直接赋值(须要用括号括起来)
[root@localhost_01 shell]# echo ${a[@]} 1 2 3 4 5 6 7 8 9 10 [root@localhost_01 shell]# a=(${a[@]/8/6}) [root@localhost_01 shell]# echo ${a[@]} 1 2 3 4 5 6 7 6 9 10
告警系统需求分析:
需求:使用shell定制各类个性化工具,但须要统一化管理,规范化管理:
思路:指定一个脚本包,包含主程序,子程序,配置文件,邮件引擎,输出日记等:
主程序:做为整个脚本的入口,是整个系统的命脉:
子程序:真正的监控脚本,用来监控各个指标:
配置日记:是一个控制中心,用它来开关各个子程序,指定各个相关联的日记文件:
邮件引擎:是由一个python程序来实现,定义发邮件的服务器,发邮件人以及发件人的密码:
输出日记:真个监控系统要有日记输出:
程序架构:
bin 下是主程序:
conf 下是配置文件:
shares 是各个监控脚本:
mail 是邮件引擎:
log 是日记: