• linux shell 数组的长度计算、修改、循环输出等操作


    From :http://blog.csdn.net/snrqtdhuqf/article/details/7242309 

    在shell中,数组变量的赋值有两种方法:

    (1) name = (value1 ... valuen)此时下标从0开始

    (2) name[index] = value

     example:

    1. #!/bin/sh  
    2. #arrayTest  
    3. name=(yunix yhx yfj)  
    4. echo "array is:${name[@]}"  
    5. echo "array length is:${#name[*]}"  
    6. echo ${name[1]}  
    7. name[1]=yang  
    8. echo ${name[1]}  
    9. read -a name  
    10. echo ${name[1]}  
    11. echo "loop the array"  
    12. len=${#name[*]}  
    13. i=0  
    14. while [ $i -lt $len ]  
    15. do  
    16. echo ${name[$i]}  
    17. let i++  
    18. done  

    result:

    array is:yunix yhx yfj
    array length is:3
    yhx
    yang
    a b c d e
    b
    loop the array
    a
    b
    c
    d
    e

    下面的是关于数组的输出实例

    example:

     

    #!/bin/sh  

    1. #arrayLoopOut  
    2. read -a array  
    3. len=${#array[*]}  
    4. echo "array's length is $len"  
    5. echo "use while out the array:"  
    6. i=0  
    7. while [ $i -lt $len ]  
    8. do  
    9.         echo -n "${array[$i]}"  
    10. let i++  
    11. done  
    12. echo  
    13. echo "use for out the array:"  
    14. for ((j=0;j<"$len";j=j+1))  
    15. do  
    16.         echo -n ${array[$j]}  
    17. done  
    18. echo  
    19. echo "use for in out the array:"  
    20. for value in ${array[*]}  
    21. do  
    22. echo -n $value  
    23. done  

    result:

    a b c d e f g
    array's length is 7
    use while out the array:
    abcdefg
    use for out the array:
    abcdefg
    use for in out the array:
    abcdefg

  • 相关阅读:
    密码加密
    注册视图
    session会话
    验证码功能
    使用Django表单替代html表单
    实现登陆视图功能
    设计登陆需求页面
    配置视图
    配置数据库
    npm 学习
  • 原文地址:https://www.cnblogs.com/cnland/p/2949843.html
Copyright © 2020-2023  润新知