Shell動(dòng)態(tài)生成數(shù)組的多種方法
如果對(duì)linux shell 數(shù)組不是很熟悉的話,請(qǐng)看上一篇文章:linux shell 數(shù)組建立及使用技巧 ,這篇文章主要講是動(dòng)態(tài)生成數(shù)組系列。方法應(yīng)該很多,我這里主要以一個(gè)求和計(jì)算的題目為例進(jìn)行分析。
題目:請(qǐng)用linux shell 寫一段腳本,實(shí)現(xiàn)從1..1000中所有偶數(shù)的和值。
方法一:
通過(guò)while 循環(huán)得到需要的結(jié)果:
start=1; total=0; while [ $start -le 1000 ];do [[ $(($start%2)) == 0 ]]&&total=$(($total+$start)); start=$(($start+1)); done; echo $total; [chengmo@centos5 ~]$ start=1;total=0;while [ $start -le 1000 ];do [[ $(($start%2)) == 0 ]]&&total=$(($total+$start)); start=$(($start+1));done;echo $total; 250500
以上運(yùn)行結(jié)果是:249500,在linux shell 中,”;”作為命令行分隔符。如果大家對(duì)于$(()) 運(yùn)算符號(hào)不是很理解,可以查看:linux shell 實(shí)現(xiàn) 四則運(yùn)算(整數(shù)及浮點(diǎn)) 簡(jiǎn)單方法 ,如果對(duì)于:[[]] [] 符號(hào),可以參考另外一篇文章linux shell 邏輯運(yùn)算符、邏輯表達(dá)式詳解。
方法二:
通過(guò) for 循環(huán)得到結(jié)果:
start=0; total=0; for i in $(seq $start 2 1000); do total=$(($total+$i)); done; echo $total; [chengmo@centos5 ~]$ start=0;total=0;for i in $(seq $start 2 1000); do total=$(($total+$i));done;echo $total; 250500
上面語(yǔ)句已經(jīng)代碼方面明顯優(yōu)于方法一,而且性能方面表現(xiàn)也很好。下面比較就可以發(fā)現(xiàn):
比較性能:
[chengmo@centos5 ~]$ time (start=0;total=0;for i in $(seq $start 2 1000); do total=$(($total+$i));done;echo $total;) 250500 real 0m0.016s user 0m0.012s sys 0m0.003s [chengmo@centos5 ~]$ time (start=1;total=0;while [ $start -le 1000 ];do [[ $(($start%2)) == 0 ]]&&total=$(($total+$start)); start=$(($start+1));done;echo $total;) 250500 real 0m0.073s user 0m0.069s sys 0m0.004s
方法一耗時(shí) 是方法二的 6倍!
seq 使用:
seq [OPTION]... LAST seq [OPTION]... FIRST LAST seq [OPTION]... FIRST INCREMENT LAST [chengmo@centos5 ~]$ seq 1000 ‘起始默認(rèn)是 1,間隔默認(rèn)也是1 [chengmo@centos5 ~]$seq 2 1000 ‘間隔默認(rèn)是1 [chengmo@centos5 ~]$seq 1 3 10 '從1開始,到10 間隔為3 結(jié)果是:1 4 7 10 說(shuō)明:默認(rèn)間隔是“空格” 如果想換成其它的可以帶參數(shù):-s [chengmo@centos5 ~]$seq -s'#' 1 3 10 1#4#7#10
應(yīng)用技巧:
生成連續(xù)數(shù)組系列:
[chengmo@centos5 ~]$ a=($(seq 1 3 10)) [chengmo@centos5 ~]$ echo ${a[1]} 4 [chengmo@centos5 ~]$ echo ${a[@]} 1 4 7 10
生成連續(xù)相同字符:
[chengmo@centos5 ~]$ seq -s '#' 30 | sed -e 's/[0-9]*//g' #############################
上面例子:通過(guò)加入間隔字符‘#'后,替換掉數(shù)字, 生成連續(xù)相同字符'#',這個(gè)在以后書寫中還是有不少幫助。
相關(guān)文章
完美解決ntp的錯(cuò)誤問(wèn)題no server suitable for synchronization fo
下面小編就為大家?guī)?lái)一篇完美解決ntp的錯(cuò)誤問(wèn)題no server suitable for synchronization fo。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03使用shell腳本每天對(duì)MySQL多個(gè)數(shù)據(jù)庫(kù)自動(dòng)備份的講解
今天小編就為大家分享一篇關(guān)于使用shell腳本每天對(duì)MySQL多個(gè)數(shù)據(jù)庫(kù)自動(dòng)備份的講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03linux awk時(shí)間計(jì)算腳本及awk命令詳解
這篇文章主要介紹了linux awk時(shí)間計(jì)算腳本及awk命令詳解的相關(guān)資料,需要的朋友可以參考下2015-11-11Shell腳本運(yùn)行中的停止方法實(shí)現(xiàn)
Linux系統(tǒng)Shell中提交了一個(gè)腳本,但是需要停止這個(gè)進(jìn)程,如何處理?本文就來(lái)詳細(xì)的介紹一下,感興趣的可以了解一下2021-11-11shell簡(jiǎn)單處理mysql查詢結(jié)果的方法
今天小編就為大家分享一篇shell簡(jiǎn)單處理mysql查詢結(jié)果的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06實(shí)現(xiàn)core文件自動(dòng)生成配置文件的方法
這篇文章主要介紹了實(shí)現(xiàn)core文件自動(dòng)生成配置文件的方法,需要的朋友可以參考下2014-07-07