Bash 数组示例

原文 http://www.thegeekstuff.com/2...

1 数组声明

像下面 会自动创建 name 数组,不用声明

name[index]=value

例如

$ cat arraymanip.sh
#! /bin/bash
Unix[0]='Debian'
Unix[1]='Red hat'
Unix[2]='Ubuntu'
Unix[3]='Suse'

echo ${Unix[1]}

$./arraymanip.sh
Red hat

通过索引访问元素 ${name[index]}

2 声明时初始化数组

语法

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

元素使用空格分隔

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

3 打印整个数组

如果索引是 @* 表示整个数组

echo ${Unix[@]}

# Add the above echo statement into the arraymanip.sh
#./t.sh
Debian Red hat Ubuntu Suse

4 获取数组长度

语法

${#arrayname[@]}

$ cat arraymanip.sh
declare -a Unix=('Debian' 'Red hat' 'Suse' 'Fedora');
echo ${#Unix[@]} # 数组长度
echo ${#Unix}  # 这个是获取数组第一个元素字符数量 .i.e Debian
$./arraymanip.sh
4
6

5 数组中第n个元素长度

${#arrayname[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.sh
4

6 通过数组的偏移量和长度来提取子数组

下面的例子从数组Unix 位置3开始,提取2个元素, 索引从0开始

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

$./arraymanip.sh
Suse Fedora

7 根据偏移和长度获取数组元素的一部分

例如,位于数组的第二个索引的Ubuntu 获取基 第0个到第4个字符

$cat arraymanip.sh
#! /bin/bash

Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
echo ${Unix[2]:0:4}

./arraymanip.sh
Ubun

上述示例从数组的第二个索引元素中提取前四个字符

8 搜索 替换 数组中元素

下面的代码 搜索 Ubuntu 然后 替换成 SCO Unix

$cat arraymanip.sh
#!/bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');

echo ${Unix[@]/Ubuntu/SCO Unix}
echo ${Unix[@]}

$./arraymanip.sh
Debian Red hat SCO Unix Suse Fedora UTS OpenLinux
Debian Red hat Ubuntu Suse Fedora UTS OpenLinux

在这个例子中, 它用“SCO Unix”替换了第二个索引“Ubuntu”中的元素, 但是这个例子不会改变原来数组

9 向数组中添加元素

向数组Unix中添加 AIX HP-UX

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

$./arraymanip.sh
AIX

10 在从数组删除元素

使用 unset 删除索引3的Suse

$cat arraymanip.sh
#!/bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');

unset Unix[3]
echo ${Unix[3]}

上面的例子打印索引3的值为空,并不是我们想要的结果, 下面的例子完整的移除一个元素

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

$./arraymanip.sh
Debian Red hat Ubuntu Fedora UTS OpenLinux

上面的代码中 ${Unix[@]:0:$pos} 表示 从索引0到pos的子数组 ${Unix[@]:4} 表示从索引4到最后一个元素的子数组
然后 把两个子数组合并成一个数组

11 通过模式删除数组元素

在搜索条件下,您可以给出模式,并将剩余元素存储到另一个数组,比如

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

$ ./arraymanip.sh
Debian Ubuntu Suse Fedora

上述示例删除了具有模式Red* 的元素

12 复制一个数组

展开数组元素并将其存储到新的数组中

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

$ ./arraymanip.sh
Debian Red hat Ubuntu Fedora UTS OpenLinux

13 合并两个数组

展开两个数组的元素并将其分配给新数组

$cat arraymanip.sh
#!/bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
Shell=('bash' 'csh' 'jsh' 'rsh' 'ksh' 'rc' 'tcsh');

UnixShell=("${Unix[@]}" "${Shell[@]}")
echo ${UnixShell[@]}
echo ${#UnixShell[@]}

$ ./arraymanip.sh
Debian Red hat Ubuntu Suse Fedora UTS OpenLinux bash csh jsh rsh ksh rc tcsh
14

14 删除整个数组

使用 unset 命令

$cat arraymanip.sh
#!/bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
Shell=('bash' 'csh' 'jsh' 'rsh' 'ksh' 'rc' 'tcsh');

UnixShell=("${Unix[@]}" "${Shell[@]}")
unset UnixShell
echo ${#UnixShell[@]}

$ ./arraymanip.sh
0

15 从文件加载数组

读取文件每一行,一行作为数组中的一个元素

#Example file
$ cat logfile
Welcome
to
thegeekstuff
Linux
Unix

$ cat loadcontent.sh
#!/bin/bash
filecontent=( `cat "logfile" `)

for t in "${filecontent[@]}"
do
    echo $t
done
echo "Read file content!"

$ ./loadcontent.sh
Welcome
to
thegeekstuff
Linux
Unix
Read file content!

你可能感兴趣的:(bash)