shell 运维实战 数组

一 数组

一 数组简介:
1.变量;
用一个固定的字符串,代替一个不固定字符串。
2.数组:
用一个固定的字符串,代替多个不固定字符串。

二 类型:
1.普通数组:
只能使用整数作为数组索引
2.关联数组:
可以使用字符串作为数组索引
3.总结:
变量切片有个索引的概念。一个索引(整数)对应一个字符。
普通数组:中的索引对应一个字符串。
关联数组:数组中的索引可以使用字符串。

三 普通数组:
1.定义数组:
方法1: 一次赋一个值
数组名[下标]=变量值# array1[0]=pear
#array1[1]=apple
#array1[2]=orange
#array1[3]=peach
查看数组
[root@localhost ~]# declare -a | grep array1
declare -a array1=’([0]=“pear” [1]=“apache” [2]=“orange” [3]=“peach”)’
查看数组
[root@localhost ~]# echo ${array1[@]}
pear apache orange peach

方法2: 一次赋多个值
#array2=(tom jack alice)
#array3=(cat /etc/passwd) 希望是将该文件中的每一个行作为一个元数赋值给数组array3
#array4=(ls /var/ftp/Shell/for*)
#array5=(tom jack alice “bash shell”)
#colors=($red $blue $green $recolor)
#array6=(1 2 3 4 5 6 7 “linux shell” [20]=saltstack)

2.访问数组元素:
#echo ${array1[0]} 访问数组中的第一个元数
#echo ${array1[@]} 访问数组中所有元数 等同于 echo ${array1[*]}
#echo ${#array1[@]} 统计数组元素的个数
#echo ${!array2[@]} 获取数组元素的索引
#echo ${array1[@]:1} 从数组下标1开始
#echo ${array1[@]:1:2} 从数组下标1开始,访问两个元素

四 关联数组:
1.定义关联数组:
方法1:一次赋一个值
数组名[索引]=变量值
#declare -A ass_array1
#ass_array1[index1]=pear
#ass_array1[index2]=apple
#ass_array1[index3]=orange
#ass_array1[index4]=peach
查看
[root@localhost ~]# echo ${ass_array1[@]}
peach pear apple orange

方法2: 一次赋多个值
#declare -A ass_array2
#ass_array2=([index1]=tom [index2]=jack [index3]=alice [index4]=‘bash shell’)

2.查看数组:
#declare -A
declare -A ass_array1=’([index4]=“peach” [index1]=“pear” [index2]=“apple” [index3]=“orange” )’
declare -A ass_array2=’([index4]=“bash shell” [index1]=“tom” [index2]=“jack” [index3]=“alice” )

3.访问数组元素:
#echo ${ass_array2[index2]} 访问数组中的第二个元数
#echo ${ass_array2[@]} 访问数组中所有元数 等同于 echo ${array1[*]}
#echo ${#ass_array2[@]} 获得数组元数的个数
#echo ${!ass_array2[@]} 获得数组元数的索引

五 数组和循环:
1:通过循环定义和显示数组
2:通过数组统计数据

六 案例:
案例1:while脚本快速定义数组
定义数组:
#!/bin/bash
#循环读取文件,定义数组
while read line
do
#hosts:数组名
#[++i]:索引递增,++i是1开始,i++是0开始。
# l i n e : 值 , 即 文 件 中 的 内 容 h o s t s [ + + i ] = line:值,即文件中的内容 hosts[++i]= line:hosts[++i]=line
done < /etc/hosts

#输出索引每一行
for i in ! h o s t s [ @ ] d o e c h o " {!hosts[@]} do echo " !hosts[@]doecho"i : KaTeX parse error: Expected '}', got 'EOF' at end of input: {hosts[i]}"
done
测试数组:
[root@localhost ~]# bash array1.sh
“数组hosts first:127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4”

1 : 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
2 : ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

案例2:数组统计性别
1.定义性别文件:
[root@localhost ~]# vim sex.txt
jack m
alice f
tom m
2.定义脚本统计性别:
#!/bin/bash
declare -A sex
while read line
do
type=echo $line|awk '{print $2}'
let sex[$type]++
done < sex.txt

for i in ! s e x [ @ ] d o e c h o " {!sex[@]} do echo " !sex[@]doecho"i : KaTeX parse error: Expected '}', got 'EOF' at end of input: {sex[i]}"
done
3.测试脚本:
[root@localhost ~]# bash sex.sh
f : 1
m : 2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值