1.數(shù)組定義
[chengmo@centos5 ~]$ a=(1 2 3 4 5)
[chengmo@centos5 ~]$ echo $a
一對括號表示是數(shù)組,數(shù)組元素用“空格”符號分割開。(網(wǎng)絡(luò)技術(shù)外包)
2.數(shù)組讀取與賦值
得到長度:
[chengmo@centos5 ~]$ echo ${
#a[@]}
用${#數(shù)組名[@或*]} 可以得到數(shù)組長度
讀取:
[chengmo@centos5 ~]$ echo ${a[2]}
3.[chengmo@centos5 ~]$ echo ${a[*]}
1 2 3 4 5
用${數(shù)組名[下標(biāo)]} 下標(biāo)是從0開始 下標(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
直接通過 數(shù)組名[下標(biāo)] 就可以對其進(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.直接通過:unset 數(shù)組[下標(biāo)] 可以清除相應(yīng)的元素,不帶下標(biāo),清除整個(gè)數(shù)據(jù)。(it外包)
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 就是一個(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ù)組名[@或*]/查找字符/替換字符} 該操作不會改變原先數(shù)組內(nèi)容,如果需要修改,可以看上面例子,重新定義數(shù)據(jù)。(電腦桌面維護(hù)外包)
艾銻無限是中國領(lǐng)先IT外包服務(wù)商,專業(yè)為企業(yè)提供IT運(yùn)維外包、電腦維護(hù)、網(wǎng)絡(luò)維護(hù)、網(wǎng)絡(luò)布線、辦公設(shè)備維護(hù)、服務(wù)器維護(hù)、數(shù)據(jù)備份恢復(fù)、門禁監(jiān)控、網(wǎng)站建設(shè)等多項(xiàng)IT服務(wù)外包,服務(wù)熱線:400-650-7820 聯(lián)系電話:010-62684652 咨詢QQ1548853602 地址:北京市海淀區(qū)北京科技會展2號樓16D,用心服務(wù)每一天,為企業(yè)的發(fā)展提升更高的效率,創(chuàng)造更大的價(jià)值。
更多的IT外包信息盡在艾銻無限http://www.dyfgwiyq.cn
相關(guān)文章