linux shell在編程方面比windows 批處理強大太多,無論是在循環(huán)、運算。已經(jīng)數(shù)據(jù)類型方面都是不能比較的。 下面是個人在使用時候,對它在數(shù)組方面一些操作進行的總結。
1.數(shù)組定義
[chengmo@centos5 ~]$ a=(1 2 3 4 5)
[chengmo@centos5 ~]$ echo $a
一對括號表示是數(shù)組,數(shù)組元素用“空格”符號分割開。
2.數(shù)組讀取與賦值
得到長度:
[chengmo@centos5 ~]$ echo ${
#a[@]}
用${#數(shù)組名[@或*]} 可以得到數(shù)組長度
讀?。?/p>
[chengmo@centos5 ~]$ echo ${a[2]}
3
[chengmo@centos5 ~]$ echo ${a[*]}
1 2 3 4 5
用${數(shù)組名[下標]} 下標是從0開始 下標是:*或者@ 得到整個數(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
直接通過 數(shù)組名[下標] 就可以對其進行引用賦值,如果下標不存在,自動添加新一個數(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
直接通過:unset 數(shù)組[下標] 可以清除相應的元素,不帶下標,清除整個數(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
直接通過 ${數(shù)組名[@或*]:起始位置:長度} 切片原先數(shù)組,返回是字符串,中間用“空格”分開,因此如果加上”()”,將得到切片數(shù)組,上面例子:c 就是一個新數(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ù)組名[@或*]/查找字符/替換字符} 該操作不會改變原先數(shù)組內(nèi)容,如果需要修改,可以看上面例子,重新定義數(shù)據(jù)。
以上內(nèi)容是小編給大家介紹的linux shell 數(shù)組建立及使用技巧,希望對大家有所幫助。
標簽:優(yōu)質小號 百色 自貢 武漢 丹東 鎮(zhèn)江 六盤水 滁州
巨人網(wǎng)絡通訊聲明:本文標題《Linux Shell 數(shù)組建立及使用技巧》,本文關鍵詞 Linux,Shell,數(shù)組,建立,及,;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。