主頁 > 知識庫 > Shell中創(chuàng)建序列和數(shù)組(list、array)的方法

Shell中創(chuàng)建序列和數(shù)組(list、array)的方法

熱門標(biāo)簽:呼叫中心市場需求 服務(wù)外包 地方門戶網(wǎng)站 Linux服務(wù)器 網(wǎng)站排名優(yōu)化 百度競價排名 AI電銷 鐵路電話系統(tǒng)

關(guān)于linux數(shù)組定義,以及生成方法,請看:linux shell 動態(tài)生成 數(shù)組系列 seq使用技巧 。這里我主要說的是高效生成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得到是字符串,不同之處以:空格分隔開。在linux里面,可以把它看作是list. 可以通過for…in  循環(huán)讀取。

[chengmo@centos5 shell]$ for i in $aNumList;do echo $i;done;
1
2
3
4……

如果需要生成array只需要將$(seq 100) 再加個”()”即可。

[chengmo@centos5 ~]$ aNumList=($(seq 100));
[chengmo@centos5 ~]$ echo $aNumList
1
[chengmo@centos5 ~]$ echo ${#aNumList[@]}
100

長度是100的數(shù)組。

 

二、通過內(nèi)部{begin..end}生成

這種方法生成seq非常方便。通過內(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)用時候可以考慮通過內(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)的實例詳解
  • shell 使用數(shù)組作為函數(shù)參數(shù)的方法(詳解)
  • 淺談shell數(shù)組的定義及循環(huán)
  • Shell動態(tài)生成數(shù)組的多種方法
  • Shell中數(shù)組以及其相關(guān)操作的詳細(xì)實例

標(biāo)簽:仙桃 湘潭 崇左 黃山 湖南 銅川 衡水 蘭州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Shell中創(chuàng)建序列和數(shù)組(list、array)的方法》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266