主頁(yè) > 知識(shí)庫(kù) > Linux Shell 數(shù)組的創(chuàng)建及使用技巧

Linux Shell 數(shù)組的創(chuàng)建及使用技巧

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

linux shell在編程方面比windows 批處理強(qiáng)大太多,無(wú)論是在循環(huán)、運(yùn)算。已經(jīng)數(shù)據(jù)類型方面都是不能比較的。 下面是個(gè)人在使用時(shí)候,對(duì)它在數(shù)組方面一些操作進(jìn)行的總結(jié)。

1.數(shù)組定義

[chengmo@centos5 ~]$ a=(1 2 3 4 5)
[chengmo@centos5 ~]$ echo $a

一對(duì)括號(hào)表示是數(shù)組,數(shù)組元素用“空格”符號(hào)分割開(kāi)。

2.數(shù)組讀取與賦值

得到長(zhǎng)度:

[chengmo@centos5 ~]$ echo ${#a[@]}
5


用${#數(shù)組名[@或*]} 可以得到數(shù)組長(zhǎng)度

讀?。?br />

[chengmo@centos5 ~]$ echo ${a[2]} 
3

[chengmo@centos5 ~]$ echo ${a[*]} 
1 2 3 4 5  

用${數(shù)組名[下標(biāo)]} 下標(biāo)是從0開(kāi)始  下標(biāo)是:*或者@ 得到整個(gè)數(shù)組內(nèi)容

賦值:

[chengmo@centos5 ~]$ a[1]=100

[chengmo@centos5 ~]$ echo ${a[*]} 
1 100 3 4 5

[chengmo@centos5 ~]$ a[5]=100   
[chengmo@centos5 ~]$ echo ${a[*]}

1 100 3 4 5 100

直接通過(guò) 數(shù)組名[下標(biāo)] 就可以對(duì)其進(jìn)行引用賦值,如果下標(biāo)不存在,自動(dòng)添加新一個(gè)數(shù)組元素

刪除:

[chengmo@centos5 ~]$ a=(1 2 3 4 5)
[chengmo@centos5 ~]$ unset a
[chengmo@centos5 ~]$ echo ${a[*]}

[chengmo@centos5 ~]$ a=(1 2 3 4 5)
[chengmo@centos5 ~]$ unset a[1]  
[chengmo@centos5 ~]$ echo ${a[*]} 
1 3 4 5
[chengmo@centos5 ~]$ echo ${#a[*]}
4

直接通過(guò):unset 數(shù)組[下標(biāo)] 可以清除相應(yīng)的元素,不帶下標(biāo),清除整個(gè)數(shù)據(jù)。

3.特殊使用

分片:

[chengmo@centos5 ~]$ a=(1 2 3 4 5)
[chengmo@centos5 ~]$ echo ${a[@]:0:3}
1 2 3
[chengmo@centos5 ~]$ echo ${a[@]:1:4}
2 3 4 5

[chengmo@centos5 ~]$ c=(${a[@]:1:4})
[chengmo@centos5 ~]$ echo ${#c[@]}
4
[chengmo@centos5 ~]$ echo ${c[*]} 
2 3 4 5


直接通過(guò) ${數(shù)組名[@或*]:起始位置:長(zhǎng)度} 切片原先數(shù)組,返回是字符串,中間用“空格”分開(kāi),因此如果加上”()”,將得到切片數(shù)組,上面例子:c 就是一個(gè)新數(shù)據(jù)。

替換:

[chengmo@centos5 ~]$ a=(1 2 3 4 5)  
[chengmo@centos5 ~]$ echo ${a[@]/3/100}
1 2 100 4 5
[chengmo@centos5 ~]$ echo ${a[@]}
1 2 3 4 5
[chengmo@centos5 ~]$ a=(${a[@]/3/100}) 
[chengmo@centos5 ~]$ echo ${a[@]}   
1 2 100 4 5

調(diào)用方法是:${數(shù)組名[@或*]/查找字符/替換字符} 該操作不會(huì)改變?cè)葦?shù)組內(nèi)容,如果需要修改,可以看上面例子,重新定義數(shù)據(jù)。

從上面講到的,大家可以發(fā)現(xiàn)linux shell 的數(shù)組已經(jīng)很強(qiáng)大了,常見(jiàn)的操作已經(jīng)綽綽有余了。

您可能感興趣的文章:
  • 通過(guò)實(shí)例深入理解linux shell數(shù)組
  • linux shell數(shù)組深入學(xué)習(xí)理解
  • Linux Shell腳本系列教程(六):數(shù)組和關(guān)聯(lián)數(shù)組
  • Linux Shell 數(shù)組建立及使用技巧

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

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

    • 400-1100-266