2016-08-22
授課內容:
數組
高級字符串操作
一、數組
變量:存儲單個元素的內存空間
數組:存儲多個元素的連續的內存空間,相當于多個變量的集合。
數組名和索引
索引:編號從0開始,屬于數值索引
bash的數組支持稀疏格式(索引不連續)
1、聲明數組:
declare -a ARRAY_NAME(并非強制聲明,但最好按規范聲明)
declare -A ARRAY_NAME: 關聯數組
2、數組元素的賦值:
(1) 一次只賦值一個元素;
ARRAY_NAME[INDEX]=VALUE
[19:41 root@Centos7.2~]# arr=(1 2 3 4 5) [19:41 root@Centos7.2~]# echo ${arr[@]} 1 2 3 4 5 [19:41 root@Centos7.2~]# arr[5]=88 [19:41 root@Centos7.2~]# echo ${arr[@]} 1 2 3 4 5 88 [19:41 root@Centos7.2~]# arr[10]=188 [19:42 root@Centos7.2~]# echo ${arr[@]} 1 2 3 4 5 88 188
(2) 一次賦值全部元素:
ARRAY_NAME=("VAL1" "VAL2" "VAL3" …)
[19:41 root@Centos7.2~]# arr=(1 2 3 4 5) [19:41 root@Centos7.2~]# echo ${arr[@]} 1 2 3 4 5
3、引用數組:
引用數組元素:${ARRAY_NAME[INDEX]},注意:省略[INDEX]表示引用下標為0的元素
[19:43 root@Centos7.2~]# echo ${arr[@]} 1 2 3 4 5 88 188 [19:43 root@Centos7.2~]# echo ${arr[1]} 2
數組的長度(數組中元素的個數):
${#ARRAY_NAME[*]}
${#ARRAY_NAME[@]}
[19:44 root@Centos7.2~]#echo ${arr[@]} 1 2 3 4 5 88 188 [19:44 root@Centos7.2~]# echo ${#arr[@]} 7 [19:44 root@Centos7.2~]# echo ${#arr[*]} 7
4、數組數據處理:切片
數組切片:${ARRAY[@]:offset:number}
offset: 要跳過的元素個數
number: 要取出的元素個數
取偏移量之后的所有元素${ARRAY[@]:offset}
[19:46 root@Centos7.2~]# echo ${arr[@]} 1 2 3 4 5 88 188 [19:46 root@Centos7.2~]# echo ${arr[@]:2:3} 3 4 5 #跳過兩個元素,取后面三個元素
5、刪除數組中的某元素:導致稀疏格式
unset ARRAY[INDEX]
二、字符串處理
1、字符串切片:
${#var}:返回字符串變量var的長度
${var:offset}:返回字符串變量var中從第offset個字符后(不包括第offset個字符)的字符開始,到最后的部分,offset的取值在0 到${#var}-1 之間
${var:offset:number}:返回字符串變量var中從第offset個字符后(不包括第offset個字符)的字符開始,長度為number的部分
${var: -lengh}:取字符串的最右側幾個字符:
注意:冒號后必須有一空白字符
[19:50 root@Centos7.2~]# str="nihaolinux" [19:51 root@Centos7.2~]# echo $str nihaolinux [19:51 root@Centos7.2~]# echo ${#str} 10 #取字符串的長度 [19:51 root@Centos7.2~]# echo ${str:2} haolinux #從第二個字符串(不包括第二個)開始到最后 [19:51 root@Centos7.2~]# echo ${str:2:3} hao #從第二個字符串(不包括第二個)開始取后三個字符 [19:51 root@Centos7.2~]# echo ${str: -5} linux#取最后側起5個字符
2、基于模式取子串:
(1)${var#*word}:其中word可以是指定的任意字符
功能:自左而右,查找var變量所存儲的字符串中,第一次出現的word, 刪除字符串開頭至第一次出現word字符之間的所有字符
(2)${var##*word}
同上,不同的是,刪除的是字符串開頭至最后一次由word指定的字符之間的所有內容
(3)${var%word*}:其中word可以是指定的任意字符;
功能:自右而左,查找var變量所存儲的字符串中,第一次出現的word, 刪除字符串最后一個字符向左至第一次出現word字符之間的所有字符;
(4)${var%%word*}
同上,只不過刪除字符串最右側的字符向左至最后一次出現word字符之間的所有字符;
[19:55 root@Centos7.2~]# str=`getent passwd root` [19:56 root@Centos7.2~]# echo $str root:x:0:0:,,62985600:/root:/bin/bash [19:56 root@Centos7.2~]# echo ${str#*root} #刪除第一次出現root(包含)字符串及之前的字符 :x:0:0:,,62985600:/root:/bin/bash [19:56 root@Centos7.2~]# echo ${str##*root} #刪除最后出現root(包含)字符串及之前的字符 :/bin/bash [19:56 root@Centos7.2~]# echo ${str%root*} #刪除從右到左第一次出現root(包含)字符串及之前的字符 root:x:0:0:,,62985600:/ [19:56 root@Centos7.2~]# echo ${str%%root*} #刪除從右到左最后出現root(包含)字符串及之前的字符
3、查找替換
(1)${var/pattern/substi}:
查找var所表示的字符串中,第一次被pattern所匹配到的字符串,以substi替換之
(2)${var//pattern/substi}:
查找var所表示的字符串中,所有能被pattern所匹配到的字符串,以substi替換之
(3)${var/#pattern/substi}:
查找var所表示的字符串中,行首被pattern所匹配到的字符串,以substi替換之
(4)${var/%pattern/substi}:
查找var所表示的字符串中,行尾被pattern所匹配到的字符串,以substi替換之
[19:57 root@Centos7.2~]# echo ${str/root/ROOT} ROOT:x:0:0:,,62985600:/root:/bin/bash [20:01 root@Centos7.2~]# echo ${str//root/ROOT} ROOT:x:0:0:,,62985600:/ROOT:/bin/bash [20:02 root@Centos7.2~]# echo ${str/#root/ROOT} ROOT:x:0:0:,,62985600:/root:/bin/bash [20:02 root@Centos7.2~]# echo ${str/%bash/BASH} root:x:0:0:,,62985600:/root:/bin/BASH
4、查找并刪除
${var/pattern}:查找var所表示的字符串中,刪除第一次被pattern所匹配到的字符串
${var//pattern}:所有
${var/#pattern}:首行
${var/%pattern}:行尾
[20:02 root@Centos7.2~]# echo ${str/root} :x:0:0:,,62985600:/root:/bin/bash [20:05 root@Centos7.2~]# echo ${str//root} :x:0:0:,,62985600:/:/bin/bash [20:05 root@Centos7.2~]# echo ${str#root} :x:0:0:,,62985600:/root:/bin/bash [20:06 root@Centos7.2~]# echo ${str%bash} root:x:0:0:,,62985600:/root:/bin/
三、創建臨時文件
mktemp命令:創建的臨時文件可避免沖突
mktemp[OPTION]… [TEMPLATE]
TEMPLATE: filename.XXX
【X至少要出現三個】
OPTION:
-d: 創建臨時目錄
–tmpdir=/DIR:指明臨時文件所存放的目錄位置
實例:
#mktemp–tmpdir=/testdirtest.XXXXXX
原創文章,作者:麥德良,如若轉載,請注明出處:http://www.www58058.com/39156
文章對數組的知識點整理的很詳細,并通過示例展示了數組的申明和調用,最后可以通過一個示例來展示數組在腳本中的用法,跟其在腳本中的作用,這樣整篇文章會更完整了。