Shell中創(chuàng)建序列和數(shù)組(list、array)的方法
關(guān)于linux數(shù)組定義,以及生成方法,請(qǐng)看:linux shell 動(dòng)態(tài)生成 數(shù)組系列 seq使用技巧 。這里我主要說(shuō)的是高效生成list 字符串,還有數(shù)組方法。
一、seq方法生成:
[chengmo@centos5 shell]$ aNumList=$(seq 100); [chengmo@centos5 shell]$ echo $aNumList 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
aNumList得到是字符串,不同之處以:空格分隔開(kāi)。在linux里面,可以把它看作是list. 可以通過(guò)for…in 循環(huán)讀取。
[chengmo@centos5 shell]$ for i in $aNumList;do echo $i;done; 1 2 3 4……
如果需要生成array只需要將$(seq 100) 再加個(gè)”()”即可。
[chengmo@centos5 ~]$ aNumList=($(seq 100)); [chengmo@centos5 ~]$ echo $aNumList 1 [chengmo@centos5 ~]$ echo ${#aNumList[@]} 100
長(zhǎng)度是100的數(shù)組。
二、通過(guò)內(nèi)部{begin..end}生成
這種方法生成seq非常方便。通過(guò)內(nèi)部運(yùn)算符完成。
[chengmo@centos5 ~]$ echo {1..10} 1 2 3 4 5 6 7 8 9 10 [chengmo@centos5 ~]$ for a in {1..10};do echo $a;done; 1 2 3 4 5 6 7 8 9 10
三、性能比較
[chengmo@centos5 ~]$ time echo {1..100} 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 real 0m0.000s user 0m0.001s sys 0m0.000s [chengmo@centos5 ~]$ time echo $(seq 100) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 real 0m0.003s user 0m0.002s sys 0m0.001s
從上面可以看到,{begin..end}速度比seq調(diào)用快了不少了。 以后調(diào)用時(shí)候可以考慮通過(guò)內(nèi)部操作符完成。
- Shell腳本數(shù)組操作小結(jié)
- Shell腳本數(shù)組用法小結(jié)
- linux shell數(shù)組深入學(xué)習(xí)理解
- shell for循環(huán)與數(shù)組應(yīng)用介紹
- linux shell 中數(shù)組的定義和for循環(huán)遍歷的方法
- Linux shell數(shù)組循環(huán)的實(shí)例詳解
- shell 使用數(shù)組作為函數(shù)參數(shù)的方法(詳解)
- 淺談shell數(shù)組的定義及循環(huán)
- Shell動(dòng)態(tài)生成數(shù)組的多種方法
- Shell中數(shù)組以及其相關(guān)操作的詳細(xì)實(shí)例
相關(guān)文章
關(guān)于shell的幾個(gè)不為人知卻十分有用的命令分享
這篇文章主要介紹了關(guān)于shell的幾個(gè)不為人知卻十分有用的命令,需要的朋友可以參考下2016-03-03Shell腳本實(shí)現(xiàn)隨機(jī)數(shù)多種方法介紹(date、random、uuid)
這篇文章主要介紹了Shell腳本實(shí)現(xiàn)隨機(jī)數(shù)多種方法介紹,本文講解了通過(guò)時(shí)間獲得隨機(jī)數(shù)、通過(guò)內(nèi)部系統(tǒng)變量、通過(guò)系統(tǒng)內(nèi)部唯一數(shù)據(jù)生成隨機(jī)數(shù)等方法,需要的朋友可以參考下2014-11-11高級(jí)開(kāi)發(fā)運(yùn)維測(cè)試必須掌握的envsubst命令使用詳解
這篇文章主要為大家介紹了高級(jí)開(kāi)發(fā)運(yùn)維測(cè)試必須掌握的envsubst命令使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04一個(gè)監(jiān)控網(wǎng)卡流量的shell腳本
這篇文章主要為大家分享一個(gè)簡(jiǎn)單的監(jiān)控流量腳本,需要的朋友可以參考下2016-08-08Bash 腳本實(shí)現(xiàn)每次登錄到 Shell 時(shí)可以查看 Linux 系統(tǒng)信息
這篇文章主要介紹了Bash 腳本實(shí)現(xiàn)每次登錄到 Shell 時(shí)可以查看 Linux 系統(tǒng)信息,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2019-12-12Log4j 日志文件Linux/Mac/Windows通用存放位置設(shè)置方法
下面小編就為大家?guī)?lái)一篇Log4j 日志文件Linux/Mac/Windows通用存放位置設(shè)置方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01