这篇主要记录下数组的使用,相比来说用得比较少,同样的根据阅读<<跟老男孩学Linux运维:Shell编程实战>>所记录

系列笔记传送门:

  • Linux运维:shell脚本(1)
  • Linux运维:shell脚本(2)
  • Linux运维:shell脚本(3)
  • Linux运维:shell脚本(4)

shell数组

Shell数组是一个元素的集合,它把有限个元素用一个名字来命名,然后用编号对所有元素进行区分。这个名字就是数组名,用于区分不同内容的编号就叫数组的下标,组成数组的各个元素就是数组的元素,也叫下标变量。

1、数组的常用操作

1.1 数组的定义

数组的定义有多种方法:

  • 1、用小括号将变量值括起来赋值给变量,每个变量值之间用空格隔开。
  • 2、用小括号将变量值括起来,同时采用键值对的形式赋值。
  • 3、通过分别定义数组变量的方法定义。
  • 4、动态定义数组变量,并使用命令的输出结果作为数组内容。

方法1示例:

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

方法2示例:

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

方法3示例:

[root@moli_linux1 ~]# array[0]=a;array[1]=b;array[2]=c[root@moli_linux1 ~]# echo ${array[*]}a b c[root@moli_linux1 ~]# 

方法4示例:

[root@moli_linux1 test]# touch {1..3}.txt[root@moli_linux1 test]# ls1.txt  2.txt  3.txt[root@moli_linux1 test]# array=($(ls .))[root@moli_linux1 test]# echo ${array[*]}1.txt 2.txt 3.txt[root@moli_linux1 test]# 

1.2 数组打印输出

  • 打印数组里面的单个元素的值时,使用echo ${数组名[下标值]},如果没有指定数组的下标,那么默认数组的下标从0开始;
  • 打印整个数组的值可以使用echo ${数组名[*]}或者echo ${数组名[@]}
[root@moli_linux1 test]$ array=(one two three)[root@moli_linux1 test]$ echo ${array[0]}one[root@moli_linux1 test]$ echo ${array[1]}two[root@moli_linux1 test]$ echo ${array[*]}one two three[root@moli_linux1 test]$ echo ${array[@]}one two three

除了打印数组的值以外,还可以输出数组元素的个数,使用echo ${#数组名[*]}或者echo ${#数组名[@]}。这与变量字串的内容是一样,数组元素也是变量,不过是特殊的变量。

[root@moli_linux1 test]$ echo ${#array[*]}3[root@moli_linux1 test]$ echo ${#array[@]}3

1.3 数组的赋值

数组的赋值直接使用数组名[下标值]=value,当下标值不存在则添加一个新的数组元素,如果下标存在则会覆盖原来的值。

[root@moli_linux1 test]$ echo ${array[*]}one two three[root@moli_linux1 test]$ echo ${array[1]}two[root@moli_linux1 test]$ array[3]=four # 进行赋值[root@moli_linux1 test]$ echo ${array[*]}one two three four[root@moli_linux1 test]$ array[0]=1 # 下标已存在,覆盖下标为0的值[root@moli_linux1 test]$ echo ${array[*]}1 two three four

1.4 数组的删除

数组的本质还是变量,因此可以用unset 数组名[下标值]进行数组元素的删除,如果不指定下标值,而是使用unset 数组名则会删除整个数组。

[root@moli_linux1 test]$ echo ${array[*]}1 two three four[root@moli_linux1 test]$ unset array[0] # 删除下标为0的数组元素1[root@moli_linux1 test]$ echo ${array[*]} # 已删除two three four[root@moli_linux1 test]$ unset array # 删除整个数组[root@moli_linux1 test]$ echo ${array[*]}  # 显示为空,已被删除[root@moli_linux1 test]$ 

1.5 数组内容的截取与替换

数组的截取和替换与变量字串的截取和替换是一样的,直接例子吧!

[root@moli_linux1 test]# array=(1 2 3 4 5)[root@moli_linux1 test]# echo ${array[@]:1:3} # 截取下标为1到3的元素2 3 4[root@moli_linux1 test]# array=($(echo {a..z})) # 将命令的结果赋值给数组[root@moli_linux1 test]# 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@moli_linux1 test]# echo ${array[@]:1:3} # 截取下标为1到3的元素b c d[root@moli_linux1 test]# echo ${array[@]:0:2} a b[root@moli_linux1 test]# echo ${array[@]/a/A} # 把小写字母a替换为大写字母AA 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

2、shell数组开发实践

2.1 结合for循环打印数组元素

#!/bin/bashecho "====No.1====="array1=(1 2 3 4 5)for((i=0;i<${#array1[@]};i++))do    echo ${array1[i]}doneecho "====No.2===="array2=(1 2 3 4 5)for n in ${array2[*]}do    echo $ndone

执行结果:

2.2 打印下列句子中,单词字母数小于6的单词
句子:I am oldboy teacher welcome to oldboy training class
用数组方法实现:

#!/bin/basharray=(I am oldboy teacher welcome to oldboy training class)echo "====No,1===="for((i=0;i<${#array[@]};i++))do    if [ ${#array[$i]} -lt 6 ];then # 这里使用变量字串知识进行字符串长度统计        echo "${array[$i]}"    fidoneecho "====No.2===="for word in ${array[*]}do    if [ `expr length $word` -lt 6 ];then # 使用expr命令的length函数计算字符串长度        echo $word    fidone

执行结果:

[root@moli_linux1 script]# sh 13_4.sh ====No,1====Iamtoclass====No.2====Iamtoclass