linux 第四章 shell编程及自动化运维–数组
数组
数组简介
变量:用一个固定的字符串,代替一个不固定的字符串。
数组:用一个固定的字符串,代替多个不固定字符串。
类型
普通数组:只能使用整数作为数组索引
[root@localhost ~] hero=(zhugelinag anqila baiqi buzhihuowu daji)
[root@localhost ~] echo ${hero[*]}
zhugelinag anqila baiqi buzhihuowu daji
自定义索引
[root@localhost ~] hero2=([0]=zhugeliang [1]=anqila [2]=baiqi [3]buzhihuowu [4]=daji )
看索引
[root@localhost ~] echo ${!hero[*]}
0 1 2 3 4
关联数组:可以使用字符串作为数组索引
[root@localhost ~] declare -A position #声明关联数组
[root@localhost ~] position=([up]=白起 [center]=小乔 [down]=孙策)
[root@localhost ~] echo ${position[*]}
白起 小乔 孙策
[root@localhost ~] echo ${!position[*]}
up center down
总结:变量切片有个索引的概念。一个索引(整数)。
普通数组:中的索引对应一个字符串
关联数组:数组中的索引可以使用字符串。
一、普通数组
定义数组:
方法一:一次赋一个值
[root@localhost ~] array1[0]=pear
[root@localhost ~] array1[2]=orange
[root@localhost ~] array1[3]=apple
[root@localhost ~] echo ${array1[*]}
pear orange apple
[root@localhost ~] echo ${!array1[*]}
0 2 3
查看数组
[root@localhost ~] echo ${array1[@]}
pear orange apple
他会把计算机当中所有的普通数组都显示出来
[root@localhost ~] declare -a
方法二:
把一个文件的内容传递给了数组
[root@localhost ~] array2=(tom jack alice)
[root@localhost ~] array3=(`cat /etc/passwd`)
[root@localhost ~] echo ${array3[1]}
bin:x:1:1:bin:/bin:/sbin/nologin
数组还能调用变量
[root@localhost ~] red=111
[root@localhost ~] blue=222
[root@localhost ~] colors=($red $blue)
[root@localhost ~] echo ${colors[*]}
111 222
如果你定义的数组当中有空格一定要加上转义符
[root@localhost ~] array5=(tom jack alice "bash shell")
[root@localhost ~] echo ${array5[3]}
bash shell
二、关联数组
- 定义关联数组:
方法一
切记先声明关联数组
[root@localhost ~] declare -A ass_array1
[root@localhost ~] ass_array1[index1]=pear
[root@localhost ~] ass_array1[index2]=apple
[root@localhost ~] ass_array1[333]=orange #此时333不在有逻辑特性
[root@localhost ~] echo ${ass_arrary1[*]}
pear apple orange
[root@localhost ~] echo ${!ass_arrary1[*]}
index1 index2 333
方法二:一次赋值多个
[root@localhost ~] declare -A ass_array2
[root@localhost ~] ass_array2=([index1]=tom [index2]=jack [index3]=alice )
查看所有关联数组:
[root@localhost] declare -A
declare -A BASH_ALIASES='()'
declare -A BASH_CMDS='()'
declare -A pos='([up]="白起" [center]="小乔" )'
获取数组元素的个数
[root@localhost] echo ${#pos[*]}
2
数组和循环
1:通过循环定义和显示数组
2:通过数组统计数据
###案例
案例1:for脚本快速定义数组
自己创一个文件用于做实验
[root@localhost ~] vim /etc/hostss
111 222 333
aaa bbb ccc
[root@localhost ~] vim for_array.sh
#!/bin/bash
#定义数组
for aaa in `cat /etc/hostss`
do
hosts[i++]=$aaa #i++从0开始,++i从1开始
done
#调用数组,查看
for bbb in ${!hosts[*]}
do
echo "第$bbb个索引,它的值是:${hosts[$bbb]}"
done
[root@localhost ~] bash for_array.sh
第0个索引,它的值是:111
第1个索引,它的值是:222
第2个索引,它的值是:333
第3个索引,它的值是:aaa
第4个索引,它的值是:bbb
第5个索引,它的值是:ccc
案例2:数组统计性别
定义性别文件:(假如是从人力那拿过来的)
[root@localhost ~] vim sex.txt
jack m henan
alice f hebei
tom m beijing
虽然while是死循环,但是我们可以拿文件控制它的次数。拿一个文件作为while的输入重定向在while循环时设定交互式定义变量,这个文件里的内容会一行一行拿出来直到拿完位置。
#!/bin/bash
while read aaa #会把文件当中的一行赋值给aaa,而for使用空格和回车分割
do
echo $aaa
done < sex.txt
[root@localhost] bash while.sh
jack m henan
alice f hebei
tom m beijing
和这种写法有区别样
for i in `cat sex.txt`
do
echo $i
done
[root@localhost procedures]# bash while.sh
jack
m
henan
alice
f
hebei
tom
m
利用数组统计性别
[root@localhost]# vim while.sh
#!/bin/bash
declare -A sex
while read line
do
type=`echo $line | awk '{print $2}'`
let sex[$type]=sex[$type]+1
done < sex.txt
for i in ${!sex[*]}
do
echo "索引 $i:值是 ${sex[$i]}"
done
[root@localhost]# bash while.sh
索引 f:值是 1
索引 m:值是 2
案例3:使用数组统计,用户shell的类型和数量
[root@localhost] vim shell.sh
#!/bin/bash
#使用数组统计,用户shell的类型和数量
declare -A shells
while read ll
do
type=`echo $ll | awk -F: '{print $7}'` #-F:自定义分隔符用冒号
let shells[$type]++
done < /etc/passwd
for i in ${!shells[*]}
do
echo "索引/shell的类型是$i---------shell的数量是:${shells[$i]}"
done
[root@localhost procedures]# bash shell.sh
索引/shell的类型是/sbin/nologin---------shell的数量是:16
索引/shell的类型是/bin/sync---------shell的数量是:1
索引/shell的类型是/bin/bash---------shell的数量是:25
索引/shell的类型是/sbin/shutdown---------shell的数量是:1
索引/shell的类型是/sbin/halt---------shell的数量是:1
总结:while会用行来进行分割,而for使用空格和回车分割
解决方法:如何解决for的空格分割的问题使用IFS=$‘\n’。重新定义分隔符。