[关闭]
@yanglt7 2018-11-02T06:46:22.000000Z 字数 4150 阅读 834

《Shell 编程》12_Shell 数组

Shell


12.1 Shell 数组的定义与增删改查

12.1.1 Shell 数组的定义

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

  1. array=(value1 value2 value3 ...)

2)用小括号将变量值括起来,同时采用 key-value 键值对的形式赋值。

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

3)通过分别定义数组变量的方法来定义。

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

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

  1. array=($(命令))
  2. array=(`命令`)

12.1.2 Shell 数组的打印及输出

1)打印数组元素

  1. [root@web001 scripts]# array=(one two three)
  2. [root@web001 scripts]# echo ${array[0]}
  3. one
  4. [root@web001 scripts]# echo ${array[1]}
  5. two
  6. [root@web001 scripts]# echo ${array[2]}
  7. three
  8. [root@web001 scripts]# echo ${array[*]}
  9. one two three
  10. [root@web001 scripts]# echo ${array[@]}
  11. one two three

2)打印数组元素个数

  1. [root@web001 scripts]# echo ${array[*]}
  2. one two three
  3. [root@web001 scripts]# echo ${#array[*]}
  4. 3
  5. [root@web001 scripts]# echo ${array[@]}
  6. one two three
  7. [root@web001 scripts]# echo ${#array[@]}
  8. 3

3)数组赋值

  1. [root@web001 scripts]# echo ${array[*]}
  2. one two three
  3. [root@web001 scripts]# array[3]=four
  4. [root@web001 scripts]# echo ${array[@]}
  5. one two three four
  6. [root@web001 scripts]# array[0]=ylt
  7. [root@web001 scripts]# echo ${array[*]}
  8. ylt two three four

4)数组的删除

  1. [root@web001 scripts]# echo ${array[*]}
  2. ylt two three four
  3. [root@web001 scripts]# unset array[1]
  4. [root@web001 scripts]# echo ${array[*]}
  5. ylt three four
  6. [root@web001 scripts]# unset array
  7. [root@web001 scripts]# echo ${array[*]}

5)数组内容的截取

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

替换数组元素部分内容:

  1. [root@web001 scripts]# array=(1 2 3 1 1)
  2. [root@web001 scripts]# echo ${array[@]/1/b}
  3. b 2 3 b b
  4. [root@web001 scripts]# echo ${array[@]}
  5. 1 2 3 1 1

删除数组元素部分内容:

  1. [root@web001 scripts]# array=(one two three four five)
  2. [root@web001 scripts]# echo ${array[@]}
  3. one two three four five
  4. [root@web001 scripts]# echo ${array[@]#o*} #<== 从左边开始匹配最短的数组元素,并删除
  5. ne two three four five
  6. [root@web001 scripts]# echo ${array[@]##o*} #<== 从左边开始匹配最长的数组元素,并删除
  7. two three four five
  8. [root@web001 scripts]# echo ${array[@]%f*} #<== 从右边开始匹配最短的数组元素,并删除
  9. one two three
  10. [root@web001 scripts]# echo ${array[@]%%f*} #<== 从右边开始匹配最长的数组元素,并删除
  11. one two three

12.2 Shell 数组示例

例 12-1 使用循环批量输出数组的元素

方法 1:通过 C 语言型的 for 循环语句打印数组元素。

  1. #!/bin/bash
  2. array=(1 2 3 4 5)
  3. for ((i=0;i<${#array[@]};i++)
  4. do
  5. echo ${array[i]}
  6. done

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

  1. #!/bin/bash
  2. array=(1 2 3 4 5)
  3. for n in ${array[*]}
  4. do
  5. echo $n
  6. done

方法 3:使用 while 循环语句打印元素。

  1. array=(1 2 3 4 5)
  2. i=0
  3. while ((i<${#array[*]}))
  4. do
  5. echo ${array[i]}
  6. ((i++))
  7. done

例 12-2 通过竖向列举法定义数组元素并批量打印、

  1. #!/bin/bash
  2. array=(
  3. ylt
  4. yyy
  5. lll
  6. ttt
  7. )
  8. for((i=0; i<${#array[@]}; i++))
  9. do
  10. echo "This is num $i, then content is ${array[$i]}"
  11. done

例 12-3 将命令结果作为数组元素

  1. [root@web001 scripts]# mkdir -p /array
  2. [root@web001 scripts]# touch /array/array{1..3}
  3. [root@web001 scripts]# ll /array/
  4. total 0
  5. -rw-r--r-- 1 root root 0 Nov 1 06:28 array1
  6. -rw-r--r-- 1 root root 0 Nov 1 06:28 array2
  7. -rw-r--r-- 1 root root 0 Nov 1 06:28 array3
  1. [root@web001 scripts]# cat 266.sh
  2. #!/bin/bash
  3. dir=($(ls /array))
  4. for ((i=0; i<${#dir[@]}; i++))
  5. do
  6. echo "This is No.$i, filename is ${dir[$i]}"
  7. done
  8. [root@web001 scripts]# sh 266.sh
  9. This is No.0, filename is array1
  10. This is No.1, filename is array2
  11. This is No.2, filename is array3

12.3 Shell 数组的重要命令

1)定义命令

静态数组:

  1. array=(1 2 3)

动态数组:

  1. array=($(ls))

为数组赋值:

  1. array[3]=4

2)打印命令

打印所有元素:

  1. ${array[@]} ${array[*]}

打印数组长度:

  1. ${#array[@]} ${array[*]}

打印单个元素:

  1. ${array[i]} #<== i 是数组下标

3)循环打印常用语法

  1. #!/bin/bash
  2. for ((i=0;i<${#array[@]}; i++))
  3. do
  4. echo "${array[$i]}"
  5. done
  6. #<== C 语言 for 循环语法
  7. #!/bin/bash
  8. for n in ${arary[@]}
  9. do
  10. echo "$n"
  11. done
  12. #<== 普通 for 循环语法

例 12-5 利用 bash for 循环打印下面这句话中字母数不大于 6 的单词。

  1. I am learning the shell programming language

计算变量内容长度的方法:

  1. char=I am learning the shell programming language
  2. echo $char|wc -L
  3. echo ${#char}
  4. expr length $char
  5. echo $char|awk '{print length($0)}'

方法 1:通过数组方法来实现

  1. arr=(I am learning the shell programming language)
  2. for ((i=0;i<${#arr[@]}; i++))
  3. do
  4. #if [ ${#arr[$i]} -le 6 ];then
  5. echo "${arr[$i]}"
  6. fi
  7. done
  8. echo ---------------------------------------------
  9. for word in ${arr[@]}
  10. do
  11. #if [ `expr length $word` -le 6 ];then
  12. if [ `echo $word|wc -L` -le 6 ];then
  13. echo $word
  14. fi
  15. done

方法 2:通过 awk 循环实现

  1. [root@web001 scripts]# chars="I am learning the shell programming language"
  2. [root@web001 scripts]# echo $chars|awk '{for(i=1;i<=NF;i++) if(length($i)<=6) print $i}'
  3. I
  4. am
  5. the
  6. shell
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注