shell 数组

[TOC]

shell数组

01. shell数组的分类

  • 普通数组:只能以数字作为索引(也称之为下标)
  • 关联数组:可以使用字符作为索引

02. shell数组常用定义方式

定义普通数组

#定义普通数组1:
[root@shell ~]# array=(one two three)
#定义普通数组2:
[root@shell ~]# array=([0]=one [1]=two [2]=three)
#定义普通数组3:
[root@shell ~]# array[0]=one
[root@shell ~]# array[1]=two
[root@shell ~]# array[2]=three

#不连续的索引赋值,双引号将里面的内容当成一个元素
[root@shell ~]# array_1=(oldboy name [5]=linux "centos shell" [10]=kvm)
[root@shell ~]# echo ${array_1[*]}
oldboy name linux centos shell kvm
[root@shell ~]# echo ${!array_1[*]}
0 1 5 6 10

定义管理数组

创建关联数组前需要声明

#定义关联数组1:
[root@shell ~]# declare -A oldqq
[root@shell ~]# oldqq=([index1]=www [index2]=bbs [index3]=zhihu)

#定义关联数组2:
[root@shell ~]# declare -A oldqq
[root@shell ~]# oldqq[index1]=www 
[root@shell ~]# oldqq[index2]=bbs 
[root@shell ~]# oldqq[index3]=zhihu

03. 查看shell数组

查看所有定义的普通数组

[root@shell ~]# declare -a

查看所有定义的关联数组

[root@shell ~]# declare -A

查看数组所有元素(*与@都可以)

[root@shell ~]# echo ${array[*]}        
one two three
[root@shell ~]# echo ${oldqq[@]}
index1 index2 index3

查看数组所有的索引(下标)

[root@shell ~]# echo ${!array[*]}       
0 1 2
[root@shell ~]# echo ${oldqq[*]}
www bbs zhihu

查看数组的单个元素

[root@shell ~]# echo ${array[0]}
one
[root@shell ~]# echo ${array[2]}
three
[root@shell ~]#  echo ${oldqq[index1]}
www
[root@shell ~]#  echo ${oldqq[index3]}
zhihu

04. 删除shell数组

删除单个数组元素

[root@shell ~]# unset array[3]  #<= 去掉下标为3的数组元素
[root@shell ~]# echo ${array[*]}
one two three

删除整个数组

[root@shell ~]# unset array      #<= 删除整个数组
[root@shell ~]# echo ${array[*]}    #<= 内容为空,说明删除成功
    

05. shell数组内容的截取和替换

[root@shell ~]# array=(a b c d e)   
[root@shell ~]# echo ${!array[*]}
0 1 2 3 4
[root@shell ~]# echo ${array[*]}
a b c d e
[root@shell ~]# echo ${array[*]:1:3}    #<== 截取元素1到元素3(默认元素从0开始)
b c d
[root@shell ~]# array=($(echo {a..g}))  #<== 将变量的结果赋值给数组元素
[root@shell ~]# echo ${array[*]}
a b c d e f g
[root@shell ~]# echo ${array[*]/a/1}    #<== 把数组中a替换为1,原数组未被真正修改
1 b c d e f g

#数组也是变量,因此也适合变量的字串处理功能
[root@shell ~]# array=(abcABCabc abcdefg)
[root@shell ~]# echo ${array[*]}
abcABCabc abcdefg
[root@shell ~]# echo ${array[*]#*c}
ABCabc defg
[root@shell ~]# echo ${array[*]#*b}
cABCabc cdefg
[root@shell ~]# echo ${array[*]##*b}
c cdefg
[root@shell ~]# echo ${array[*]%b*}
abcABCa a
[root@shell ~]# 
[root@shell ~]# echo ${array[*]%%b*}
a a

06. shell数组循环打印

#通过C语言类型的for循环
[root@shell scripts]# cat test01sh
#!/bin/bash
ipadd=(
    10.0.0.11
    10.0.0.22
    10.0.0.33
)

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

#通过普通for循环
[root@shell scripts]# cat test02sh
#!/bin/bash
ipadd=(
    10.0.0.11
    10.0.0.22
    10.0.0.33
)

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

#两种打印结果是相同的,这里省略..
[root@shell scripts]# sh test01h
10.0.0.11
10.0.0.22
10.0.0.33

07. 案例:

计算以下数据中性别出现的次数

[root@shell ~]# cat sex.txt |column -t
zhangsan     male
lisi         female
wangwu       male
lili         male
xx           female
mayun        male
mahuateng    male
wangjianlin  male
cxk          unknown

编写的脚本

[root@shell ~]# cat sex.sh
#!/bin/bash
for i in `awk '{print $2}' /root/sex.txt`
do
    declare -A sex
    let sex[$i]++
done

for a in ${!sex[*]}
do
    echo "性别:$a 出现了:${sex[$a]}"
done

#脚本执行结果
[root@shell ~]# sh sex.sh |column -t
性别:male     出现了:6
性别:unknown  出现了:1
性别:female   出现了:2
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 官网 中文版本 好的网站 Content-type: text/htmlBASH Section: User ...
    不排版阅读 4,724评论 0 5
  • 一:定义 变量:存储单个元素的内存空间 数组:存储多个元素的连续的内存空间,相当于多个变量的集合 数组名和索引索引...
    芷_念阅读 12,367评论 0 4
  • shell脚本编程 变量:存储单个元素的内存空间;数组:存储多个元素的连续的内存空间;├── 数组名:整个数组只有...
    人間失格_430b阅读 278评论 0 0
  • 数组数组介绍基本数组关联数组案列分享一、数组介绍一个变量只能存一个值,但是现实中又很多值需要存储,那么变量就有些拘...
    亮仔_c1b5阅读 497评论 0 0
  • Shell 数组 数组中可以存放多个值。Bash Shell 只支持一维数组(不支持多维数组),初始化时不需要定义...
    Joyner2018阅读 343评论 0 0

友情链接更多精彩内容