hujh20 发表于 2018-8-26 10:27:35

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 casdsdg
  运行结果:
  The first parameter is a
  The second parameter is b
  The third parameter isc
  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
  运行结果:
  # sh f1.sh a b c d
  The first parameter is a
  The second parameter is b
  The third parameter isc
  The number of parameter is 3
  The script's name is f1.sh
  3、sum() {
  s=$[$1+$2]
  echo "$s"
  }
  sum $1 $2
  运行结果:
  # 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
  运行结果:
  # sh ip.sh
  please network name: ens37
  input ip:192.168.136.128
数组
  所谓数组,就是相同数据类型的元素按一定顺序排列的集合,就是把有限个类型相同的变量用一个名字命名,在Shell中,用括号来表示数组,数组元素用“空格”符号分割开。
  1、定义数组
  a=(1,2,3,4,a)
  2、查看
  # echo ${a
[*]}
  1,2,3,4,a
  数组的分片
  # a=(seq 1 10)
  # echo ${a
[*]}
  1 2 3 4 5 6 7 8 9 10
  3、从第一个元素开始截取第三个
  1 2 3
  4、倒数第三个元素开始截取3个
  8 9 10
  5、更改元素
  # a=90
  You have new mail in /var/spool/mail/root
  # echo ${a
[*]}
  1 90 3 4 5 6 7 8 9 10
  6、新增元素
  # a=11
  # echo ${a
[*]}
  1 90 3 4 5 6 7 8 9 10 11
告警系统分析
  需求:使用shell定制各种个性化告警工具,但是需要统一化管理、规范化管理。
  思路:指定一个脚本包,包含主程序、子程序、配置文件、邮件引擎、输出日志等等。
  主程序:作为整个程序的入口;
  配置文件:是一个控制中心,它用来开关各个子程序,指定各个相关联的日志文件;
  子程序:这才是真正的监控脚本,用来监控各个指标;
  邮件引擎:是由一个Python程序来实现,它可以定义发邮件的服务器、发邮件人以及发邮件密码;
  输出日志:整个监控系统要有日志输出。
  要求:
  机器的角色多种多样,但是所有的机器上要部署同样的监控系统,也就是说所有的机器不管什么角色,整个程序框架是一样的,不同的地方在于根据不同的角色定制不同的配置文件。
  程序架构:

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

页: [1]
查看完整版本: shell中的函数、数组、告警系统分析