bash数组

来源:互联网 发布:淘宝低价高配主机 编辑:程序博客网 时间:2024/05/16 05:50

数组是包含多个相同或不同数据类型的集合,数组索引从零开始。本文有 15 个用 bash 操作数组的例子。

1. 声明数组,赋值

当有变量为如下格式的时候,Bash 会自动创建数组。

name[index]=value
  • name 为数组名称
  • index 为任意数字,或表达式的最终计算值大于等于零。可以显式声明数组通过 declare -a arrayname
$ cat arraymanip.sh#! /bin/bashUnix[0]='Debian'Unix[1]='Red hat'Unix[2]='Ubuntu'Unix[3]='Suse' echo ${Unix[1]} $./arraymanip.shRed hat

访问数组元素使用括号,如 ${name[index]}

2. 声明时初始化数组

通过指定元素列表来声明数组,就不用一个一个分别初始化数组元素的,用空格隔开在括号里。

Syntax:declare -a arrayname=(element1 element2 element3)

如果数组元素包含空格符,用引号括起来。

#! /bin/bash$cat arraymanip.shdeclare -a Unix=('Debian' 'Red hat' 'Red hat' 'Suse' 'Fedora');

declare -a 声明一个数组以及括号中的所有元素为数组元素。

3. 输出整个 Bash 数组

有多种不同的方法来输出整个数组。如果索引是 @ 或者 *,则引用数组所有元素。也可以使用循环,遍历数组中每个元素再输出。

echo ${Unix[@]} # Add the above echo statement into the arraymanip.sh#./t.shDebian Red hat Ubuntu Suse

如果引用数组元素,而不提供索引的话,就是引用数组的第一个元素,即索引为零的元素。

4. Bash 数组长度

可以使用特殊参数 $# 来获得数组长度。

${#arrayname[@]} gives you the length of the array.

$ cat arraymanip.shdeclare -a Unix=('Debian' 'Red hat' 'Suse' 'Fedora');echo ${#Unix[@]} #Number of elements in the arrayecho ${#Unix}  #Number of characters in the first element of the array.i.e Debian$./arraymanip.sh46

5. 数组第 n 个元素的长度

${#arrayname[n]} 为数组第 n 个元素的长度。

$cat arraymanip.sh#! /bin/bash Unix[0]='Debian'Unix[1]='Red hat'Unix[2]='Ubuntu'Unix[3]='Suse' echo ${#Unix[3]} # length of the element located at index 3 i.e Suse $./arraymanip.sh4

6. 指定偏移和长度输出数组

下面的例子是输出 2 个数组元素,从第索引为3的元素开始。

$cat arraymanip.shUnix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');echo ${Unix[@]:3:2} $./arraymanip.shSuse Fedora

上面的例子返回里索引为 3 和 4 的元素。索引永远由零开始。

7. 根据偏移和长度,输出数组指定元素的一部分

输出一个数组元素的前四个字符。如例,Ubuntu 是数组索引为 3 的元素,可以指定偏移和长度来获取数组指定元素的一部分。

$cat arraymanip.sh#! /bin/bash Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');echo ${Unix[2]:0:4} ./arraymanip.shUbun

上面的例子取出索引为 2 的数组元素的前 4 个字符。

8. 搜索和替换数组元素

下面的例子,在数组元素中搜索 Ubuntu,替换为 SCO Unix

$cat arraymanip.sh#!/bin/bashUnix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux')echo ${Unix[@]/Ubuntu/SCO Unix} $./arraymanip.shDebian Red hat SCO Unix Suse Fedora UTS OpenLinux

但是,这个例子并没有永久替换数组内容。

9. 添加元素到已存在的 Bash Array

下面的例子展示了如何添加元素到已存在数组。

$cat arraymanip.shUnix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');Unix=("${Unix[@]}" "AIX" "HP-UX")echo ${Unix[7]} $./arraymanip.shAIX

‘AIX’ and ‘HP-UX’ are added in 7th and 8th index respectively.
在数组 Unix 中,元素 ‘AIX’ 和 ‘HP-UX’ 添加到第 7 和 第 8 位。

10. 删除数组元素

unset 用于移除数组元素,unset 与给数组元素赋值为 null 是一样的效果。

$cat arraymanip.sh#!/bin/bashUnix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux')unset Unix[3]echo ${Unix[3]}

以上例子,会输出 null 在索引为 3 的值。下面的例子显示如果完全从数组中删除。

$ cat arraymanip.shUnix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');pos=3Unix=(${Unix[@]:0:$pos} ${Unix[@]:$(($pos + 1))})echo ${Unix[@]} $./arraymanip.shDebian Red hat Ubuntu Fedora UTS OpenLinux

本例中,${Unix[@]:0:$pos} 将输出 3 个元素从索引为 0 开始。合并以上的输出。这是删除数组元素的一个方法。

11. 使用模式 (Patterns) 删除数组元素

在搜索条件,可以给出模式 (Patterns),存储其余元素到另外一个数组。

$ cat arraymanip.sh#!/bin/bashdeclare -a Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora');declare -a patter=( ${Unix[@]/Red*/} )echo ${patter[@]} $ ./arraymanip.shDebian Ubuntu Suse Fedora

以上例子删除数组元素形如 Red*

12. 复制数组

扩展数组元素,存储到新的数组中。

#!/bin/bashUnix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');Linux=("${Unix[@]}")echo ${Linux[@]} $ ./arraymanip.shDebian Red hat Ubuntu Fedora UTS OpenLinux

13. 连接两个 Bash 数组

扩展两个数组,赋值给新数组。

$cat arraymanip.sh#!/bin/bashUnix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');Shell=('bash' 'csh' 'jsh' 'rsh' 'ksh' 'rc' 'tcsh')UnixShell=("${Unix[@]}" "${Shell[@]}")echo ${UnixShell[@]}echo ${#UnixShell[@]} $ ./arraymanip.shDebian Red hat Ubuntu Suse Fedora UTS OpenLinux bash csh jsh rsh ksh rc tcsh14

输出数组 ‘Unix’ 和 ‘Shell’ 中的所有元素,新数组一共有 14 个元素。

14. 删除整个数组

使用 unset 删除整个数组。

$cat arraymanip.sh#!/bin/bashUnix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');Shell=('bash' 'csh' 'jsh' 'rsh' 'ksh' 'rc' 'tcsh')UnixShell=("${Unix[@]}" "${Shell[@]}")unset UnixShellecho ${#UnixShell[@]} $ ./arraymanip.sh0

unset 数组后,数组长度为零。

15. 加载文件内容到数组

可以逐行添加文件内容到数组。

#Example file$ cat logfileWelcometothegeekstuffLinuxUnix $ cat loadcontent.sh#!/bin/bashfilecontent=( `cat "logfile" `) for t in "${filecontent[@]}"doecho $tdoneecho "Read file content!" $ ./loadcontent.shWelcometothegeekstuffLinuxUnixRead file content!

以上例子中,数组中的所有元素均利用循环输出。

0 0
原创粉丝点击