数组变量可以存放多个值;bash只支持一维数组,初始化时不需要定义数组大小。
bash数组分为普通数组和关联数组,普通数组索引下标为从0开始的整数;关联数组的索引下边可以是字符串。
bash数组用小括号来表示,元素用空格分隔。
1、普通数组:
1.1、赋值:
array1=(value1 value2 value3...)
或
array1[0]=value1
array1[1]=value2
array1[2]=value3
...
1.2、查看数组值:
变量 | 取值 |
---|---|
echo ${array1[0]} | 数组第一个元素 |
echo ${array1[@]} 或 echo ${array1[*]} | 数组所有元素 |
echo ${#array1[@]} | 数组元素个数 |
echo ${!array1[@]} | 数组所有索引下标 |
echo ${array1[@]:1} | 数组索引1以后的元素 |
echo ${array1[@]:1:2} | 数组索引1以后2个元素 |
declare -a | 查看所有普通数组 |
1.3、实例
[root@localhost scripts]# ar1[0]=tom
[root@localhost scripts]# ar1[1]=hass
[root@localhost scripts]# ar1[2]=marry
[root@localhost scripts]#
[root@localhost scripts]# ar2=(123 456 789 1001)
##文件中空格分隔的字符串赋值给数组元素,不是换行符分隔
[root@localhost scripts]# ar3=(`cat /etc/hosts`)
[root@localhost scripts]# a=1;b=2;c=3
[root@localhost scripts]# ar4=($a $b $c)
##数组赋值时,索引可以跳跃
[root@localhost scripts]# ar5=(a b c "zhang san" "li si" [10]=d)
[root@localhost scripts]#
##查看所有普通数组
[root@localhost scripts]# declare -a
declare -a BASH_ARGC='()'
declare -a BASH_ARGV='()'
declare -a BASH_LINENO='()'
declare -a BASH_SOURCE='()'
declare -ar BASH_VERSINFO='([0]="4" [1]="2" [2]="46" [3]="2" [4]="release" [5]="x86_64-redhat-linux-gnu")'
declare -a DIRSTACK='()'
declare -a FUNCNAME='()'
declare -a GROUPS='()'
declare -a PIPESTATUS='([0]="0")'
declare -a ar1='([0]="tom" [1]="hass" [2]="marry")'
declare -a ar2='([0]="123" [1]="456" [2]="789" [3]="1001")'
declare -a ar3='([0]="127.0.0.1" [1]="localhost" [2]="localhost.localdomain" [3]="localhost4" [4]="localhost4.localdomain4" [5]="::1" [6]="localhost" [7]="localhost.localdomain" [8]="localhost6" [9]="localhost6.localdomain6")'
declare -a ar4='([0]="1" [1]="2" [2]="3")'
declare -a ar5='([0]="a" [1]="b" [2]="c" [3]="zhang san" [4]="li si" [10]="d")'
[root@localhost scripts]#
[root@localhost scripts]# declare -a |grep ar5
declare -a ar5='([0]="a" [1]="b" [2]="c" [3]="zhang san" [4]="li si" [10]="d")'
[root@localhost scripts]#
[root@localhost scripts]# echo ${ar5[0]}
a
[root@localhost scripts]#
[root@localhost scripts]# echo ${ar5[@]}
a b c zhang san li si d
[root@localhost scripts]#
[root@localhost scripts]# echo ${#ar5[@]}
6
[root@localhost scripts]#
[root@localhost scripts]# echo ${!ar5[@]}
0 1 2 3 4 10
[root@localhost scripts]#
[root@localhost scripts]# echo ${ar5[@]:1}
b c zhang san li si d
[root@localhost scripts]#
[root@localhost scripts]# echo ${ar5[@]:1:2}
b c
[root@localhost scripts]#
##字符串截取也可以使用上面的语法
[root@centos7u7 ~]# a='123456789'
[root@centos7u7 ~]#
[root@centos7u7 ~]# echo ${a:3}
456789
[root@centos7u7 ~]#
[root@centos7u7 ~]# echo ${a:3:3}
456
##在ksh中字符串不能用上面语法截取,可以使用cut截取
[root@centos7u7 ~]# echo $a|cut -b 4-6
456
2、关联数组:
关联数组必须显示定义.
2.1、定义赋值:
declare -A array2
array2=([name]=zhangsan [sex]=male [age]=29)
2.2、实例:
[root@localhost scripts]# declare -A user
[root@localhost scripts]#
[root@localhost scripts]# user=([name]="lisi" [sex]=male [age]=30 )
[root@localhost scripts]#
[root@localhost scripts]# declare -A
declare -A BASH_ALIASES='()'
declare -A BASH_CMDS='()'
declare -A user='([name]="lisi" [age]="30" [sex]="male" )'
[root@localhost scripts]#
[root@localhost scripts]# echo ${user[name]}
lisi
[root@localhost scripts]#
[root@localhost scripts]# echo ${user[age]}
30
[root@localhost scripts]#
[root@localhost scripts]# echo ${user[sex]}
male
[root@localhost scripts]#
3、使用数组统计/etc/passwd中shell个数的shell脚本实例:
[root@localhost scripts]# cat count_shell.sh
#!/usr/bin/bash
#count shell
#v1.0 by zhoayq 20191212
declare -A shs
#计算结果
while read line
do
sh_type=`echo $line|awk -F":" '{print $NF}'`
let shs[$sh_type]++
done < /etc/passwd
#echo ${shs[@]}
#echo ${shs[*]}
##显示统计结果
for i in ${!shs[@]}
do
echo "$i: ${shs[$i]}"
done
[root@localhost scripts]#
[root@localhost scripts]#
[root@localhost scripts]# ./count_shell.sh
/sbin/nologin: 16
/bin/sync: 1
/bin/bash: 1
/sbin/shutdown: 1
/sbin/halt: 1
[root@localhost scripts]#