设为首页 加入收藏

TOP

Shell编程-10-Shell中的数组(一)
2019-09-03 00:54:03 】 浏览:22
Tags:Shell 编程 -10-Shell

? ? 如果大家有其他语言的基础或经验,就很快能明白数组了。简单来说,数组就某一种相同类型的元素组合,而后通过下标对其进行访问各元素。

数组基础

基础语法

  • 第一种形式
 array=(value1 value2 value3 ...)
  • 第二种形式
array=([0]=value1 [1]=value2 [2]=value3 ...)

在以上形式中中括号代表的数组的下标索引,等号后面为其对应的值。

  • 第三种形式
array[0]=value1;array[1]=value2;array[2]=value3
  • 第四种形式:动态数组
array=($(命令))

array=(`命令`)

在日常使用中推荐大家使用第一种形式和第四种形式

数组示例

  • 第一种形式
[root@localhost Test]# array=(1 3 5)
[root@localhost Test]# echo ${array[*]}
1 3 5
  • 第二种形式
[root@localhost Test]# array=([0]=1 [1]=3 [2]=5)
[root@localhost Test]# echo ${array[*]}
1 3 5
  • 第三种形式
[root@localhost Test]# array[0]=1;array[1]=3;array[2]=5
[root@localhost Test]# echo ${array[*]}
1 3 5
  • 第四种形式:动态数组
[root@localhost Test]# array=($(ls))
[root@localhost Test]# echo ${array[*]}
break.sh caseif.sh case.sh compareNum.sh

[root@localhost Test]# array=(`ls`)
[root@localhost Test]# echo ${array[*]}
break.sh caseif.sh case.sh compareNum.sh

数组输出

? 数组输出通过采用下标索引的形式进行输出,输出数组的格式如下所示:

${ 数组名称 [下标索引] }

**如未指定数组下标,则默认下标索引从0开始;如果使用*或@则代表输出整个数组元素**。

[root@localhost Test]# array=(1 3 5)
[root@localhost Test]# echo ${array[2]} # 输出第3个元素
5
[root@localhost Test]# echo ${array}    # 未指定下标输出第1个元素
1
[root@localhost Test]# echo ${array[*]} #  使用 * 输出整个数组元素
1 3 5
[root@localhost Test]# echo ${array[@]} #  使用 @ 输出整个数组元素
1 3 5

数组长度

? 输出数长度常用格式如下所示:

${ #数组名称 [*] }
或
${ #数组名称 [@] }

示例如下所示:

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

数组赋值

? 可通过数组名[下标索引]对数组进行赋值,其格式如下所示:

数组名[下标索引]=value
  • 如果下标不存在,则自动向数组添加一个新的元素值
  • 如果下标存在,则会覆盖先前的元素值

示例如下所示:

[root@localhost Test]# array=(1 3 5)
[root@localhost Test]# array[2]=100  # 下标存在,覆盖之前的元素
[root@localhost Test]# array[5]=888  # 下标不存在,则自动添加一个新的元素值
[root@localhost Test]# echo ${array[*]}
1 3 100 888
[root@localhost Test]# echo ${#array[@]}
4

数组删除

? 数组本质上还是一种变量,因此通过使用unset进行清除数组元素。其语法格式如下所示:

unset 数组名称[下标索引]

如果不带下标索引,则表示清除整个数组,需要注意与输出数组元素不带下标索引的区别

示例如下所示:

[root@localhost Test]# array=(1 3 5 7 9)
[root@localhost Test]# echo ${array[@]}
1 3 5 7 9
[root@localhost Test]# unset array[1] # 清除数组中第2个元素
[root@localhost Test]# echo ${array[@]}
1 5 7 9
[root@localhost Test]# unset array    # 清除整个数组
[root@localhost Test]# echo ${array[@]}
                                      # 清除数组后,输出为空

数组删除扩展方法

[root@localhost Test]# b=(a b c d e f g h i)
[root@localhost Test]# echo ${b[*]}
a b c d e f g h i
[root@localhost Test]# echo ${b[*]#a*}  # 从左边开始匹配最短的数组元素并删除
b c d e f g h i
[root@localhost Test]# echo ${b[*]##b*} # 从左边开始匹配最长的数组元素并删除
a c d e f g h i
[root@localhost Test]# echo ${b[*]%i*}  # 从右边开始匹配最短的数组元素并删除
a b c d e f g h
[root@localhost Test]# echo ${b[*]%%g*} # 从右边开始匹配最长的数组元素并删除
a b c d e f h i
[root@localhost Test]# echo ${b[*]}     # 该删除并不影响原数组的内容
a b c d e f g h i

数组从某种意义上来说,就是一种特殊的字符变量,因此也适合前面讲的子符串处理的功能。

数组截取与替换

? 数组的截取示例如下所示:

[root@localhost Test]# a=($(echo {0..9}))
[root@localhost Test]# echo ${a[*]}
0 1 2 3 4 5 6 7 8 9
[root@localhost Test]# echo ${a[*]:1:3} # 截取下标索引1~3的元素
1 2 3
[root@localhost Test]# echo ${a[*]:0:2}# 截取下标索引0~2的元素
0 1

? 数组的替换格式如下所示:

${ 数组名[*/@]/查找字符/替换字符 }

该替换操作并不会改变原先的数组内容

? 数组的替换示例如下所示:

[root@localhost Test]# echo ${a[*]}
0 1 2 3 4 5 6 7 8 9
[root@localhost Test]# echo ${a[*]/4/456} # 将4替换为456
0 1 2 3 456 5 6 7 8 9
[root@localhost Test]# echo ${a[*]}
0 1 2 3 4 5 6 7 8 9

数组示例

1、使用循环打印数组元素

[root@localhost Test]# cat array.sh
#!/bin/bash
arr
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇vim常用命令总结 (转) 下一篇chmod chown llinux文件及目录的..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目