shell中的函数(function)、数组、告警系统分析

20.16-20.17 shell中的函数

函数就是把一段代码整理到一个小单元中,并给这个小单元命名,当用到这段代码时直接调用这个小单元的名字即可。

格式

function f_name() {
    commond
}

说明: 单词“function”可以省略,直接写函数的名字;函数必须放在脚本的最前面;调用函数的方法:直接写函数名。  

eg:

eg1:

[root@localhost sbin]# vim func.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 number of parameter is $#"
    echo "The script's name is $0"
}
#定义一个函数
inp a b c  asd  sdg
#引用该函数

[root@localhost sbin]# sh func.sh 
The first parameter is a
The second parameter is b
The third parameter is  c
The number of parameter is 5
The script's name is func.sh

说明: “$#” 表示参数的个数;“$0”表示参数名。

eg2:

#!/bin/bash
function inp(){
    echo "The first parameter is $1"
    echo "The second parameter is $2"
    echo "The third parameter is  $3"
    echo "The number of parameter is $#"
    echo "The script's name is $0"
}
inp $1 $2 $3

[root@localhost sbin]# sh func.sh 1 2
The first parameter is 1
The second parameter is 2
The third parameter is  
The number of parameter is 2
The script's name is func.sh

说明: 此时inp后面的“$1,$2,$3”为函数inp的变量,在执行该脚本时直接输入。

eg3:

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

[root@localhost sbin]# sh func2.sh 
11

eg3:

#!/bin/bash
ip(){
    ifconfig |grep -A1 "$1: " |tail -1 |awk '{print $2}'
}
read -p "Please input the eth name:" e
myip=`ip $e`
echo "$e address is $myip"

[root@localhost sbin]# sh func3.sh 
Please input the eth name:ens37
ens37 address is 192.168.75.129
[root@localhost sbin]# sh func3.sh 
Please input the eth name:ens33
ens33 address is 192.168.8.130

20.18 shell中的数组

所谓数组,就是相同数据类型的元素按一定顺序排列的集合,就是把有限个类型相同的变量用一个名字命名,在Shell中,用括号来表示数组,数组元素用“空格”符号分割开。

数组格式

  • 定义数组:

    • 方法1:a=(1 2 3 4 5)
    • 方法2:a=(seq 1 5)
  • 查看整个数组:
    echo ${a[@]} 或 echo ${a[*]}

  • 查看数组中某个元素的值:
    echo ${a[x]} :x表示数字,从0开始,代表的是数组中的位置。如数组a:

    序号
    01
    12
    23
    34
    45
  • 查看数组中元素的个数
    echo ${#a[@]}

  • 为数组赋值或更改某元素的值:
    a[5]=f :5表示该元素在数组中的位置(序号);如果要更改某元素的值,直接对该元素重新赋值即可!

  • 删除数组中的元素:

    • 删除某个元素 unset a[x] :x表示元素的序号
    • 清空数组:unset b

数组的分片

[root@localhost sbin]# a=(`seq 1 10`)
[root@localhost sbin]# echo ${a[*]}
1 2 3 4 5 6 7 8 9 10
分片
  • 从第一个元素开始,截取3个:
[root@localhost sbin]# echo ${a[@]:0:3}
1 2 3
  • 从第四个元素开始,截取4个:
[root@localhost sbin]# echo ${a[@]:3:4}
4 5 6 7

注意: 数组中元素的序号是从0开始排序的。

  • 从倒数第三个元素开始,截取2个:
[root@localhost sbin]# echo ${a[@]:0-3:2}
8 9

数组的替换

[root@localhost sbin]# echo ${a[@]/3/three}
1 2 three 4 5 6 7 8 9 10

[root@localhost sbin]# echo ${a[*]}
1 2 3 4 5 6 7 8 9 10

注: 该方法不会改变元素中的值,知识改变打印时的值。

[root@localhost sbin]# a=(${a[@]/3/three})
[root@localhost sbin]# echo ${a[*]}
1 2 three 4 5 6 7 8 9 10

注: 该方法会直接更改数组中对应元素的值。

20.19 告警系统需求分析

  • 需求:使用shell定制各种个性化告警工具,但是需要统一化管理、规范化管理。
  • 思路:指定一个脚本包,包含主程序、子程序、配置文件、邮件引擎、输出日志等等。
  • 主程序:作为整个程序的入口;
  • 配置文件:是一个控制中心,它用来开关各个子程序,指定各个相关联的日志文件;
  • 子程序:这才是真正的监控脚本,用来监控各个指标;
  • 邮件引擎:是由一个Python程序来实现,它可以定义发邮件的服务器、发邮件人以及发邮件密码;
  • 输出日志:整个监控系统要有日志输出。

要求:
机器的角色多种多样,但是所有的机器上要部署同样的监控系统,也就是说所有的机器不管什么角色,整个程序框架是一样的,不同的地方在于根据不同的角色定制不同的配置文件。

  • 程序架构:

mark

  • bin:主程序
  • conf:配置文件
  • shares:各个监控脚本
  • mail:邮件引擎
  • log:日志

转载于:https://my.oschina.net/adailinux/blog/1538109

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值