国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Linux > 正文

Linux Shell 數組建立及使用技巧

2019-10-26 18:47:03
字體:
來源:轉載
供稿:網友

linux shell在編程方面比windows 批處理強大太多,無論是在循環、運算。已經數據類型方面都是不能比較的。 下面是個人在使用時候,對它在數組方面一些操作進行的總結。

1.數組定義

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

一對括號表示是數組,數組元素用“空格”符號分割開。

2.數組讀取與賦值

得到長度:

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

用${#數組名[@或*]} 可以得到數組長度

讀取:

[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

調用方法是:${數組名[@或*]/查找字符/替換字符} 該操作不會改變原先數組內容,如果需要修改,可以看上面例子,重新定義數據。

以上內容是小編給大家介紹的linux shell 數組建立及使用技巧,希望對大家有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 香格里拉县| 禄丰县| 论坛| 巴塘县| 克什克腾旗| 屏山县| 错那县| 宜章县| 房产| 宁都县| 银川市| 周至县| 友谊县| 平远县| 健康| 南涧| 高安市| 丰县| 南投市| 昌宁县| 什邡市| 吉首市| 乐陵市| 成安县| 广汉市| 凤翔县| 尼勒克县| 定结县| 隆德县| 灵石县| 隆尧县| 广饶县| 正宁县| 东乡| 盐亭县| 营口市| 岑溪市| 托里县| 康保县| 天祝| 宝丰县|