Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

Linux 第十三天:陣列與進階字串處理一次搞懂

2016-08-22

今日主線:陣列(Array)與高階字串操作,最後補充臨時檔案的建立方式。

一、陣列:把多個值收進同一個容器

變數只是一塊用來存放單一元素的記憶體空間;陣列則是一段連續的記憶體空間,相當於多個變數的集合。每個陣列都由「陣列名」與「索引」組成,索引從 0 開始,屬於數值索引。值得一提的是,Bash 的陣列支援稀疏格式,也就是索引不必連續。

1. 宣告陣列

declare -a ARRAY_NAME 用於宣告普通陣列,declare -A ARRAY_NAME 則宣告關聯陣列。宣告並非強制,但依規範寫出宣告是好習慣。

2. 為陣列元素賦值

一次只賦值一個元素: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

一次賦值全部元素: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[@]}
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:offset} 取從第 offset 個字元之後(不含第 offset 個)到結尾的部分,offset 的範圍是 0 到 ${#var}-1;${var:offset:number} 再指定長度;${var: -length} 取最右側的幾個字元,注意冒號後必須留一個空白。

[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. 基於模式取子串

${var#*word}:自左而右找到第一次出現的 word,刪除字串開頭到該 word(含)之間的所有字元。${var##*word} 相同邏輯,但匹配的是最後一次出現。${var%word*}:自右而左找到第一次出現的 word,刪除從最右側字元向左到該 word(含)之間的內容。${var%%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}
:x:0:0:,,62985600:/root:/bin/bash
[19:56 root@Centos7.2~]# echo ${str##*root}
:/bin/bash
[19:56 root@Centos7.2~]# echo ${str%root*}
root:x:0:0:,,62985600:/
[19:56 root@Centos7.2~]# echo ${str%%root*}
(空字串)

3. 查找並替換

${var/pattern/substi} 只替換第一次匹配到的字串;${var//pattern/substi} 替換全部匹配;${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} 刪除行尾匹配。

[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 建立的臨時檔名帶有隨機碼,能有效避免與他人衝突。語法為 mktemp [OPTION]... [TEMPLATE],TEMPLATE 形如 filename.XXX,其中 X 至少要出現三個。常用選項:-d 建立臨時目錄;--tmpdir=/DIR 指定臨時檔存放位置。

實例:mktemp --tmpdir=/testdir test.XXXXXX

Copyright © Linux教程網 All Rights Reserved