这篇主要记录下数组的使用,相比来讲用得比较少,一样的根据阅读<<跟老男孩学Linux运维:Shell编程实战>>所记录
系列笔记传送门:linux
Shell数组是一个元素的集合,它把有限个元素用一个名字来命名,而后用编号对全部元素进行区分。这个名字就是数组名,用于区分不一样内容的编号就叫数组的下标,组成数组的各个元素就是数组的元素,也叫下标变量。shell
1.1 数组的定义编程
数组的定义有多种方法:segmentfault
方法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示例:bash
[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]# ls 1.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 数组打印输出spa
单个元素
的值时,使用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 ${#数组名[@]}
。这与变量字串的内容是同样,数组元素也是变量,不过是特殊的变量。code
[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替换为大写字母A 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
2.1 结合for循环打印数组元素
#!/bin/bash echo "====No.1=====" array1=(1 2 3 4 5) for((i=0;i<${#array1[@]};i++)) do echo ${array1[i]} done echo "====No.2====" array2=(1 2 3 4 5) for n in ${array2[*]} do echo $n done
执行结果:
2.2 打印下列句子中,单词字母数小于6的单词
句子:I am oldboy teacher welcome to oldboy training class
用数组方法实现:
#!/bin/bash array=(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]}" fi done echo "====No.2====" for word in ${array[*]} do if [ `expr length $word` -lt 6 ];then # 使用expr命令的length函数计算字符串长度 echo $word fi done
执行结果:
[root@moli_linux1 script]# sh 13_4.sh ====No,1==== I am to class ====No.2==== I am to class