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

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介:

shell中的函数

1、#!/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

运行结果:
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 f1.sh

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 number of parameter is $#"
echo "The script's name is $0"
}
inp $1 $2 $3

运行结果:
[root@centos7 shell]# sh f1.sh a b c d
The first parameter is a
The second parameter is b
The third parameter is c
The number of parameter is 3
The script's name is f1.sh

3、sum() {
s=$[$1+$2]
echo "$s"
}
sum $1 $2

运行结果:
[root@centos7 shell]# sh f1.sh 2 4

6、#!/bin/sh
ip(){
ifconfig |grep -A1 "$1: " |tail -1|awk '{print $2}'

}

read -p 'please network name: ' n
myip=ip $n
echo "input ip:"$myip

运行结果:
[root@centos7 shell]# sh ip.sh 
please network name: ens37
input ip:192.168.136.128

数组

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

1、定义数组
a=(1,2,3,4,a)
2、查看
[root@centos7 shell]# echo ${a[*]}
1,2,3,4,a

数组的分片
[root@centos7 shell]# a=(seq 1 10)

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

3、从第一个元素开始截取第三个

1 2 3

4、倒数第三个元素开始截取3个

8 9 10

5、更改元素
[root@centos7 shell]# a[1]=90
You have new mail in /var/spool/mail/root
[root@centos7 shell]# echo ${a[*]}
1 90 3 4 5 6 7 8 9 10

6、新增元素
[root@centos7 shell]# a[11]=11
[root@centos7 shell]# echo ${a[*]}
1 90 3 4 5 6 7 8 9 10 11

告警系统分析

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

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

程序架构:
shell中的函数、数组、告警系统分析

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










本文转自方向对了,就不怕路远了!51CTO博客,原文链接:http://blog.51cto.com/jacksoner/2045919 ,如需转载请自行联系原作者



相关实践学习
通过日志服务实现云资源OSS的安全审计
本实验介绍如何通过日志服务实现云资源OSS的安全审计。
相关文章
|
存储 Shell
Shell 数组
【10月更文挑战第16天】
194 3
|
Shell Linux C语言
Shell 函数
10月更文挑战第4天
72 7
|
Shell PHP 索引
Shell 数组
10月更文挑战第2天
92 1
|
Shell KVM 虚拟化
Shell 数组编程
【8月更文挑战第22天】 Shell 数组编程
136 10
|
Shell Linux 程序员
在Linux中, 什么是shell函数?如何使用它们?
在Linux中, 什么是shell函数?如何使用它们?
|
Shell 开发者
Shell 函数深入解析与实践
了解 Shell 函数的基础,包括定义、参数传递及返回值。函数定义有多种语法,如 `function func() {...}` 或 `func() {...}`。参数通过 `$1`, `$2` 等访问,`$@` 代表所有参数。`return` 用于返回退出状态码(0-255),非数值数据需用 `echo`。正确获取函数返回值应立即检查 `$?`,例如:`result=$?`。实践中不断探索和学习!
133 1
|
存储 算法 安全
shell 脚本之 函数与数组
shell 脚本之 函数与数组
|
运维 Shell Python
第五章 Shell函数与数组
第五章 Shell函数与数组
|
存储 Shell 开发者
Shell 数组:灵活操作的秘诀
**Shell 数组简介**:作为基础数据结构,数组在Shell编程中不可或缺。它们存储多个值,下标从0开始。创建如`array=(值1 值2...)`,访问用`${array[index]}`。增删改查及获取长度、拼接数组都有相应语法,例如`unset array[index]`删除元素,`${#array[@]}`获取长度。通过实践这些操作,提升Shell脚本技能。
162 0
|
Shell 应用服务中间件 nginx
shell学习(七) 【shell 函数】
shell学习(七) 【shell 函数】
105 1