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

网友投稿 459 2022-09-21

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

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

20.16 shell中的函数(上)

函数就是把一段代码整理到了一个小单元中,并给这个小单元起

一个名字,当用到这段代码时直接调用这个小单元的名字即可。

1.

[root@hao-01 ~]# 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 number of par is $#"

}

inp $1 $2 $3

2. 执行fun1.sh脚本,后面跟函数:

[root@hao-01 ~]# sh fun1.sh 1

20.17 shell中的函数(下)

1. 加法函数:

[root@hao-01 ~]# vi fun2.sh

添加内容:

#!/bin/bash

sum() {

s=$[$1+$2]

echo $s

}

sum 1 10

2. 执行fun2.sh脚本:

[root@hao-01 ~]# sh -x fun2.sh

1. 输入网卡名字,显示网卡ip:

[root@hao-01 ~]# vi fun3.sh

添加内容:

#!/bin/bash

ip()

{

ifconfig |grep -A1 "$1: "|awk '/inet/ {print $2}'

}

read -p "please input the eth name: " ech

ip $eth

2. 执行fun3.sh脚本:

[root@hao-01 ~]# sh fun3.sh

please input the eth name: ens33

20.18 shell中的数组

1. 定义数组:

[root@hao-01 ~]# a=(1 2 3 4 5)

2. 查看a数组的元素:

[root@hao-01 ~]# echo ${a[*]}

3. 查看数组某个元素的值(数组从0开始值为1):

[root@hao-01 ~]# echo ${a[1]}

4. 获取数组的元素 个数:

[root@hao-01 ~]# echo ${#a[*]}

5. 如果下标不存在则会自动添加一个元素:

[root@hao-01 ~]# a[5]=b

[root@hao-01 ~]# echo ${a[*]}

数组元素赋值(更改替换):

[root@hao-01 ~]# a[5]=bbb

[root@hao-01 ~]# echo ${a[*]}

6. 删除数组元素:

7. 删除(清空)数组值:

[root@hao-01 ~]# unset a

[root@hao-01 ~]# echo ${a[*]}

8. 设定数组:

[root@hao-01 ~]# a=(`seq 1 10`)

[root@hao-01 ~]# echo ${a[*]}

9. 从第1个元素开始,截取出5个数值:

[root@hao-01 ~]# echo ${a[*]:0:5}

从第2个元素开始,截取出5个数值:

[root@hao-01 ~]# echo ${a[*]:1:5}

10. 从倒数第3个元素开始,截取出2个数值:

[root@hao-01 ~]# echo ${a[*]:0-3:2}

11. 截取替换,8元素打印成cc66:

[root@hao-01 ~]# echo ${a[@]/8/cc66}

12. 替换元素值,8元素替换成cc66:

[root@hao-01 ~]# a=(${a[*]/8/cc66})

[root@hao-01 ~]# echo ${a[*]}

替换元素值,cc66元素替换成888:

[root@hao-01 ~]# a=(${a[*]/cc66/888})

[root@hao-01 ~]# echo ${a[*]}

20.19 告警系统需求分析

1. 需求:使用shell定制各种个性化告警工具,但需要统一化管理、规范化管理。

2. 思路:指定一个脚本包,包含主程序、子程序、配置文件、邮件引擎、输出日志等。

3. 主程序:作为整个脚本的入口,是整个系统的命脉。

4. 配置文件:是一个控制中心,用它来开关各个子程序,指定各个相关联的日志文件。

5. 子程序:这个才是真正的监控脚本,用来监控各个指标。

6. 邮件引擎:是由一个python程序来实现,它可以定义发邮件的服务器、发邮件人以及发件人密码

7. 输出日志:整个监控系统要有日志输出。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Mac safari 卸载插件、扩展
下一篇:Python将Sqlite3查询结果保存为字典形式(python sqlite3 数据类型)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~