shell之 数组(Array)

 shell之 数组

    bash shell只支持一维。数组从0开始,以array[x]表示数组元素。array[0]表示第一个元素。bash shell 支持最大数组标号是599 147 937 791。获取bash shell数组值的方式 ${array[x]}。

(一)bash shell 数组常见用法:

1、bash shell脚本执行权限

2、数组赋值和获取数组值

3、圆括号对数组赋值

4、圆括号赋值且指定元素值

5、@和*表示数组元素

6、@和*加引号打印区别

(二)bash shell 数组特殊用法

1、抽取、删除和替换 数组元素中的子串

2、声明数组、清空数组、求取数组长度

3、数组与数组连接

(三)bash shell 实现数据结构

bash shell不直接支持堆栈和二维数组,但是可以通过一维数组来实现这些线性数据结构。

1、实现堆栈

2、实现二维数组

(一)bash shell 数组常见用法:

 
  1. 1

    1、bash shell脚本执行权限

     

    [root@rhel6 ~]# mkdir /zxx_shell

    [root@rhel6 ~]# chown -R oracle:oinstall /zxx_shell/

    [root@rhel6 ~]# su - oracle

    [oracle@rhel6 ~]$ cd /zxx_shell/

    [oracle@rhel6 zxx_shell]$ vi 1-array.sh

    [oracle@rhel6 zxx_shell]$ ll

    总用量 4

    -rw-r--r--. 1 oracle oinstall 168 8月   4 17:00 1-array.sh

    [oracle@rhel6 zxx_shell]$ chmod +x *

    [oracle@rhel6 zxx_shell]$ ll

    总用量 4

    -rwxr-xr-x. 1 oracle oinstall 168 8月   4 17:00 1-array.sh

    shell脚本需要执行权限 

     

    shell之 数组(Array)
  2. 2

    2、数组赋值和获取数组值

    [oracle@rhel6 zxx_shell]$ cat 1-array.sh 

    #!/bin/bash

     

    clxx[0]=wiscom0

    clxx[1]=0256656

    clxx[8]=wiscom8

     

    echo "clxx[0]=${clxx[0]}"

    echo "clxx[1]=${clxx[1]}"

    echo "clxx[8]=${clxx[8]}"

    echo "clxx[20]=${clxx[20]}"

     

    [oracle@rhel6 zxx_shell]$ ./1-array.sh 

    clxx[0]=wiscom0

    clxx[1]=0256656

    clxx[8]=wiscom8

    clxx[20]=

    允许数组空缺元素,也可以不连续的给数组赋值。

    shell之 数组(Array)
  3. 3

    3、圆括号对数组赋值

     

     

    [oracle@rhel6 zxx_shell]$ cat 1-array.sh 

    #!/bin/bash

     

    clxx=(wiscom0 0256656 wiscom8)

     

    echo "clxx[0]=${clxx[0]}"

    echo "clxx[1]=${clxx[1]}"

    echo "clxx[2]=${clxx[2]}"

    echo "clxx[3]=${clxx[3]}"

    echo "clxx[8]=${clxx[8]}"

    echo "clxx[20]=${clxx[20]}"

     

    [oracle@rhel6 zxx_shell]$ ./1-array.sh 

    clxx[0]=wiscom0

    clxx[1]=0256656

    clxx[2]=wiscom8

    clxx[3]=

    clxx[8]=

    clxx[20]=

     

    圆括号默认从0开始,默认以空格为分隔符。

    shell之 数组(Array)
  4. 4

    4、圆括号赋值且指定元素值

     

    [oracle@rhel6 zxx_shell]$ cat 1-array.sh 

    #!/bin/bash

     

    clxx=(wiscom0 [3]=0256656 wiscom8)

     

    echo "clxx[0]=${clxx[0]}"

    echo "clxx[1]=${clxx[1]}"

    echo "clxx[2]=${clxx[2]}"

    echo "clxx[3]=${clxx[3]}"

    echo "clxx[4]=${clxx[4]}"

    echo "clxx[8]=${clxx[8]}"

    echo "clxx[20]=${clxx[20]}"

    [oracle@rhel6 zxx_shell]$ ./1-array.sh 

    clxx[0]=wiscom0

    clxx[1]=

    clxx[2]=

    clxx[3]=0256656

    clxx[4]=wiscom8

    clxx[8]=

    clxx[20]=

    可以元素位置从指定元素重新计算,如果指定元素之后值没有指定元素

    [oracle@rhel6 zxx_shell]$ cat 1-array.sh

    #!/bin/bash

     

    clxx=([8]=wiscom0 [3]=0256656 [0]=wiscom8)

     

    echo "clxx[0]=${clxx[0]}"

    echo "clxx[1]=${clxx[1]}"

    echo "clxx[2]=${clxx[2]}"

    echo "clxx[3]=${clxx[3]}"

    echo "clxx[4]=${clxx[4]}"

    echo "clxx[8]=${clxx[8]}"

    echo "clxx[20]=${clxx[20]}"

    [oracle@rhel6 zxx_shell]$ ./1-array.sh 

    clxx[0]=wiscom8

    clxx[1]=

    clxx[2]=

    clxx[3]=0256656

    clxx[4]=

    clxx[8]=wiscom0

    clxx[20]=

     

    shell之 数组(Array)
  5. 5

    5、@和*表示数组元素

     

    [oracle@rhel6 zxx_shell]$ cat 1-array.sh

    #!/bin/bash

     

    clxx=([8]=wiscom0 [3]=0256656 [0]=wiscom8)

     

    echo "clxx[@]=${clxx[@]}"

    echo "----------------"

    echo "clxx[*]=${clxx[*]}"

    [oracle@rhel6 zxx_shell]$ ./1-array.sh 

    clxx[@]=wiscom8 0256656 wiscom0

    ----------------

    clxx[*]=wiscom8 0256656 wiscom0

    @和*表示数组元素表示数组所有元素。

     

    [oracle@rhel6 zxx_shell]$ cat 1-array.sh

    #!/bin/bash

     

    clxx=([8]=wiscom0 [3]=0256656 [0]=wiscom8)

     

    for i in ${clxx[@]}

    do

        echo $i

    done  

    [oracle@rhel6 zxx_shell]$ ./1-array.sh 

    wiscom8

    0256656

    wiscom0

    打印时只输出赋值的元素。

    shell之 数组(Array)
  6. 6

    6、@和*加引号打印区别

     

    注意:当使用引号时,@和*打印有区别

    [oracle@rhel6 zxx_shell]$ cat 1-array.sh

    #!/bin/bash

     

    clxx=([8]=wiscom0 [3]=0256656 [0]=wiscom8)

     

    for i in "${clxx[@]}"

    do

        echo $i

    done  

    [oracle@rhel6 zxx_shell]$ ./1-array.sh 

    wiscom8

    0256656

    wiscom0

    [oracle@rhel6 zxx_shell]$ vi 1-array.sh 

    [oracle@rhel6 zxx_shell]$ cat 1-array.sh 

    #!/bin/bash

     

    clxx=([8]=wiscom0 [3]=0256656 [0]=wiscom8)

     

    for i in "${clxx[*]}"

    do

        echo $i

    done  

    [oracle@rhel6 zxx_shell]$ ./1-array.sh 

    wiscom8 0256656 wiscom0

     

    shell之 数组(Array)
    END

(二)bash shell 数组特殊用法

 
  1. 1

    1、抽取、删除和替换 数组元素中的子串

    [oracle@rhel6 zxx_shell]$ cat 1-array.sh 

    #!/bin/bash

     

    clxx=(wsscom0 0256656 wiscom888 ednns)

     

    echo "extracting substring"

    echo ${clxx[*]:0}       #表示从第一个元素开始抽取

    echo ${clxx[*]:1}       #表示从第二个元素开始抽取

    echo ${clxx[*]:3}

    echo ${clxx[*]:0:1}   #表示从第一个元素开始抽取2个元素

     

    echo "removing substring"

    echo ${clxx[*]#w*s}    #表示删除元素中匹配w*s的子串

     

    echo "replacing substring"

    echo ${clxx[*]/w*s/zxx}   #表示第一次全部将元素中匹配w*s的子串替换为"zxx"子串

    echo ${clxx[*]//s*c/cs}   

    #表示全部将元素中匹配s*c的子串替换为"cs"子串,如果第一次替换之后又符合“s*c”又进行替换,直到没有为止

     

    [oracle@rhel6 zxx_shell]$ ./1-array.sh 

    extracting substring

    wsscom0 0256656 wiscom888 ednns

    0256656 wiscom888 ednns

    ednns

    wsscom0

    removing substring

    scom0 0256656 com888 ednns

    replacing substring

    zxxcom0 0256656 zxxcom888 ednns

    wcsom0 0256656 wicsom888 ednns

    shell之 数组(Array)
  2. 2

    2、声明数组、清空数组、求取数组长度

     

    [oracle@rhel6 zxx_shell]$ cat 1-array.sh 

    #!/bin/bash

     

    declare -a clxx    #声明clxx为一个数组

     

    echo "input company by a SPACE"  #元素之间需要用空格隔离

    read -a clxx        #将键盘输入的值赋值给clxx数组

     

    for i in $ "${clxx[*]}"

    do

     echo "$i"

    done

     

    echo "the length of this array clxx is:${#clxx[*]}"  #获取数组长度

    unset clxx[1]    #清除clxx[1]元素

    echo "the length of this array clxx is:${#clxx[*]}"

    unset clxx   #清除clxx整个数组

    echo "the length of this array clxx is:${#clxx[*]}"

     

    [oracle@rhel6 zxx_shell]$ ./1-array.sh 

    input company by a SPACE

    wiscom ednns zxx wisedu

    $

    wiscom ednns zxx wisedu

    the length of this array clxx is:4

    the length of this array clxx is:3

    the length of this array clxx is:0

    [oracle@rhel6 zxx_shell]$ vi 1-array.sh 

    [oracle@rhel6 zxx_shell]$ ./1-array.sh 

    input company by a SPACE

    wiscom ednns zxx wisedu

     

    $

    wiscom ednns zxx wisedu

    the length of this array clxx is:4

    the length of this array clxx is:3

    the length of this array clxx is:0

     

    shell之 数组(Array)
  3. 3

    3、数组与数组连接

     

    [oracle@rhel6 zxx_shell]$ cat 1-array.sh 

    #!/bin/bash

     

    compony=(wiscom wisvision wisedu)

    person=(zxx [7]=wl yxz)

     

    declare -a combine    #声明combine为数组

     

    combine=(${compony[*]} ${person[*]})   #连接两个数组组成combine数组

     

    element_count=${#combine[*]}

    index=0

    while [ "$index" -lt "$element_count"  ]  #while循环遍历

    do

      echo "element[$index]=${combine[$index]}"

      let "index=$index+1"   

    done

     

    echo 

     

    unset combine   #清空数组

     

    combine[0]=${compony[*]}

    combine[1]=${person[*]}

    element_count=${#combine[*]}

    index=0

    while [ "$index" -lt "$element_count"  ]

    do

      echo "element[$index]=${combine[$index]}"

      let "index=$index+1"

    done

     

    [oracle@rhel6 zxx_shell]$ ./1-array.sh 

    element[0]=wiscom

    element[1]=wisvision

    element[2]=wisedu

    element[3]=zxx

    element[4]=wl

    element[5]=yxz

     

    element[0]=wiscom wisvision wisedu

    element[1]=zxx wl yxz

    第一种连接会将子数组中的不连接元素按顺序重新赋值给连接数组

    第二种连接直接输类似赋值

    shell之 数组(Array)
    END

(三)bash shell 实现数据结构

 
  1.  

    1、实现堆栈

     

    [oracle@rhel6 zxx_shell]$ cat 1-array.sh 

    #!/bin/bash

     

    maxtop=50   #堆栈放入最大元素

    top=$maxtop  #定义栈顶指针,初始值为maxtop

     

    temp=   #定义临时全局变量,存放出栈元素

    declare -a stack   #定义一个全局数组stack

     

    push()

    {

    if [ -z "$1"  ]    #如果第一参数为空,退出push函数

    then

      return

    fi

     

    until [ $# -eq 0  ]  #如果参数个数为0退出

    do

      let top=top-1

      stack[$top]=$1

      shift   #将后面参数逐个覆盖前面参数

    done

    return

    }

    #push是将第一个参数赋值给stack[49],第二个参数赋值给stack[48],依次循环

    pop()

    {

    temp=

    if [ "$top" -eq "$maxtop"  ]  #如果堆栈为空退出pop

    then

      return

    fi

     

    temp=${stack[$top]}   #将栈顶赋值给temp

    unset stack[$top]

    let top=top+1

    return

    }

    #pop是将栈顶赋值给temp,然后清空栈顶

    status()

    {

    echo "===============stack===================="

    for i in ${stack[*]}

    do

      echo $i

    done

    echo "------------------------------------"

    echo "stack pointer=$top"

    echo "just popped \""$temp"\" off the stack"

    echo "=========================================="

    }

     

    push wiscom

    status

    push ednns wisvision wisedu

    status

     

    pop

    pop

    status

    push zxx

    push zxx kkk

    status

     

    [oracle@rhel6 zxx_shell]$ ./1-array.sh 

    ===============stack====================

    wiscom

    ------------------------------------

    stack pointer=49

    just popped "" off the stack

    ==========================================

    ===============stack====================

    wisedu

    wisvision

    ednns

    wiscom

    ------------------------------------

    stack pointer=46

    just popped "" off the stack

    ==========================================

    ===============stack====================

    ednns

    wiscom

    ------------------------------------

    stack pointer=48

    just popped "wisvision" off the stack

    ==========================================

    ===============stack====================

    kkk

    zxx

    zxx

    ednns

    wiscom

    ------------------------------------

    stack pointer=45

    just popped "wisvision" off the stack

    ==========================================

    第一个参数为: $1 参数个数为: $#

    shift原来的$2会变成$1

    shell之 数组(Array)
  2. 2

    2、实现二维数组

posted @ 2017-01-11 22:55  少年努力吧  阅读(851)  评论(0编辑  收藏  举报