woxio770 发表于 2018-5-21 13:10:50

Linux shell 数组使用

  Ubuntu12.04 TLS 64bit, bash 4.2.25

  一、定义
  数组是一些数据的集合,分为两个类型
  (1)普通数组,只能使用整数作为索引
  (2)关联数组,可以使用字符串作为索引,bash 4.0开始支持,旧版版不支持

  

  二、数组的一些操作
  (1)数组的定义以及赋值
  <普通数组>   

  【1】定义
  数组名 = ()
  【2】赋值
  1. 数组名 = (elem1 elem2 elem3 ......)

  每个元素由空格隔开
  2. 数组名 = "test1"
  数组名 = "test2"
  ......
  

  <关联数组>
  【1】定义
  declare -A 数组名
  【2】赋值
  1. 数组名 = ( = val1 = val2 .....)
  2. 数组名 = val1
  数组名 = val2
  ......

  
  (2)求数组的长度
      ${#数组名
[*]} 或 {#数组名[@]}  (3)获得数组制定索引的元素
      ${数组名[索引值]}  (4)获得数组的所有元素
      ${数组名
[*]} 或 ${数组名[@]}  (5)数组元素的替换
      ${数组名
[*]/查找字符/替换字符} 或 ${数组名[@]/查找字符/替换字符}  替换并没有改变原来的数组,要修改原来的数组,需要对数组重新赋值
  (6)删除数组元素
         unset 数组名#删除整个数组
         unset 数组名[索引值]#删除指定的元素  (7)数组的切片
      ${数组名
[*]:起始位置:长度}或${数组名[@]:起始位置:长度}  切片后返回的是由空格隔开的字符串,加上()后将得到切片后的子数组
  (8)获得数组的所有索引值
      ${!数组名
[*]} 或 ${!数组名[@]}  

  三、例子
    #!/bin/bash   
   
    #定义普通数组并进行赋值
    comm_arr=(1 2 3 4 5)
    comm_arr2=()
    comm_arr2=6
    comm_arr2=7
    comm_arr2=8
    #获得普通数组的长度
    echo "comm_arr1 len is ${#comm_arr
[*]}"
    echo "comm_arr2 len is ${#comm_arr2[@]}"
    #获得普通数组的所有元素
    echo "comm_arr1 all elem is : ${comm_arr
[*]}"
    echo "comm_arr2 all elem is :${comm_arr2[@]}"
    #通过索引获得元素
    echo "comm_arr = ${comm_arr}"
    echo "comm_arr = ${comm_arr2}"
    echo "comm_arr = ${comm_arr}" #索引值不存在,但不出错,结果为空
    #普通数组中元素的替换
    echo "3 in comm_arr is replace 200 : ${comm_arr
[*]/3/200}"
    echo "comm_arr is ${comm_arr
[*]}" #原数组没有被修改
    comm_arr2=(${comm_arr2[@]/7/9})#替换原数组
    echo "7 in comm_arr2 is replace 9 and modify comm_arr2"
    echo "comm_arr2 is ${comm_arr2
[*]}"
    #普通数组的切片
    echo "splite comm_arr 1-3 is : ${comm_arr
[*]:1:3}"
    comm_arr_split=(${comm_arr[@]:2:3}) #获得从第三个位置子开始3个元素的子数组
    echo "sub arr : len = ${#comm_arr_split
[*]} , elems is : ${comm_arr_split[@]}"
    #普通数组的删除
    echo "del before, comm_arr is ${comm_arr
[*]}"
    echo "del a elem"
    unset comm_arr #删除某个元素
    echo "del after, comm_arr is ${comm_arr
[*]}"
    echo "del all arr"
    unset comm_arr #删除整个数组
    echo "del after, comm_arr is ${comm_arr
[*]}" #为空
    #获得所有索引值
    echo "indexs is ${!comm_arr2[@]}"
    #定义关联数组并赋值
    declare -A link_arr
    link_arr['apple']='10$'
    link_arr='100Y'
    declare -A link_arr2
    link_arr2=(=20 =zhangsan =m)
    #获得关联数组的长度
    echo "link arr len: ${#link_arr
[*]}"
    echo "link arr2 len: ${#link_arr2[@]}"
    #通过索引获得元素
    echo "link arr index=apple, elem is ${link_arr['apple']}"
    echo "link arr2 index=name, elem is ${link_arr2}"
    echo "link arr index=name not exist, but no error, ${link_arr}" #虽然没有这个索引,但不会错误,只是结果为空
    #输出关联数组中所有元素
    echo "apple is ${link_arr
[*]}"
    echo "link_arr2 is ${link_arr2[@]}"
    #关联数组中的替换
    echo "link arr2 zhangsan is replace lisi, ${link_arr2
[*]/zhangsan/lisi}" #原数组没有修改
    echo "link arr2 is ${link_arr2
[*]}"
    #link_arr2=(${link_arr2
[*]/zhangsan/lisi}) #报错,关联数组必须使用下标
    #echo "link arr2 is ${link_arr2
[*]}"
    #echo "link arr2 name=${link_arr2}"
    #关联数组的切片
    echo "link arr2 age-name: ${link_arr2
[*]:name:2}"
    #关联数组的删除
    echo "del before link arr2 is ${link_arr2
[*]}"
    unset link_arr2
    echo ""del after link arr2 is ${link_arr2
[*]}
    unset link_arr
    echo "del all arr ${link_arr
[*]}"
      
    #获得所有的索引值
    echo "link arr2 indexs : ${link_arr2
[*]}"  结果:

  comm_arr1 len is 5
            comm_arr2 len is 3
            comm_arr1 all elem is : 1 2 3 4 5
            comm_arr2 all elem is :6 7 8
            comm_arr = 5
            comm_arr = 8
            comm_arr =
            3 in comm_arr is replace 200 : 1 2 200 4 5
            comm_arr is 1 2 3 4 5
            7 in comm_arr2 is replace 9 and modify comm_arr2
            comm_arr2 is 6 9 8
            splite comm_arr 1-3 is : 2 3 4
            sub arr : len = 3 , elems is : 3 4 5
            del before, comm_arr is 1 2 3 4 5
            del a elem
            del after, comm_arr is 2 3 4 5
            del all arr
            del after, comm_arr is

  indexs is 0 1 2         

  link arr len: 2
            link arr2 len: 3
            link arr index=apple, elem is 10$
            link arr2 index=name, elem is zhangsan
            link arr index=name not exist, but no error,
            apple is 100Y 10$
            link_arr2 is zhangsan 20 m
            link arr2 zhangsan is replace lisi, lisi 20 m
            link arr2 is zhangsan 20 m
            link arr2 age-name: zhangsan 20
            del before link arr2 is zhangsan 20 m
            del after link arr2 is zhangsan m
            del all arr
            link arr2 indexs : zhangsan m
四、总结
  【1】与C语言的数组相比较
  (1)C语言的数组的下标值只能是整数; 而shell的数组的下标值可以是字符串
  (2)C语言的数组的内容是一组相同类型的元素; 而shell数组的内容可以是混杂的
  (3)C语言的数组的大小在声明的时候就必须指定,并且大小不能扩增; 而shell数组声明不需要指定大小,直接添加元素即可
  (4)访问C语言的数组时, 下标值不能越界, 否则出错; 而shell数组没有限制,访问不存在的下标的元素时返回空,而不出错
  (5)C语言数组没有shell数组那么多操作
页: [1]
查看完整版本: Linux shell 数组使用