美文网首页
Bash Array 的 15个实例

Bash Array 的 15个实例

作者: 癞痢头 | 来源:发表于2021-03-26 11:21 被阅读0次

    数组是一个包含多个值的变量,它们可以是相同类型或不同类型。没有最大的数组大小限制,也没有要求成员变量被连续索引或连续分配。数组索引从零开始。

    本文展示最常用的15个array示例

    1.声明一个数组并赋值

    在bash中,使用以下格式的变量时会自动创建数组,

    name[index]=value
    
    • name 是数组的名称
    • index 可以是任何数字或表达式,其值必须等于或大于零。您可以使用 declare -a arrayname声明一个显式数组。
    $ 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)
    
    

    如果元素具有空格字符,请用引号将其引起来。

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

    declare -a 声明一个数组,括号中的所有元素都是数组的元素。

    3.打印整个Bash阵列

    有多种方法可以打印数组的整个元素。如果索引号是@或*,则引用数组的所有成员。您可以使用bash中的循环语句遍历数组元素并进行打印。

    echo ${Unix[@]}
    
    # 将上面一行添加到 arraymanip.sh
    #./arraymanip.sh
    
    Debian Red hat Ubuntu Suse
    

    4. Bash数组的长度

    使用称为$# 的特殊参数来获取数组的长度。

    ${#arrayname[@]} 将给出数组长度

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

    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.sh
    4
    
    

    6.按数组的偏移量和长度提取

    以下示例显示了从名为Unix的数组中从位置3开始提取2个元素的方法。

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

    上面的示例返回第三个索引和第四个索引中的元素。索引始终以零开头。

    7. 对于数组的特定元素,使用偏移量和长度提取

    从数组元素中只提取前四个元素。例如,位于数组第二个索引的Ubuntu,可以为数组的特定元素使用offset和length。

    $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}
    
    $./arraymanip.sh
    Debian Red hat SCO Unix Suse Fedora UTS OpenLinux
    

    在此示例中,它将第二个索引Ubuntu中的元素替换为SCO Unix。但是此示例不会永久替换数组内容。

    9. 将元素添加到现有的Bash数组

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

    在名为Unix的数组中,元素AIXHP-UX 分别添加在第7个索引和第8个索引中。

    10. 从数组中删除元素

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

    上面的脚本将只打印null,这是第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} 从 Unix 中拿出前3个元素, ${Unix[@]:4} 从Unix中拿出后四个元素. 把他们合并在一起成一个新的数组. 这也是种删除数组元素的方法

    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.两个Bash数组的串联

    展开两个数组的元素,然后将其分配给新数组。

    $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 arrayName

    $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!
    

    本文翻译自 https://www.thegeekstuff.com/2010/06/bash-array-tutorial/

    相关文章

      网友评论

          本文标题:Bash Array 的 15个实例

          本文链接:https://www.haomeiwen.com/subject/cjljhltx.html