1 .创建数组    array=(1 2 3 5 8) (以此为例)

    或者  array=([1]=one [2]=two [3]=three)    array=( ($ls) )

2 .获取数组长度    echo ${#array[@]} 或 echo ${#array[*]}  得到 5

3 .打印数组元素    echo ${array[@||*]}(显示全部)  echo ${array[1]}(显示 2 ) 

4 .数组赋值    array[0]=9(替换数组元素)  array[5]=five(增加数组元素)

5 .数组内容截取与替换     echo ${array[@||*] :1:3} (显示 2 3 5 )

               echo ${array[@||*] :3:2} (显示 5 8 )从第三位截取显示两位 

                echo ${array[@||*] /3/6} ( 显示 1 2 6 5 8 five )临时替换

            array=(echo ${array[@||*] /3/6}) (显示 1 2 6 5 8 five)永久生效

6 .数组匹配删除    echo ${array[@||*]#f} (显示 1 2 6 5 8 ive)

            echo ${array[@||*]%f*e} (显示 1 2 6 5 8)

            echo ${array[@||*]#fi} (显示 1 2 6 5 8 ve) 均临时生效

         

7 .数组元素删除    unset array (删除整个数组 )  unset array[0] (删除数组元素)

例子:(列出当前目录文件)

        

    #!/bin/sh

    

    dir=($(ls))

    for((i=0; i<${#dir[@||*]}; i++))

    do

        echo  ${dir[$i]}

    done

(显示为 ls 结果集 列表显示)

例子: (检查URL脚本(数组实现))

    #!/bin/sh

    . /etc/init.d/function

    url_list=(

    

    

    http://www.163.com

    )

    function wait(){

        echo -n "3秒后,执行操作"

        for((i=0; i<3; i++))

        do

            echo -n ".";sleep 1

        done

    }

    function check_url(){

        for((i=0; i<${#url_list[@]}; i++))

        do

            judge=($(curl -I -s ${url_list[$i]} | head -l | tr "\r" "\n"))

            if [[ "${judge[1]}" == '200' && "$judge[2]" == "OK" ]];then

                action "${url_list[$i]}"  /bin/true

            else

                action "${url_list[$i]}"  /bin/false     

            fi

        done

    }

    check_url