分類配列 |
通常の配列:配列のインデックスとしてのみを使用して、整数
連想配列:文字列は、配列のインデックスとして使用することができます
方法アレイ割り当て、各インデックスの割り当て |
配列名[インデックス] =変数値
[root@master1 ~]# test=jacqueline
[root@master1 ~]# test=(jacqueline test sqj)
[root@master1 ~]# echo $test
jacqueline
[root@master1 ~]# echo ${test[*]}
jacqueline test sqj
[root@master1 ~]# unset test ## unset 代表清空
[root@master1 ~]# test[0]=linuxstart
[root@master1 ~]# test[1]=lisi
[root@master1 ~]# test[2]=sqj
[root@master1 ~]# echo ${test[@]} ## @ 和 *都代表所有
linuxstart lisi sqj
[root@master1 ~]# echo ${test[*]}
linuxstart lisi sqj
[root@master1 ~]# echo ${test[1]}
lisi
[root@master1 ~]# echo ${test[0]}
linuxstart
[root@master1 ~]# echo ${!test[@]} ## 查看所有的索引
0 1 2
複数回配列の割り当て第二の方法は、値 |
名前=配列(変数複数の値)
[root@master1 ~]# array=(linux jacqueline test)
[root@master1 ~]# echo ${array[*]}
linux jacqueline test
[root@master1 ~]# array=(1 2 3 "test" [10]=jacqueline)
[root@master1 ~]# echo ${array[@]}
1 2 3 test jacqueline
[root@master1 ~]# echo ${!array[@]}
0 1 2 3 10
ビューの割り当て結果 |
[root@master1 ~]# declare -a ##查看普通数组
[root@master1 ~]# declare –A ##查看关联数组
配列の要素にアクセスする方法 |
[root@master1 ~]# array=(1 2 3 "test" [10]=jacqueline)
[root@master1 ~]# echo ${array[0]} ## 第一个元素
1
[root@master1 ~]# echo ${array[10]} ##
jacqueline
[root@master1 ~]# echo ${array[@]} ## 访问数组所有元素
1 2 3 test jacqueline
[root@master1 ~]# echo ${#array[@]} ## 统计元素的个数
5
配列のインデックスへのアクセス方法 |
配列要素のインデックスを取得します!
[root@master1 ~]# array=(1 2 3 "test" [10]=jacqueline)
[root@master1 ~]# echo ${!array[@]}
0 1 2 3 10
連想配列 |
1.1定義連想配列、連想配列を宣言されています
[root@master1 ~]# declare -A array_1 ##先申明,才能将索引转换为关联数组
[root@master1 ~]# declare -A array_2
方法の割り当て、各インデックスの割り当てに関連付けられている1.2
配列名[インデックス] =変数値
[root@master1 ~]# declare -A array_1
[root@master1 ~]# declare -A array_2
[root@master1 ~]# array_1[name]=jacqueline
[root@master1 ~]# array_1[age]=23
[root@master1 ~]# echo ${!array_1[@]}
name age
[root@master1 ~]# echo ${array_1[@]}
jacqueline 23
2の1.3関連の割り当て、値を複数回
[root@master1 ~]# declare array_2=([index1]=shao [index2]=qi [index3]=jun)
[root@master1 ~]# echo ${array_2[@]}
shao qi jun
[root@master1 ~]# echo ${!array_2[@]}
index1 index2 index3
[root@master1 ~]# declare -A
declare -A array_1='([name]="jacqueline" [age]="23" )'
declare -A array_2='([index1]="shao" [index2]="qi" [index3]="jun" )'
1.4 Accessデータ要素
[root@master1 ~]# declare array_2=([index1]=shao [index2]=qi [index3]=jun)
[root@master1 ~]# echo ${array_2[index1]}
shao
[root@master1 ~]# echo ${array_2[@]}
shao qi jun
[root@master1 ~]# echo ${!array_2[@]}
index1 index2 index3
反復行ケース |
悪い指標を通じて何度も確認してください
[root@master1 scripts]# unset array
[root@master1 scripts]# let array[m]++
[root@master1 scripts]# let a++
[root@master1 scripts]# echo $a
1
[root@master1 scripts]# let a++
[root@master1 scripts]# echo $a
2
コラムケース1:ジェンダー統計は、何度も表示されます
## 思路
let array_pa[m]++
let array_pa[f]++
let array_pa[x]++
let array_pa[m]++
$ echo ${array_pa[*]}
2 1 1
$ echo ${!array_pa[*]} ##查看索引(看下标
m f x
## 脚本
[root@master1 scripts]#cat count_sex.sh
#!/bin/sh
declare -A sex
while read line
do
type=$(echo $line|awk '{print $2}')
let sex[$type]++
done<sex.txt
#遍历数组
for i in ${!sex[*]}
do
echo $i共有${sex[$i]}个
done
2つのテキスト列:IPの訪問を記録しますnginxの統計
[root@master1 scripts]# # cat nginx_count.sh
#!/bin/sh
declare -A array_nginx
while read line
do
type=$(echo $line|awk '{print $1}')
let array_nginx[$type]++
done</var/log/nginx/access.log
for i in ${!array_nginx[*]}
do
echo "IP是 $i 共出现了${array_nginx[$i]}次"
done
コラムケース3:統計TCP状態情報
[root@master1 scripts]# cat nginx_status.sh
#!/bin/sh
declare -A array_nginx
type=`ss -an|grep 80|awk '{print $2}'`
for i in $type
do
let array_nginx[$i]++
done
for n in ${!array_nginx[*]}
do
echo $n ${array_nginx[$n]}
done