Shell数组
数组中可以存放多个值。Bash Shell 只支持一维数组(不支持多维数组),初始化时不需要定义数组大小(与 PHP 类似)。
与大部分编程语言类似,数组元素的下标由0开始。
数组的通俗解释
何为数组?学过计算机编程语言的同学都知道,数组的特性就是一组数据类型相同的集合(不包括有一些编程语言提出来的关联数组的概念)。那么shell中数组是怎么定义的呢,我们来看两种数据类型:一是数值类型,二是字符串类型;虽然shell本身是弱类型的,但也可以这么区分。
数值类型的数组:一对括号表示数组,数组中元素之间使用“空格”来隔开。
举个列子:
arr_number=(1 2 3 4 5);
字符串类型数组:同样,使用一对括号表示数组,其中数组中的元素使用双引号或者单引号包含,同样使用“空格”来隔开。
arr_string=(“abc” “edf” “sss”); 或者 arr_string=(‘abc’ ‘edf’ ‘sss’);
数组往往配合循环使用
数组名称 arr=(11 22 33 44 55] 那arr就是属组名称 自己定义
数组元素 11,22,33,44,55,没一个都是其中的数组元素
数组长度 元素的总个数
数组下标 从0开始,第一个是0.第二个是1,第三个是2……以此类推,上面33的元素下标值就是2
数组的应用场景
获取数组长度
获取元素长度
遍历元素
元素切片
元素替换
元素删除
数组定义方法
方法一
数组名=(value0 value1 value2…)
[root@1centos /]# fs1=(1 3 5 7 9)
[root@1centos /]# echo ${fs1[*]}
1 3 5 7 9
方法二
数组名=([0]=value [1]=value [2]=value)
这个方法可以自定义下标
[root@1centos /]# fs2=([0]=2 [1]=4 [2]=6 [3]=8)
[root@1centos /]# echo ${fs2[*]}
2 4 6 8
方法三
先定义一个列表,再定义数组
列表名=“value0 value1 value2…”
数组名=($列表名)
[root@1centos hanshu]# liebiao="1 3 5 7 9 11 13 15 17 19"
[root@1centos hanshu]# shuzu=($liebiao)
[root@1centos hanshu]# echo ${shuzu[*]}
1 3 5 7 9 11 13 15 17 19
方法四
逐个增加
数组名[0]=“value1”
数组名[1]=“value2”
数组名[2]=“value3”
[root@1centos hanshu]# SHUZU[0]="0"
[root@1centos hanshu]# SHUZU[1]="2"
[root@1centos hanshu]# SHUZU[2]="4"
[root@1centos hanshu]# SHUZU[3]="6"
[root@1centos hanshu]# echo ${SHUZU[*]}
0 2 4 6
数组的使用
赋值数组 1-20
[root@1centos hanshu]# vim fuzhi.sh
#!/bin/bash
for ((i=0;i<20;i++))
do
shu[$i]=`expr $i + 1`
done
echo ${shu[*]}
[root@1centos hanshu]# source fuzhi.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
[root@1centos hanshu]# echo ${shu[*]}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
修改数组其中的元素
[root@1centos hanshu]# xiugai=(1 2 3 4 5 6 7 8 9)
[root@1centos hanshu]# vim xiugai.sh
#!/bin/bash
echo "当前数组为:"${xiugai[*]}
echo "当您输入的位置和数字都为空时,退出修改"
read -p "你想修改第几个数字:" a
if [ -n "$a" ];then
read -p "将数字修改为:" b
y=`expr "$a" - 1`
xiugai[$y]=$b
echo "数组修改为:" ${xiugai[*]}
while [ -n "$a" ];do
read -p "你想修改第几个数字:" a
read -p "将数字修改为:" b
if [ -n "$b" ];then
z=`expr "$a" - 1`
xiugai[$z]=$b
echo "数组修改为:" ${xiugai[*]}
else
echo "数组为:" ${xiugai[*]}
fi
done
else
echo "数组为:" ${xiugai[*]}
fi
进行测试
[root@1centos hanshu]# source xiugai.sh
当前数组为:1 2 3 4 5 6 7 8 9
当您输入的位置和数字都为空时,退出修改
你想修改第几个数字:1
将数字修改为:0
数组修改为: 0 2 3 4 5 6 7 8 9
你想修改第几个数字:2
将数字修改为:3
数组修改为: 0 3 3 4 5 6 7 8 9
你想修改第几个数字:
将数字修改为:
数组为: 0 3 3 4 5 6 7 8 9
[root@1centos hanshu]#
数组的操作
数组切片
${数组名[*]:起始下标:长度}
[root@1centos hanshu]# echo ${xiugai[*]}
1 3 3 4 5 6 7 8 9
[root@1centos hanshu]# echo ${xiugai[*]:3:5}
4 5 6 7 8
数组替换
${数组名[*]/查找的字符/替换成这个}
[root@1centos hanshu]# echo ${xiugai[*]}
1 3 3 4 5 6 7 8 9
[root@1centos hanshu]# echo ${xiugai[*]:3:5}
4 5 6 7 8
[root@1centos hanshu]# echo ${xiugai[*]/3/0}
1 0 0 4 5 6 7 8 9
[root@1centos hanshu]# echo ${xiugai[*]}
1 3 3 4 5 6 7 8 9
可以看到上面这个替换并不能修改数组的值,那么想修改怎么办呢
可以再次赋值
[root@1centos hanshu]# xiugai=(11 22 33 44 55 66)
[root@1centos hanshu]# echo ${xiugai[*]}
11 22 33 44 55 66
[root@1centos hanshu]# echo ${xiugai[*]/22/88}
11 88 33 44 55 66
[root@1centos hanshu]# echo ${xiugai[*]}
11 22 33 44 55 66
[root@1centos hanshu]# xiugai=(${xiugai[*]/22/88})
[root@1centos hanshu]# echo ${xiugai[*]}
11 88 33 44 55 66
数组删除
删除整条数组 : unset 数组名
删除数组里的一个元素: unset 数组名[下标位置]
[root@1centos hanshu]# unset xiugai
[root@1centos hanshu]# echo ${xiugai[*]}
[root@1centos hanshu]# xiugai=(11 22 33 44 55 66)
[root@1centos hanshu]# unset xiugai[2]
[root@1centos hanshu]# echo ${xiugai[*]}
11 22 44 55 66