Shell编程数组array详解
数组:存储相同数据类型的数据 Python中的列表:可以存储不同数据类型的数组 Shell数组:shell数组定义以及获取数组元素 和其他编程语言(C C++ java ...)一样,shell也支持数组,数组(Array)是若干数据的集合,其中的每一份数据都成为元素 Shell并没有限制数组的大小,理论上可以存放无限量的数据,和C++,java,C#等类似 Shell数组元素的下标也是从0开始计数。 获取数组中的元素要使用下标[],下标可以是一个整数,也可以时一个结果为整数的表达式,当然,下标必须大于等于0 遗憾的是,常用的Bash Shell只支持一维数组,不支持多维数组
Shell数组的定义:
Shell中用括号()来表示数组,数组元素之间用空格来分隔,由此,定义数组的一般形式为:
Array_name=(ele1 ele2 ele3 …… elen)
#注意,赋值号=两边不能由空格,必须紧挨着数组名和数组元素
定义数组:
第一种:直接显示变量
用小括号将变量括起来赋值给数组变量,每个变量值之间要用空格进行分隔(此为常用定义方法,需要重点掌握)
[root@localhost 数组]# array=(1 2 3)
[root@localhost 数组]# echo $array
1
[root@localhost 数组]# echo ${array[*]}
1 2 3
第二种:用小括号将元素括起来,采用键值对的形式不用默认的下标(类似于python函数参数中的位置参数)
[root@localhost 数组]# array=([1]=one [2]=two [3]=three)
[root@localhost 数组]# echo ${array[*]}
one two three
[root@localhost 数组]# echo ${array[1]}
one
[root@localhost 数组]# echo ${array[2]}
two
[root@localhost 数组]# echo ${array[3])
> ^C
[root@localhost 数组]# echo ${array[3])
> echo ${array[3])^C
[root@localhost 数组]# echo ${array[3]}
three
[root@localhost 数组]# echo ${array[0]}
[root@localhost 数组]#
第三种:通过分别定义数组的变量的方式
注意这种方式如果定义的数组是之前存在的,则只在原来数组的基础上进行添加和更改
定义存在的数组:
[root@localhost 数组]# echo ${array[*]}
a b c three #之前array数组的第4个元素是three
[root@localhost 数组]# echo ${array[0]}
a
[root@localhost 数组]# echo ${array[1]}
b
[root@localhost 数组]# echo ${array[2]}
c
[root@localhost 数组]#
定义不存在的数组:
[root@localhost 数组]# echo ${haha[*]}
q f h
第四种:动态的定义数组
并使用命令的输出结果作为数组的内容
[root@localhost 数组]# files=($(ls /array))
[root@localhost 数组]# echo ${files[*]}
1.txt 2.txt 3.txt
[root@localhost 数组]# array=($(ls /array))
[root@localhost 数组]# echo ${array[*]}
1.txt 2.txt 3.txt
数组的输出和打印
以 小括号+键值对 定义数组时,可以不用定义下标为0的数组
(默认的方法是从下标为0的元素开始定义的)
* 和 @都可以显示所有的元素
[root@localhost 数组]# echo ${c[*]}
one two three
[root@localhost 数组]# echo ${c[@]}
one two three
打印数组元素的个数(长度) ${#c[@]}
[root@localhost 数组]# echo ${#c[@]}
3
[root@localhost 数组]# echo ${#c[*]}
3
删除数组(按下标删除数组元素,直接清空数组): unset
因为数组本质上还是变量,因此可以通过“unset数组(下标)”清除相应的数组元素,如果不带下标,则表示清除整个数组的所有数据
数组的赋值:如果下标不存在,则自动添加一个新的元素。
如果下标存在。则覆盖原来的值
[root@localhost 数组]# array=(one two three)
[root@localhost 数组]# echo ${array[*]}
one two three
[root@localhost 数组]# array[3]=four
[root@localhost 数组]# echo ${array[*]}
one two three four
[root@localhost 数组]# array[0]=westos
[root@localhost 数组]# echo ${array[*]}
westos two three four
[root@localhost 数组]# unset array[1]
[root@localhost 数组]# echo ${array[*]}
westos three four
[root@localhost 数组]# unset array
[root@localhost 数组]# echo ${array[*]}
数组的截取和替换
${array[@]:0}
${array[@]:1} #抽取第一个元素到结束的数组
${array[@]:3} #抽取第三个元素到结束的数组
${array[@]:3} #抽取从第0个元素开始的两个元素
打印(截取和命令快速生成元素):
[root@localhost 数组]# array=(1 2 3 4 5)
[root@localhost 数组]# echo ${array[@]:1:3} #
截取下标为1到3的数组元素
2 3 4
[root@localhost 数组]# array=($(echo {a..z}))
[root@localhost 数组]# echo ${array[*]}
a b c d e f g h i j k l m n o p q r s t u v w x y z
[root@localhost 数组]# echo ${array[*]:1:3}
b c d
[root@localhost 数组]# echo ${array[*]:0:2}#抽取从第0个元素开始的两个元素
a b
替换(原数组没有改变):
[root@localhost 数组]# array=(1 2 3 1 1)
[root@localhost 数组]# echo ${array[@]/1/b}
#把数组中的1替换成b,原数组未改变,和sed很像
b 2 3 b b
[root@localhost 数组]# echo ${array[@]}
1 2 3 1 1
使用循环批量输出数组的元素
[root@localhost 数组]# cat 01.sh #
!/bin/bash #使用循环批量输出数组的元素 array=(1 2 3 4 5) for ((i=0;i<${#array[*]};i++)) #从数组的第一个下标0开始,循环数组所有下标 do echo ${array[i]} #打印数组元素 done
[root@localhost 数组]# sh 01.sh
1
2
3
4
5
通过竖向列举方法定义数组元素并批量打印
[root@localhost 数组]# cat 02.sh
#!/bin/bash #通过竖向列举方法定义数组元素并批量打印 array=( westos linux redhat python ) for ((i=0;i<${#array[*]};i++)) do echo "This is num $i,then content is ${array[$i]}" done echo --------------------------------- echo "array len:&${#array[*]}"
[root@localhost 数组]# sh 02.sh
This is num 0,then content is westos
This is num 1,then content is linux
This is num 2,then content is redhat
This is num 3,then content is python
---------------------------------
array len:&4
将命令结果作为数组元素取打印
[root@localhost 数组]# sh 03.sh
This is NO.0,filename is 1.txt
This is NO.1,filename is 2.txt
This is NO.2,filename is 3.txt
[root@localhost 数组]# cat 03.sh
#!/bin/bash
dir=($(ls /array))
for ((i=0;i<${#dir[*]};i++))
do
echo "This is NO.$i,filename is ${dir[$i]}"
done
Shell编程数组array详解
猜你喜欢
转载自blog.csdn.net/qq_43309149/article/details/104336833
今日推荐
周排行