shell-数组

1. 数组

1.1 什么是shell数组

shell的数组就是把有限个元素(变量或字符内容)用一个名字命名,然后用编号对他们进行区分的元素集合。这个名字就称为数组名,用于区分不同内容的编号就称为数组下标。组成数组的各个元素(变量)称为数组的元素,有时也称为下标变量。下标默认是从0开始。

1.2 数组的分类

普通数组:只能使用整数 作为数组索引 
关联数组:可以使用字符串 作为数组索引

1.3 数组的赋值方式

数组赋值方式: 一次赋多个值
数组名=(多个变量值)

[root@shell scripts]# array=(1 2 3 4 5)
[root@shell scripts]# echo ${array[0]}
1
[root@shell scripts]# echo ${array[1]}
2
[root@shell scripts]# echo ${array[2]}
3
[root@shell scripts]# echo ${array[3]}
4
[root@shell scripts]# echo ${array[4]}
5

数组赋值方式:针对每个索引进行赋值
数组名[索引]=变量值

[root@shell ~]# arraytest[0]=beijing
[root@shell ~]# arraytest[1]=shenzhen
[root@shell ~]# arraytest[2]=shanghai

数组赋值方式:第三种

[root@shell scripts]#array=([1]=one [2]=two [3]=three) 
[root@shell scripts]# array=([1]=one [2]=two [3]=three) 
[root@shell scripts]# echo ${array[*]}
one two three
[root@shell scripts]# echo ${array[0]}

[root@shell scripts]# echo ${array[1]}
one 

数组赋值方式:动态赋值

array=($(ls))
array=(`ls`)

覆盖

[root@shell scripts]# array=([1]=one [2]=two [3]=three) 
[root@shell scripts]# echo ${array[*]}
one two three
[root@shell scripts]# echo ${array[0]}

[root@shell scripts]# echo ${array[1]}
one
[root@oshell scripts]# array[0]=a;array[1]=b;array[2]=c
[root@shell scripts]# echo ${array[*]}
a b c three

1.4 查看数组

查看数组所有元素
[root@shellscripts]# echo ${array[*]}
1 2 3 4 5
[root@shell scripts]# echo ${array[@]}
1 2 3 4 5
统计元素的个数
[root@shell scripts]# echo ${#array[@]} 
5
[root@shell scripts]# echo ${#array[*]}
5
获取数组元素的索引 
[root@shell scripts]# echo ${!array[@]}
0 1 2 3 10

1.5 查看数组的赋值结果

[root@shell ~]#  declare –a     查看普通数组
[root@shell ~]#  declare –A     查看关联数组

1.6 关联数组

关联数组可以用字符串当索引

1.6.1定义关联数组
[root@shell ~]# declare -A array_1
[root@shell ~]# declare -A array_2
1.6.2 关联数组赋值

关联数组的赋值方式一,针对每个索引进行赋值

#数组名[索引]=变量值
[root@shell ~]# declare -A array_1
[root@shell ~]# declare -A array_2
[root@shell ~]# array_1[index1]=beijing
[root@shell ~]# array_1[index2]=shenzhen
[root@shell ~]# array_1[index3]=shanghai

关联数组的赋值方式二,一次赋多个值

[root@shell ~]#  array_2=([index1]=beijing [index2]=shenzhen [index3]=shanghai)

访问数据元素

[root@shell ~]# echo ${array_2[index2]}
shenzhen
[root@shell ~]# echo ${array_2[@]}     
beijing shenzhen shanghai
[root@shell ~]# echo ${!array_2[@]} 
index1 index2 index3

1.7 例题
输出123456两种方法

array=(1 2 3 4 5)
for n in ${array[*]}
do
    echo $n
done

echo --------------
for((i=0;i<${#array[*]};i++))
do
    echo ${array[i]}
done  

利用bash for循环打印下面这句话中字母数不大于6的单词(某企业面试真题)。
I am English teacher welcome to English training class
要求用定义数组完成

array=(I am English teacher welcome to English training c
lass)
echo "第一种"
for n in ${array[*]}
do
    if [ ${#n} -lt 7 ]
    then
        echo $n
    else
        continue
    fi
done

echo "第二种"
for((i=0;i<${#array[*]};i++))
do
   [ ${#array[i]} -le 6 ]&&
       echo ${array[i]}
done
方法3:通过awk循环实现。
[root@shell scripts]# chars="I am English teacher welcome to English training class"
[root@shell scripts]# echo $chars|awk '{for(i=1;i<=NF;i++) if(length($i)<=6)print $i}' 

1.8 遍历数组

范例1

已知文件里有名字对应的性别 请统计性别出现的总次数
let array_pa[m]++
let array_pa[f]++
let array_pa[x]++
let array_pa[m]++
其实就是对相同索引进行计数

[root@shell ~]# cat sex.txt 
gs m
eg m
mz f
wr m
zs f
ls m
[root@shell ~]# 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

统计nginx日志IP访问次数
[root@shell ~]# 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@shell ~]# 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
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容

  • Shell编程之数组 1.什么是数组: 数组其实也算是变量,传统的变量只能是存储一个值,但是数组可以存储多个值。 ...
    你好树洞先生阅读 243评论 0 1
  • 数组数组介绍基本数组关联数组案列分享一、数组介绍一个变量只能存一个值,但是现实中又很多值需要存储,那么变量就有些拘...
    亮仔_c1b5阅读 439评论 0 0
  • 数组 一、数组介绍一个变量只能存一个值,但是现实中又很多值需要存储,那么变量就有些拘谨了。 二、基本数组数组可以让...
    巴巴11阅读 143评论 0 0
  • (一)数组定义 数组:存储多个元素的连续的内存空间,相当于多个变量的集合 数组名和索引 索引:编号从0 开始,属于...
    王王王小白阅读 2,316评论 0 0
  • shell脚本编程 变量:存储单个元素的内存空间;数组:存储多个元素的连续的内存空间;├── 数组名:整个数组只有...
    人間失格_430b阅读 215评论 0 0