为什么80%的码农都做不了架构师?>>>   

20.16/20.17 shell中的函数

shell中的函数

说明:函数就是子shell, 是一个代码段,定义完函数就可以引用它.

格式:function f_name() {                  
                 command  
         }

注释: function后面跟的是函数的名字,函数名字后面有中括号和花括号,花括号是具体的命令.

演示1

演示1
[root@quandong test]# cat function.sh
#! /bin/bash
function inp(){echo $1 $2 $3 $0 $#
}
inp 1 a 2 #执行结果
[root@quandong test]# sh function.sh
1 a 2 function.sh 3注释:$0 --> 表示脚本本身$# --> 表示参数个数演示1.1
说明:这脚本和上面那个是一样,只是这样会比较清晰.
[root@quandong test]# cat function1.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 script name is $0"echo "The number of parameter is $#"
}
inp a b 1 2 Hy#执行结果
[root@quandong test]# sh function1.sh
The first parameter is a
The second parameter is b
The third parameter is 1
The script name is function1.sh
The number of parameter is 5演示1.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 script name is $0"echo "The number of parameter is $#"
}
inp $1 $2                                      //函数定义到外面#执行结果
[root@quandong test]# sh function1.sh 2       //函数定义到外面,执行时输入都多少个参数,就显示多少个
The first parameter is 2
The second parameter is
The third parameter is
The script name is function1.sh
The number of parameter is 1                 //只输入了一个参数,所以就显示1个参数

演示2

说明:此脚本的目的是获取两个参数相加的值

[root@quandong test]# vim function2.sh
#! /bin/bash
sum() {s=$[$1+$2]          //函数的第一个参数和第二个参数相加echo $s             //输出相加的结果
}sum 1 10               //定义参数1 10 #执行结果
[root@quandong test]# sh function2.sh
11

演示3
说明:此脚本的目的是为了获取IP的

[root@quandong test]# vim function3.sh#! /bin/bash
ip()
{ifconfig |grep -A1 "$1"|grep 'inet'|awk '{print $2}'
}read -q "Please input the eth name: " eth
ip $eth#命令行的演示1. ifconfig |grep -A1 "lo:"      // 获取lo网卡第1行以及下面一行[root@quandong ~]# ifconfig |grep -A1 "lo:"lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536inet 127.0.0.1  netmask 255.0.0.02. grep 'inet'                  // 搜索 inet ,匹配到inet这行[root@quandong ~]# ifconfig |grep -A1 "lo"|grep 'inet' inet 127.0.0.1  netmask 255.0.0.03. |awk '{print $2}'            // 匹配到inet这行,再打印第二段[root@quandong ~]# ifconfig |grep -A1 "lo"|grep 'inet'|awk '{print $2}'127.0.0.1# 执行结果
[root@quandong test]# sh function3.sh
Please input the eth name: eth0
172.31.41.7
[root@quandong test]# sh function3.sh
Please input the eth name: lo
127.0.0.1

20.18 shell中的数组

数组的介绍

所谓属组就是一串字符串和一串数字,形成一个变量,把这个变量叫做属组,针对数组再做一些操作,比如说可以取属组的某一个值(数组的第一个,第二个),也可以针对属组进行分片,取数组中某几个数值,某几个元素. 数组很有用,但在使用的场景并不多,不过也需要看需求.

定义数组的格式:a=(1 2 3 4 5  ); echo ${a[*]}
                           a=( ) 这括号里不一定是一串数字,也可以是字母

定义数组

#命令行里定义数组
[root@quandong test]# a=(a b c)#打印
[root@quandong test]# echo ${a[*]}
a b c[root@quandong test]# echo ${a[@]}
a b c说明:* 和 @ 符号得出来的结果是一样的#查看某一个元素的值
[root@quandong test]# echo ${a[1]}
b
[root@quandong test]# echo ${a[2]}
c
[root@quandong test]# echo ${a[0]}
a说明:方括号里面的数字表示是下标,意思元素是第几个,第1个的话是即第2个元素,第0就是第1个,它是从零开始计算的#查看元素的个数
[root@quandong test]# echo ${#a[*]}
3

数组的赋值

[root@quandong test]# a[3]=w[root@quandong test]# echo ${#a[*]}
4
[root@quandong test]# echo ${a[*]}
a b c w

数组的删除

#删除数组的某个元素
[root@quandong test]# unset a[3]
[root@quandong test]# echo ${a[*]}
a b c#删除整个数组
[root@quandong test]# unset a
[root@quandong test]# echo ${a[*]}

数组分片

说明:在特殊的情况或者是复杂的需求,有可能会用到数组分片

截取数组中某几个元素

[root@quandong test]# b=(`seq 1 10`)[root@quandong test]# echo ${b[*]}
1 2 3 4 5 6 7 8 9 10[root@quandong test]# echo ${b[@]:3:4}
4 5 6 7注释: :3 表示从第几个元素; :4 表示取几个元素#反着来截取
[root@quandong test]# echo ${b[@]:0-5:2}
6 7#数组替换
[root@quandong test]# echo ${b[@]/9/12}
1 2 3 4 5 6 7 8 12 10说明:把9替换成12#可以把整个数组赋值
[root@quandong test]# b=(${b[*]/9/12})
[root@quandong test]# echo ${b[*]}
1 2 3 4 5 6 7 8 12 10

20.19 告警系统需求分析

在Linux运维工作中,大部分工作都是围绕监控,虽然可以使用Zabbix监控,但是有时候Zabbix也不能完全满足所有的需求,比如有时候需要做一个比较冷门的一些监控,那Zabbix需要去写自定义脚本、传输数据等. 例如有时候服务器之间通信有点问题,没有办法从客服端到服务端通信,那没有办法把数据上报到服务端去,怎么做好呢? 可以使用shell脚本监控一下,这样的话非常自由. 实际上告警系统是一个分布式的,也就是说需要在每一台机器上放shell脚本,每一台机器都可以独立监控,不需要依赖其他的机器. 前面使用while 循环,写了一个监控系统负载的脚本,接下来写的脚本都是类似于while ,for, 或是crontab 执行任务,每分钟监控一次,可以让我们清楚的知道服务器是否正常.
     很关键的地方是需要弄一个邮件系统,什么时候需要告警,总不能一分钟监测一次, 每一分钟监测到有问题都发邮件告警的话,那样很繁琐,所以要做一个告警收敛

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

转载于:https://my.oschina.net/AnnaWu/blog/1538040

shell中的函数、shell中的数组、 告警系统需求分析相关推荐

  1. scala 函数中嵌套函数_Scala中的嵌套函数 用法和示例

    scala 函数中嵌套函数 Scala中的嵌套函数 (Nested functions in Scala) A nested function is defined as a function whi ...

  2. shell中的函数shell中的数组告警系统需求分析

    2019独角兽企业重金招聘Python工程师标准>>> 20.16/20.17 shell中的函数 函数的概念 函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用 ...

  3. python中mat函数_Python中flatten( )函数及函数用法详解

    flatten()函数用法 flatten是numpy.ndarray.flatten的一个函数,即返回一个一维数组. flatten只能适用于numpy对象,即array或者mat,普通的list列 ...

  4. scala 函数中嵌套函数_Scala中的VarArgs函数和@varargs批注

    scala 函数中嵌套函数 In this post, we are going to discuss about Functions with Var-Args (Variable Number O ...

  5. python中multiply函数_python中numpy中的multiply、*、matul 的区别

    numpy中的multiply.*.matul 的区别 1.对于矩阵(matrix)而言,multiply是对应元素相乘,而 *  .np.matmul() 函数 与 np.dot()函数 相当于矩阵 ...

  6. matlab中ind2sub函数,Python中的MATLAB ind2sub等价

    据我所知,这些函数在MATLAB中没有直接实现. 结果我看不懂文件.如果您想要sub2ind的功能,那么您需要^{}函数.函数声明说您需要两个输入.第一个输入是2Dnumpy数组,其中每一行是特定维度 ...

  7. python中pop函数_Python中的Pop函数

    python中pop函数 什么是弹出功能? (What is the pop function?) The method pop() removes and returns the last elem ...

  8. oracle中各种函数,oracle中常用函数大全

    1.数值型常用函数 函数 返回值 样例 显示 ceil(n) 大于或等于数值n的最小整数 select ceil(10.6) from dual; 11 floor(n) 小于等于数值n的最大整数 s ...

  9. python中set函数_python中set()函数简介及实例解析

    set函数也是python内置函数的其中一个,属于比较基础的函数.其具体介绍和使用方法,下面进行介绍. set() 函数创建一个无序不重复元素集,可进行关系测试,删除重复数据,还可以计算交集.差集.并 ...

  10. python查看dll中所有函数_Python中的函数

    初识函数 我们的程序的代码块很快变得越来越大,需要一些方法分成小代码块,便于组织,便于编写和阅读 程序分解成较小代码块有三种方法: 1.函数(function)实现具体功能的代码块,向代码的积木,可以 ...

最新文章

  1. .NET Compact Framework下SQL CE的使用
  2. vue中v-model原理
  3. sqlplus -prelim,sqplus区别
  4. 队列实现栈 | 栈实现队列
  5. P6378 [PA2010] Riddle(2-sat/前后缀优化建图)
  6. VMware 8.0不能手动安装Linux5.5(命令行)问题解决方法
  7. centos配置mysql
  8. Android 系统(226)---Android 阿拉伯语适配
  9. springboot和springcloud的基本概念理解
  10. 小米 11 不送充电器;苹果已修复 iCloud 登录激活问题;Ruby 3.0.0 发布|极客头条...
  11. java 调用soapui_利用soapui和jdk API访问webservice
  12. 64位linux下安装libpng出错,安装libpng-1.6.10时make出现错误,请帮忙
  13. 30天敏捷结果(10):发挥你的优势
  14. 用Java实现N*N的标准数独及对角线数独解题
  15. 苹果笔记本怎么找文件夹_苹果Mac电脑快速查找文件的两种方法
  16. (2022)安卓和苹果应用注册上架概述
  17. 叔叔不约---匿名聊天网 聊天图片爬虫抓取
  18. android壁纸制作,安卓动态壁纸制做壁纸的方法教程
  19. win10系统的计算机C盘在哪,win10系统只有一个C盘怎么解决
  20. oracle查运行sql语句,查询Oracle正在执行的SQL语句

热门文章

  1. GDCM:提取DICOM文件的加密内容到der文件的测试程序
  2. allocator_difference_type的实例
  3. boost::container模块实现范围分配器适配器
  4. ITK:更改图像原点间距或方向
  5. ITK:计算代码点之间的时间
  6. VTK:几何对象之IsoparametricCellsDemo
  7. OpenCV定制和调试检测系统
  8. c++判断二个数是否为相反的符号算法实现(附完整源码)
  9. OpenGL linesmooth线平滑的实例
  10. OpenGL抗锯齿实例