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

简介:

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:日志


本文转自 jiekegz  51CTO博客,原文链接:http://blog.51cto.com/jacksoner/2045919


相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
6月前
|
运维 Shell C语言
运维(14)- shell函数
运维(14)- shell函数
26 0
|
6月前
|
运维 Shell
运维(13)- shell输入输出
运维(13)- shell输入输出
31 0
|
6月前
|
分布式计算 Hadoop Java
17 案例:开发shell采集脚本
17 案例:开发shell采集脚本
46 0
|
13天前
|
运维 Shell Python
第五章 Shell函数与数组
第五章 Shell函数与数组
|
4月前
|
Shell Linux
Shell编程自动化之特殊Shell扩展变量
本文主要介绍了Shell编程自动化之特殊Shell扩展变量,并结合实例测试。
104 0
|
6月前
|
运维 Shell
运维(12)- shell位置参数和特殊变量
运维(12)- shell位置参数和特殊变量
37 0
|
6月前
|
运维 Shell C语言
运维(11)- shell循环
运维(11)- shell循环
21 0
|
弹性计算 Shell Linux
3天玩转shell--11.实战编写服务器资源告警脚本
本文将通过shell代码示例,简单通俗的讲解shell。通过执行代码和运行结果反向掌握shell编程方法。准备一台低配的阿里云ECS Linux环境,跟着教程走起,本文比较适合shell小白。
113 0
|
Shell
【shell】函数、数组、免交互
文章目录 前言 一、定义 二、函数
72 0
【shell】函数、数组、免交互

热门文章

最新文章