linux shell在編程方面比windows 批處理強大太多,不管是在循環、運算。已經數據類型方面都是不能比較的。 下面是我的在使用時候,對它在數組方面一些操做進行的總結。linux
1.數組定義shell
[chengmo@centos5 ~]$ a=(1 2 3 4 5)
[chengmo@centos5 ~]$ echo $a
1編程
一對括號表示是數組,數組元素用「空格」符號分割開。windows
2.數組讀取與賦值centos
獲得長度:數組
[chengmo@centos5 ~]$ echo ${#a[@]}
5ide
用${#數組名[@或*]} 能夠獲得數組長度spa
讀取:orm
[chengmo@centos5 ~]$ echo ${a[2]}
3字符串
[chengmo@centos5 ~]$ echo ${a[*]}
1 2 3 4 5
用${數組名[下標]} 下標是從0開始 下標是:*或者@ 獲得整個數組內容
賦值:
[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
直接經過 數組名[下標] 就能夠對其進行引用賦值,若是下標不存在,自動添加新一個數組元素
刪除:
[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 數組[下標] 能夠清除相應的元素,不帶下標,清除整個數據。
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
直接經過 ${數組名[@或*]:起始位置:長度} 切片原先數組,返回是字符串,中間用「空格」分開,所以若是加上」()」,將獲得切片數組,上面例子:c 就是一個新數據。
替換:
[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
調用方法是:${數組名[@或*]/查找字符/替換字符} 該操做不會改變原先數組內容,若是須要修改,能夠看上面例子,從新定義數據。