1. 程式人生 > 實用技巧 >瞭解下JUC的執行緒池學習六(工作執行緒內部類Worker原始碼分析)

瞭解下JUC的執行緒池學習六(工作執行緒內部類Worker原始碼分析)

Shell陣列:shell陣列的定義、陣列長度

Shell在程式設計方面比Windows批處理強大很多,無論是在迴圈、運算。

bash支援一維陣列(不支援多維陣列),並且沒有限定陣列的大小。類似與C語言,陣列元素的下標由0開始編號。獲取陣列中的元素要利用下標,下標可以是整數或算術表示式,其值應大於或等於0。

定義陣列

在Shell中,用括號來表示陣列,陣列阿元素用“空格”符號分割開。定義陣列的一般形式為:
array_name=(value1 ... valuen)
例如:

array_name=(value0 value1 value2 value3)

#陣列也是一個變數,只不過他有多個值,區別在於定義了陣列,有多種取值方式
[root@hass-12 ~]# a=`ls /tmp`;echo $a
check.sh systemd-private-14b199c09fb346a0a1464e9334e57ec3-nginx.service-gU1I5K systemd-private-45002b0a75ba46079a70971b526f8584-nginx.service-dI2xZr systemd-private-bbac906744e645909ee6c515c1540d4e-nginx.service-rokXRe vmware-root_6813-3879179888 vmware-root_6816-2822835213 vmware-root_6822-2822966282

或者

array_name=(
value0
value1
value2
value3
)

array_name=(
["0"]=value0
["1"]=value1
["2"]=value2
["3"]=value3
)

還可以單獨定義陣列的各個分量:

#可以不使用連續的下標,而且下標的範圍沒有限制。

array_name[0]=value0
array_name[1]=value1
array_name[2]=value2
array_name[5]=value5

array_name=([0]=0 [1]=1 [5]=5)
array_name=([5]=5 [0]=0 [1]=1)	#不按順序
array_name=(`ls .`)

array_name[-1]=value5	#定義陣列最後一個元素
array_name[-2]=value5

使用陣列

獲取陣列的長度

獲取陣列長度的方法與獲取字串長度的方法相同,例如:

純文字複製
# 取得陣列元素的個數
length=${#array_name[@]}
# 或者
length=${#array_name[*]}
# 取得陣列單個元素的長度
lengthn=${#array_name[n]}

${#res[]}	#取變數的字元個數,或元組內元素的個數,或元組內某個元素的字元個數

讀取陣列

讀取陣列元素值的一般格式是:

  ${array_name[index]}

例如:

valuen=${array_name}
valuen=${array_name[2]}

#不指定索引或下標,取第一個值

使用@ 或 * 可以獲取陣列中的所有元素,例如:

${array_name[*]}
${array_name[@]}

檢視系統中有哪些陣列

declare可以宣告變數的種類和屬性,declare擴充套件

#檢視所有陣列
[root@hass-11 ~]# name=(1 2 3)
[root@hass-11 ~]# declare

#檢視所有普通陣列
[root@hass-12 ~]# declare -a |grep name
name=([0]="1" [1]="2" [2]="3")

#檢視所有關聯陣列
[root@hass-11 ~]# declare -A |grep name
declare -A array='([gender]="male" [name]="syy" [age]="18" )'

檢視陣列的索引

[root@hass-11 ~]# a=(a b c)
[root@hass-11 ~]# echo ${a[1]}
b
[root@hass-12 ~]# echo ${a[*]}
1 2 3
[root@hass-11 ~]# echo ${!a[a]}	#索引取值,不能是值取索引
-bash: a: expression recursion level exceeded (error token is "a")
[root@hass-11 ~]# echo ${!a[*]}
0 1 2

刪除

#刪除元組
unset array

#指定索引刪除
[root@hass-11 ~]# a=1
[root@hass-11 ~]# a=(1 2 3 4 "aaa")	#變數被陣列覆蓋
[root@hass-11 ~]# echo $a
1
[root@hass-11 ~]# echo ${a[@]}
1 2 3 4 aaa
[root@hass-11 ~]# unset a	#刪除陣列
[root@hass-11 ~]# echo $a

[root@hass-11 ~]# a=(1 2 3 4 "aaa")
[root@hass-11 ~]# unset a[0]	#刪除數組裡的元素
[root@hass-11 ~]# echo ${a[*]}
2 3 4 aaa

#指定元素刪除,不改變原陣列
[root@hass-11 ~]# echo ${a[*]}
one two tree fore five six
[root@hass-11 ~]# unset ${a[two]}
[root@hass-11 ~]# echo ${a[*]}
one two tree fore five six

[root@hass-11 ~]# unset ${a[*]#two}
[root@hass-11 ~]# echo ${a[*]}
one two tree fore five six

擷取

#指定元素擷取(顧左不顧右)

[root@hass-11 ~]# echo ${a[*]:two}
one two tree fore five six
[root@hass-11 ~]# echo ${a[*]:two:1}
one
[root@hass-11 ~]# echo ${a[*]:two:2}
one two
[root@hass-11 ~]# echo ${a[*]::two:2}	#陣列不支援從右邊擷取
-bash: a[*]: two:2: syntax error in expression (error token is ":2")

替換,刪除

#指定元素替換
[root@hass-11 ~]# echo ${a[*]}
one two tree fore five six
[root@hass-11 ~]# echo ${a[*]/one/two}
two two tree fore five six
[root@hass-11 ~]# echo ${a[*]//two/one}	#匹配多個
one one tree fore five six
[root@hass-11 ~]# echo ${a[*]//one/}	#刪除元組內的元素,但不是真正的刪除
two tree fore five six
[root@hass-11 ~]# echo ${a[*]}
one two tree fore five six

關聯陣列

關聯陣列指的是可以指定 (["key"]:"value")

#定義一個普通陣列,declare -a 可以省略
[root@hass-11 ~]# declare -a array=("a" "b" "c" "d")
[root@hass-11 ~]# echo ${array[*]}
a b c d

#定義一個關聯陣列
[root@hass-11 ~]# declare -A array=(["name"]="syy" ["age"]=18 ["gender"]="male")

#取關聯陣列的value
[root@hass-11 ~]# echo ${array["name"]}
syy
#取關聯陣列的key
[root@hass-11 ~]# echo ${!array["syy"]}	#只能通過key取value,不能通過value取key
...

#檢視關聯陣列所有value,陣列內元素是沒有順序的
[root@hass-11 ~]# echo ${array[*]}
male syy 18
#檢視關聯陣列所有key
[root@hass-11 ~]# echo ${!array[*]}
gender name age

#同種屬性的值使用普通陣列
#不同屬性使用關聯陣列
#資料可以自由選擇不同的陣列

陣列的遍歷

遍歷普通陣列的方式1,取值

array=(one two three four five fire)

for i in ${array[*]}
do
    echo $i
done

遍歷普通陣列的方式2,取索引,再取值

array=(one two three four five fire)

for i in ${!array[*]}
do
    echo $i ${array[$i]}  # 獲取索引及其對應的元素
done

遍歷關聯陣列的方式1,取value

declare -A array=(["name"]="egon" ["age"]=18 ["gender"]="male")

for x in ${array[*]}
do
   echo $x
done

遍歷關聯陣列的方式2,取key,進而取value

declare -A array=(["name"]="egon" ["age"]=18 ["gender"]="male")

for k in ${!array[*]}
do
   echo $k ${array[$k]}
done

練習:陣列的使用

基於關聯陣列統計/etc/passwd檔案中不同種類shell的個數

[root@egon day06]# cat 11.sh 
#!/bin/bash

declare -A shell_count
while read line
do
        shell=`echo $line|awk -F: '{print $7}'`
        let shell_count["$shell"]++
done < /etc/passwd

for key in ${!shell_count[*]}
do
        echo $key : ${shell_count["$key"]}
done
[root@egon day06]# ./11.sh 
/sbin/nologin : 22
/bin/sync : 1
/bin/bash : 4
/sbin/shutdown : 1
/sbin/halt : 1
#普通陣列的自增
。。。無意義

#關聯陣列的自增
[root@hass-12 ~]# declare -A d_name
[root@hass-12 ~]# let d_name["age"]++	#1.定義一個key 2.value從0開始自增
[root@hass-12 ~]# echo ${d_name[*]}
1
[root@hass-12 ~]# echo ${!d_name[*]}
age
[root@hass-12 ~]# echo ${d_name["age"]}
1

[root@hass-12 ~]# let d_name["age"]++
[root@hass-12 ~]# let d_name["age"]++
[root@hass-12 ~]# let d_name["age"]++
[root@hass-12 ~]# echo ${d_name[*]}
4
[root@hass-12 ~]# echo ${!d_name[*]}
age