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

前端之家收集整理的这篇文章主要介绍了shell中的函数、shell的数组、告警系统需求分析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

shell中的函数

函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这个小单元的名字即可。

  1. 格式: function f_name() {
  2. command
  3. }
  4. 函数必须要放在最前面
  • 脚本示例1(用来定义函数打印参数)
  1. [root@garytao-01 aming]# vim fun1.sh
  2. [root@garytao-01 aming]# cat fun1.sh
  3. #!/bin/bash
  4. function inp(){
  5. echo $1 $2 $3 $0 $#
  6. }
  7.  
  8. inp 1 a 2
  9. [root@garytao-01 aming]# sh fun1.sh
  10. 1 a 2 fun1.sh 3
  11. [root@garytao-01 aming]# sh -x fun1.sh
  12. + inp 1 a 2
  13. + echo 1 a 2 fun1.sh 3
  14. 1 a 2 fun1.sh 3
  15.  
  16. [root@garytao-01 aming]# vi fun1.sh
  17. [root@garytao-01 aming]# cat fun1.sh
  18. #!/bin/bash
  19. function inp(){
  20. echo "The first par is $1"
  21. echo "The second par is $2"
  22. echo "The third par is $3"
  23. echo "the scritp name is $0"
  24. echo "the number of par is $#"
  25. }
  26.  
  27. inp b a 2 3 adf
  28. [root@garytao-01 aming]# sh fun1.sh
  29. The first par is b
  30. The second par is a
  31. The third par is 2
  32. the scritp name is fun1.sh
  33. the number of par is 5
  34. [root@garytao-01 aming]# sh -x fun1.sh
  35. + inp b a 2 3 adf
  36. + echo 'The first par is b'
  37. The first par is b
  38. + echo 'The second par is a'
  39. The second par is a
  40. + echo 'The third par is 2'
  41. The third par is 2
  42. + echo 'the scritp name is fun1.sh'
  43. the scritp name is fun1.sh
  44. + echo 'the number of par is 5'
  45. the number of par is 5
  46. [root@garytao-01 aming]#
  • 脚本示例2(用来定义一个加法的函数
  1. [root@garytao-01 aming]# vim fun2.sh
  2. [root@garytao-01 aming]# cat fun2.sh
  3. #!/bin/bash
  4. sum() {
  5. s=$[$1+$2]
  6. echo $s
  7. }
  8.  
  9. sum 1 2
  10. [root@garytao-01 aming]# sh fun2.sh
  11. 3
  12. [root@garytao-01 aming]# sh -x fun2.sh
  13. + sum 1 2
  14. + s=3
  15. + echo 3
  16. 3
  17. [root@garytao-01 aming]#
  • 脚本示例3(用来定义显示IP)
  1. [root@garytao-01 aming]# vim fun3.sh
  2. [root@garytao-01 aming]# cat fun3.sh
  3. #!/bin/bash
  4. ip()
  5. {
  6. ifconfig |grep -A1 "$1: "|awk '/inet/ {print $2}'
  7. }
  8.  
  9. read -p "Please input the eth name: " eth
  10. ip $eth
  11. [root@garytao-01 aming]# sh fun3.sh
  12. Please input the eth name: ens33
  13. 172.16.111.100
  14. [root@garytao-01 aming]# sh -x fun3.sh
  15. + read -p 'Please input the eth name: ' eth
  16. Please input the eth name: ens33
  17. + ip ens33
  18. + grep -A1 'ens33: '
  19. + awk '/inet/ {print $2}'
  20. + ifconfig
  21. 172.16.111.100

shell中的数组

1.shell中的数组1
  • 定义数组 a=(1 2 3 4 5); echo ${a[@]}
  • echo ${#a[@]} 获取数组的元素个数
  • echo ${a[2]} 读取第三个元素,数组从0开始
  • echo ${a[*]} 等同于 ${a[@]} 显示整个数组
    数组赋值
  • a[1]=100; echo ${a[@]}
  • a[5]=2; echo ${a[@]} 如果下标不存在则会自动添加一个元素
    数组的删除
  • unset a; unset a[1]
  1. [root@garytao-01 aming]# b=(1 2 3)
  2. [root@garytao-01 aming]# echo ${b[@]}
  3. 1 2 3
  4. [root@garytao-01 aming]# echo ${b[*]}
  5. 1 2 3
  6. [root@garytao-01 aming]# echo ${b[1]}
  7. 2
  8. [root@garytao-01 aming]# echo ${b[2]}
  9. 3
  10. [root@garytao-01 aming]# echo ${b[0]}
  11. 1
  12. [root@garytao-01 aming]# echo ${#b[@]}
  13. 3
  14. [root@garytao-01 aming]# b[3]=a
  15. [root@garytao-01 aming]# echo ${b[*]}
  16. 1 2 3 a
  17. [root@garytao-01 aming]# b[3]=aaa
  18. [root@garytao-01 aming]# echo ${b[*]}
  19. 1 2 3 aaa
  20. [root@garytao-01 aming]# unset b[3] //删除数组
  21. [root@garytao-01 aming]# echo ${b[*]}
  22. 1 2 3
  23. [root@garytao-01 aming]# unset b
  24. [root@garytao-01 aming]# echo ${b[*]}
  25.  
  26. [root@garytao-01 aming]#
2.shell中的数组2
  • 数组分片
  • a=(seq 1 5)
  • echo ${a[@]:0:3} 从第一个元素开始,截取3个
  • echo ${a[@]:1:4} 从第二个元素开始,截取4个
  • echo ${a[@]:0-3:2} 从倒数第3个元素开始,截取2个
    数组替换
  • echo ${a[@]/3/100}
  • a=(${a[@]/3/100})
  1. [root@garytao-01 aming]# a=(`seq 1 10`)
  2. [root@garytao-01 aming]# echo ${a[*]}
  3. 1 2 3 4 5 6 7 8 9 10
  4. [root@garytao-01 aming]# echo ${a[@]:3:4}
  5. 4 5 6 7
  6. [root@garytao-01 aming]# echo ${a[@]:0-3:2}
  7. 8 9
  8. [root@garytao-01 aming]# echo ${a[@]/8/6}
  9. 1 2 3 4 5 6 7 6 9 10
  10. [root@garytao-01 aming]# a=(${a[@]/8/6})
  11. [root@garytao-01 aming]# echo ${a[@]}
  12. 1 2 3 4 5 6 7 6 9 10
  13. [root@garytao-01 aming]#

告警系统需求分析

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

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

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

猜你在找的Bash相关文章