shell学习笔记(数组)

简介:
bash只提供一维数组,并且没有限定数组的大小。类似与C语言,数组元素的下标由0开始编号。获取数组中的元素要利用下标。下标可以是整数或算术表达式,其值应大于或等于0。用户可以使用赋值语句对数组变量赋值。

     BASH中的数组有点怪,所以不常用到他们。然后如果需要用它们依然可以用,下标*和@指整个数组,${#array_name[*]}和${#array_name[@]}这两中特殊形式表示数组里元素的个数。不要把他们和似乎更合乎逻辑的${#array_name}搞混了,后者实际上是数组第一个元素的长度(等价于${#array_name[0]})。 其赋值有如下两种:

(1) name = (value1 ... valuen) 此时下标从0开始

(2) name[index] = value
1
2
3
4
[root@localhost ~]# echo ${#array[*]}    //使用${#array_name[*]}查看数组元素个数
2
[root@localhost ~]# echo ${#array[@]}    //使用${#array_name[@]}产看数组元素个数
2

数组操作(创建、删除、赋值):

1
2
3
4
5
6
7
8
9
[root@localhost ~]# array=()      //定义一个空数组
[root@localhost ~]# array=(one tow three 'Hello World')    //创建一个有4个元素的数组
[root@localhost ~]# array[0]=hello     //给数组赋值,
[root@localhost ~]# array[1]=world
                                         
[root@localhost ~]# echo ${array[1]}       //访问数组单个元素
world
[root@localhost ~]# unset array[1]      //删除数组中第一个元素
[root@localhost ~]# unset array          //删除整个数组

下面是一个快速脚本,它演示了bash 中数组管理的一些功能和缺陷:

1
2
3
4
5
6
7
8
9
10
11
[root@localhost shell] # cat array.sh
#!/bin/bash
example=(one tow three  'Hello World' )
example[4]=four
                                    
echo  "example[@] = ${example[@]}"
echo  "example array contains ${#example[@]} elements"
                                    
for  elt  in  "${example[@]}" do
     echo  "Element = $elt"
done

脚本输入如下:

1
2
3
4
5
6
7
8
[root@localhost shell]# ./array.sh
example[@] = one tow three Hello World four
example array contains 5 elements
Element = one
Element = tow
Element = three
Element = Hello World
Element = four

这个列子师傅很直观易懂,但只是因为我们已经把这个脚本构造得循规蹈矩了,人们一步小心就会犯错误,如果,用下面一句话替换for语句那行代码:

for elt in ${example[@]}; do
1
2
3
4
5
6
7
8
9
10
#!/bin/bash
example=(one tow three  'Hello World' )
example[4]=four
                           
echo  "example[@] = ${example[@]}"
echo  "example array contains ${#example[@]} elements"
                           
for  elt  in  ${example[@]};  do
     echo  "Element = $elt"
done

(在数组表达式外面没有引号引起来)也能行,但它却不是输出4个元素而是5个:one、tow、three、Hello、World、four,效果如下:

1
2
3
4
5
6
7
8
9
[root@localhost shell]# ./arrs.sh
example[@] = one tow three Hello World four
example array contains 5 elements
Element = one
Element = tow
Element = three
Element = Hello
Element = World
Element = four

本文转自1594cqb 51CTO博客,原文链接:http://blog.51cto.com/wolfchen/1206111,如需转载请自行联系原作者
相关文章
|
4月前
|
Shell Linux C++
Linux C/C++ 开发(学习笔记二):Shell脚本编程案例
Linux C/C++ 开发(学习笔记二):Shell脚本编程案例
44 0
|
5月前
|
Shell 索引
shell脚本入门到实战(四)- 数组
shell脚本入门到实战(四)- 数组
|
8月前
|
Shell 索引
shell编程之数组
shell编程之数组
50 0
|
1月前
|
人工智能 机器人 Shell
【shell】shell数组的操作(定义、索引、长度、获取、删除、修改、拼接)
【shell】shell数组的操作(定义、索引、长度、获取、删除、修改、拼接)
|
7月前
|
Shell Linux PHP
|
7月前
|
Shell Linux PHP
|
4月前
|
Java Shell Linux
Shell编程 学习笔记
Shell编程 学习笔记
69 1
|
5月前
|
存储 前端开发 JavaScript
shell(七)数组
数组可以存储多个数据,这个东西是很重要的,其他一些高级语言类似PHP、javascript等语言都是支持多维数组的。Shell编程只支持一维数组。 Shell编程的数组和PHP的数组类似,声明的时候不需要指定大小。也可以使用下标来访问数组中的元素。 Shell 数组用括号来表示,元素用"空格"符号分割开,语法格式如下:
39 0
|
5月前
|
Shell PHP
Shell 数组
Shell 数组
22 0
|
5月前
|
Shell Linux Perl
shell 学习笔记
shell 学习笔记
51 1