shell脚本编程学习之路-shell数组

1.数组的介绍

在开发shell脚本时,定义变量采用的形式为“a=1;b=2;c=3",变量多了再一个一个定义就比较麻烦,并且要是有多个不确定的变量内容也会难以进行变量控制,于是为了解决上面的问题数组诞生了。

数组就是有限个元素变量或数据用一个名字命名,然后用编号区分他们的变量的集合,这个名字称为数组,编号称为数组的下标。组成数组的多个变量称为数组的分量,也称为数组的元素,有时也称为下标变量。简单的说数组就是数据类型的元素按一定顺序排列的集合。

2.数组的定义与增删改查

2.1 数组的定义

方法1:用小括号将变量值括起来赋值给数组变量,每个变量值之间要用空格进行分隔。

array=(value1 value2 value3….)

示例如下:

[root@shellbiancheng ~]# array=(1 2 3)
[root@shellbiancheng ~]# echo ${array[*]}
1 2 3

方法2:动态地定义数组变量,并使用命令的输出结果作为数组的内容。

语法为:

array=($())或者array=(`命令`)

示例如下:

[root@shellbiancheng jiaobenlianxi]# array=($(ls))
[root@shellbiancheng jiaobenlianxi]# echo ${array[1]}
 1.sh
[root@shellbiancheng jiaobenlianxi]# echo ${array[2]}
a.log
[root@shellbiancheng jiaobenlianxi]# echo ${array[3]}
 array01.sh
[root@shellbiancheng jiaobenlianxi]# echo ${array[*]}
1-100.sh 1.sh a.log array01.sh array02.sh beijingcolor.sh b.log break.sh case01.sh check.sh chkconfigoff.sh chkconfigon.sh color1.sh color.sh for1-1000.sh for1-100_3.sh for1-100.sh for1.sh for2.sh for3.sh for4.sh for5.sh hanshu01.sh hanshu02.sh huafei.sh menufruit.sh nginx.sh piliangxiugai1.sh piliangxiugai2.sh piliangxiugai3.sh suijiwnjian.sh sum1-100.sh test uptime.sh usage.sh user.sh while1.sh while2.sh while3.sh while4.sh while.sh zhuajiu.sh
[root@shellbiancheng jiaobenlianxi]# cat array03.sh
array=(
$(ls)
)

for((i=0;i<${#array[*]};i++))

do
echo ${array[i]}
done

2.2 数组的打印及输出

(1) 打印数组元素

语法:echo ${数组名[下标]}

[root@shellbiancheng ~]# array=(one two three)
[root@shellbiancheng ~]# echo ${array[0]}
one  打印单个数组元素,当未指定数组下标时,数组的下标从0开始
[root@shellbiancheng ~]# echo ${array[1]}
two
[root@shellbiancheng ~]# echo ${array[2]}
three
[root@shellbiancheng ~]# echo ${array[*]}  使用*或者@符号可以得到整个数组的内容。
one two three
[root@shellbiancheng ~]# echo ${array[@]}
one two three

(2) 打印数组元素的个数即获取数组的长度

语法:echo ${#array[]}

[root@shellbiancheng ~]# echo ${array[*]}  
one two three    
[root@shellbiancheng ~]# echo ${#array[*]}
3
[root@shellbiancheng ~]# echo ${#array[@]}
3

(3)数组赋值(了解)

语法:数组名[下标]=内容

如果下标不存在,则自动添加一个新的数组元素,如果存在则覆盖原来的值。

[root@shellbiancheng jiaobenlianxi]# array=(1 2 3)
[root@shellbiancheng jiaobenlianxi]# echo ${array[*]}
1 2 3
[root@shellbiancheng jiaobenlianxi]# array[3]=4  增加下标为3的数组
[root@shellbiancheng jiaobenlianxi]# echo ${array[*]}
1 2 3 4
[root@shellbiancheng ~]# array[1]=two 修改数组元素
[root@shellbiancheng ~]# echo ${array[*]} 
1 two 3 4

(4) 删除数组(了解)

语法:unset 数组名[下标]

清除相应的数组元素,如果不带下标,则表示清除整个数组的所有值。

[root@shellbiancheng ~]# array=(1 2 3 4) 
[root@shellbiancheng ~]# unset array[1] 删除下标为1的数组
[root@shellbiancheng ~]# echo ${array[*]}
1 3 4
[root@shellbiancheng ~]# unset array  删除整个数组
[root@shellbiancheng ~]# echo ${array[*]}

[root@shellbiancheng ~]# 

(5) 数组内容的截取和替换(了解)

截取:

[root@shellbiancheng jiaobenlianxi]# array=(1 2 3 4 5)
[root@shellbiancheng jiaobenlianxi]# echo ${array[*]:1:3}
2 3 4
[root@shellbiancheng jiaobenlianxi]# echo ${array[*]:3:4}
4 5

替换:

[root@shellbiancheng jiaobenlianxi]# echo ${array[*]/3/4} 把数组中的3替换成4临时替换,原数组未做改变
1 2 4 4 5
[root@shellbiancheng jiaobenlianxi]# array1=(${array[*]/3/4})
[root@shellbiancheng jiaobenlianxi]# echo ${array1[*]}
1 2 4 4 5

2.3 数组脚本开发实践

(1)范例1:使用循环批量输出数组元素

方法一:使用for循环语句打印数组元素

[root@shellbiancheng jiaobenlianxi]# cat array01.sh 
#!/bin/bash
array=(
1 2 3 4
)
for ip in ${array[*]}
do
    echo $ip
    sleep 2
done

方法二:使用c语言型的for循环打印数组元素

[root@shellbiancheng jiaobenlianxi]# cat array02.sh 
#!/bin/bash
array=(
1 2 3 4
)
for((i=0;i<${#array[*]};i++))
do
    echo ${array[i]}
    sleep 2
done

方法三:使用while循环语句打印数组元素

[root@shellbiancheng jiaobenlianxi]# cat array03.sh 
#!/bin/bash
array=(
1 2 3 4
)
while((i<${#array[*]}))
do
echo ${array[i]}
((i++))
done

3.Shell数组知识小结

(1)定义:

静态数组:array=(1 2 3) 空格隔开

动态数组:array=($(ls))

给数组赋值:array[3]=4

(2)打印

打印所有元素:${array[*]}或${array[@]}

打印数组长度:${#array[@]}或${#array[*]}

打印单个元素:${array[i]} i是数组下标

(3) 循环打印的常用基本语法

[root@shellbiancheng jiaobenlianxi]# cat array02.sh 
#!/bin/bash
array=(
1 2 3 4
)
for((i=0;i<${#array[*]};i++))
do
echo ${array[i]}
sleep 2
done

转载于:https://blog.51cto.com/10642812/2103603

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值