shell中的函数、shell中的数组、 告警系统需求分析
為什么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的
?
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中的数组、 告警系统需求分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows Phone笔记索引(总)
- 下一篇: 【Unity Shader】三、漫反射D