Shell入门-Shell数组

Shell数组

通常在开发Shell脚本时,定义变量采用的形式为“a=1; b=2; c=3”,可如果有多个变量呢?这时再逐个地定义就会很费劲,并且要是有多个不确定的变量内容,也会难以进行变量定义,此外,快速读取不同变量的值也是一件很痛苦的事情,于是数组就诞生了,它就是为了解决上述问题而出现的

Shell数组的定义

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

语法:

array=(arg1 arg2 arg3)

实践:

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

方法二:用小括号将变量值括起来,同时采用键值对的形式赋值

语法:

array=([1]=one [2]=two [3]=three)

实践:

[root@localhost shell]# array=([1]=one [2]=two [3]=three)
[root@localhost shell]# echo ${array[*]}
one two three
[root@localhost shell]# 

方法三:通过分别定义数组变量的方法来定义

语法:

array[0]=a;array[1]=b;array[2]=c

实践:

[root@localhost shell]# array[0]=a;array[1]=b;array[2]=c
[root@localhost shell]# echo ${array[*]}
a b c three
[root@localhost shell]# 

方法四:动态定义数组变量,使用命令的输出内容作为数组内容

语法:

array=($(命令))

array=(`命令`)

实践:

[root@localhost shell]# ls stu*
stu_102999_1.jpg  stu_102999_2.jpg  stu_102999_3.jpg  stu_102999_4.jpg
[root@localhost shell]# array=($(ls stu*))
[root@localhost shell]# echo ${array[*]}
stu_102999_1.jpg stu_102999_2.jpg stu_102999_3.jpg stu_102999_4.jpg
[root@localhost shell]# 

Shell数组的打印

1.打印数组元素

[root@localhost shell]# array=(one two three)
[root@localhost shell]# echo ${array[0]}
one
[root@localhost shell]# echo ${array[1]}
two
[root@localhost shell]# echo ${array[2]}
three
[root@localhost shell]# echo ${array[*]}
one two three
[root@localhost shell]# echo ${array[@]}
one two three
[root@localhost shell]# 

2.打印数组元素的个数

[root@localhost shell]# echo ${#array[*]}
3
[root@localhost shell]# echo ${#array[@]}
3
[root@localhost shell]# 

3.数组赋值

可直接通过“数组名[下标]”对数组进行引用赋值,如果下标不存在,则自动添加一个新的数组元素,如果下标存在,则覆盖原来的值。

[root@localhost shell]# array[2]=four
[root@localhost shell]# echo ${array[@]}
one two four

4.数组的删除

因为数组本质上还是变量,因此可通过“unset数组[下标]”清除相应的数组元素,如果不带下标,则表示清除整个数组的所有数据。

[root@localhost shell]# unset array[2]
[root@localhost shell]# echo ${array[@]}
one two
[root@localhost shell]# unset array
[root@localhost shell]# echo ${array[@]}

[root@localhost shell]# 

5.数组内容的截取和替换

这里和前文变量子串的替换是一样的,因为数组是特殊的变量。

echo ${array[@]:0:2}表示从array数组的下标0开始获取2个元素

[root@localhost shell]# array=(1 2 3 4 5)
[root@localhost shell]# echo ${array[@]:1:3}
2 3 4
[root@localhost shell]# array=($(echo {a..z}))
[root@localhost shell]# 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 shell]# echo ${array[@]:1:3}
b c d
[root@localhost shell]# echo ${array[@]:0:3}
a b c
[root@localhost shell]# echo ${array[@]:0:2}
a b
[root@localhost shell]# 

替换:

[root@localhost shell]# echo ${array[@]/a/1}
1 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 shell]# array=(one two three four five)
[root@localhost shell]# echo ${array[@]}
one two three four five
[root@localhost shell]# echo ${array[@]#o*}
ne two three four five <===从左边开始匹配最短的数组元素,并删除
[root@localhost shell]# echo ${array[@]##o*}
two three four five <===从左边开始匹配最长的数组元素,并删除
[root@localhost shell]# echo ${array[@]%f*}
one two three <===从右边开始匹配最短的数组元素,并删除
[root@localhost shell]# echo ${array[@]%%f*}
one two three <===从右边开始匹配最长的数组元素,并删除
[root@localhost shell]# 

提示:数组也是变量,因此也适合于前面讲解过的变量的子串处理的功能应用。数组的其他相关知识可通过man bash命令然后搜“Arrays”来了解。

Shell数组实践

使用循环批量输出数组的元素

方法一:通过c语言型的for循环语句打印数组元素

[root@localhost shell]# cat 13_1_1.sh 
#!/bin/bash
#***********************************************
#Author:        luotianhao
#Mail:          [email protected]
#Version:       1.0
#Date:          2021-03-16
#FileName:      13_1_1.sh
#Description:   This is a test script.
#***********************************************
array=(1 2 3 4 5)
for((i=0;i<${#array[*]};i++))
do
    echo ${array[i]}
done
[root@localhost shell]# sh 13_1_1.sh 
1
2
3
4
5

方法二:通过普通for循环语句打印数组元素

[root@localhost shell]# cat 13_1_2.sh 
#!/bin/bash
#***********************************************
#Author:        luotianhao
#Mail:          [email protected]
#Version:       1.0
#Date:          2021-03-16
#FileName:      13_1_2.sh
#Description:   This is a test script.
#***********************************************
array=(1 2 3 4 5)
for n in ${array[*]}
do
    echo $n
done
[root@localhost shell]# sh 13_1_2.sh 
1
2
3
4
5

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

[root@localhost shell]# cat 13_1_3.sh 
#!/bin/bash
#***********************************************
#Author:        luotianhao
#Mail:          [email protected]
#Version:       1.0
#Date:          2021-03-16
#FileName:      13_1_3.sh
#Description:   This is a test script.
#***********************************************
array=(1 2 3 4 5)
i=0
while (( i<${#array[*]} ))
do
    echo ${array[i]}
    ((i++))
done
[root@localhost shell]# sh 13_1_3.sh 
1
2
3
4
5

通过竖向列举法定义数组元素并批量打印。

[root@localhost shell]# sh 13_2_1.sh 
this is num 0,then content is oldboy
this is num 1,then content is oldgirl
this is num 2,then content is xiaoting
this is num 3,then content is bingbing
-----------------------------------
array len:4
[root@localhost shell]# cat 13_2_1.sh 
#!/bin/bash
#***********************************************
#Author:        luotianhao
#Mail:          [email protected]
#Version:       1.0
#Date:          2021-03-16
#FileName:      13_2_1.sh
#Description:   This is a test script.
#***********************************************
array=(
oldboy
oldgirl
xiaoting
bingbing
)

for (( i=0;i<${#array[*]};i++  ))
do
    echo "this is num $i,then content is ${array[$i]}"
done
echo -----------------------------------
echo "array len:${#array[*]}"

打印字母数不大于6的单词

利用bash for循环打印下面这句话中字母数不大于6的单词i am oldboy teacher welcome to oldboy training class

1.先把所有单词放入数组中,然后依次进行判断

array=(i am oldboy teacher welcome to oldboy training class)

2.计算变量内容的长度,常见有4种方法

[root@localhost shell]# char=oldboy
[root@localhost shell]# echo $char|wc -L
6
[root@localhost shell]# echo ${#char}
6
[root@localhost shell]# expr length $char
6
[root@localhost shell]# echo $char|awk '{print length ($0)}'
6
[root@localhost shell]# 

或者使用awk循环实现

[root@localhost shell]# array="i am oldboy teacher welcome to oldboy training class"
[root@localhost shell]# echo $array|awk '{for(i=1;i<=NF;i++) if(length($i)<=6) print $i}'
i
am
oldboy
to
oldboy
class
[root@localhost shell]# 

猜你喜欢

转载自blog.csdn.net/weixin_43169156/article/details/114883097