5shell in the array

0. understand the array

Subscript (1) shell does not limit the size of the array, the array elements counting from zero

(2) elements in the array to obtain the subscript [], can be an integer index, may be a result of the expression is an integer, but the index must be greater than or equal to 0

(3) bash shell supports only one-dimensional arrays, multidimensional arrays are not supported

1. Definition of an array

In the Shell, parentheses ( )are denoted by separated by spaces between array, the array elements. Thus, the general form of an array is defined as:

array_name=(ele1  ele2  ele3 ... elen)

Note that an assignment =on both sides No spaces must be next to the array name and array elements.

situation
script
Explanation
Definition array
  1. nums=(29 100 13 91 44)

echo ${nums[*]}----->29 100 13 8 91 44

Shell is weakly typed, it does not require the type of all array elements must be the same
  1. arr=(20 56 "http://c.biancheng.net/shell/")
The third element is a "heterogeneous", the first two elements are integers, and the third element is a string.
Shell length of the array is not fixed, then the element definition may also increase
  1. nums=(29 100 13 91 44)
  2. nums[6]=88

echo ${nums[*]}------>9 100 13 8 91 44 88

No need to assign to the array element by element, the following code is assigned only to specific elements:

  1. ages=([3]=24 [5]=19 [10]=12)

Only the above code assignment to the first element 3,5,10, so the length of the array is 3.

  • echo ${ages[3]}------->24
  • echo ${ages[4]}------->空
  • echo ${ages[5]}-----→19
  • echo ${ages[11]}------>空

Noting that while the array length is 3, but the only real value of the array at the specified location, the value of other positions are empty, even though cross-border subscript position, its value is still empty, no error

2. Get an array element

Get the value of array element, using the following general format: $ {array_name [index]}, where, ARRAY_NAME is the array name, index is the index.

situation
script
result
Gets an array of the second element echo ${nums[2]}  
Use @or *can get all the elements in the array

echo ${nums[*]}

echo ${nums[@]}

 
For Li
  1. #!/bin/bash
  2. nums=(1 3 5 7)
  3. echo ' the output of all the array elements: ' $ {the nums [ * ]} 
  4. echo ' outputs of two elements: ' $ {the nums [ 2 ]} 
  5. the nums [ . 5 ] = . 8 # assigned to the first 5 elements (in this case will increase the length of the array)
  6. echo ' outputs of three elements: ' $ {the nums [ . 3 ]} 
  7. echo ' outputs of the four elements: ' $ {the nums [ . 4 ]} 
  8. echo ' outputs five elements: ' $ {the nums [ . 5 ]} 
  9. echo ' the output of all the array elements: ' $ {the nums [ @ ]} 
operation result:

The output of all the array elements: 1357

Output of the second element: 5

Output of three elements: 7

The output of the four elements:

The output of the five elements: 8

The output of all the array elements: 13578

3. Calculate the array length

Length of the array, i.e. the number of array elements can be used #to obtain the number of array elements.

situation
script
result
使用#来获取数组元素的个数 ${#array_name[@]}
${#array_name[*]}
 
获取指定下标位置的元素长度

获取 arr 数组的第 2 个元素(假设它是字符串)的长度。

${#arr[2]} 

 
字符串长度的获取方式 ${#string_name}  
举栗
  1. #!/bin/bash
  2. nums=(1 3 5 7)

  3. echo '输出数组的长度:'${#nums[*]}

  4. var1='qwertyuiop'

  5. echo '字符串长度:'${#var1}

  6. nums[8]=${var1}

  7. echo '输出数组的长度:'${#nums[@]}

  8. echo '第1个元素的长度:'${#nums[1]}

  9. echo '第8个元素的长度:'${#nums[8]}

  10. unset nums[1]#删除元素

  11. echo '输出数组的长度:'${#nums[@]}

运行结果:

输出数组的长度:4

字符串长度:10

输出数组的长度:5

第1个元素的长度:1

第8个元素的长度:10

输出数组的长度:4

4.拼接数组

所谓 Shell 数组拼接就是将两个数组连接成一个数组。
拼接数组的思路是:先利用@*,将数组扩展成列表,然后再合并到一起。具体格式如下:

array_new=(${array1[@]}  ${array2[@]})
array_new=(${array1[*]}  ${array2[*]})

举栗
结果
  1. #!/bin/bash
  2. arr1=(1 2)
  3. arr2=(qwer tyui)
  4. array_new1=(${arr1[*]} ${arr2[@]})
  5. array_new2=(${arr1[@]} ${arr2[*]})
  6. echo ${array_new1[*]}
  7. echo ${array_new2[@]}
运行结果:

1 2 qwer tyui

1 2 qwer tyui

5.删除数组元素或数组

情形
脚本
结果
使用 unset 关键字来删除数组元素 unset array_name[index]  
删除整个数组 unset array_name  
举栗
  1. #!/bin/bash
  2. arr=(23 56 99 66 )
  3. unset arr[1]
  4. echo ${arr[@]}
  5. unset arr
  6. echo ${arr[*]}

运行结果:

23 99 66

 

注意最后的空行,它表示什么也没输出,因为数组被删除了,所以输出为空

6.关联数组

shell中的关联数组类似于Python中字典的概念,关联数组也成为键值对数组,键(key)即数组的下标,值(value)即元素值。

6.1 创建关联数组

创建关联数组必须使用带有-A选项的 declare 命令

情形
脚本
新定义后赋值
  1. declare -A people
  2. color['num1']=xiaohong
  3. color[num2]=xiaobai
  4. color["num3"]=xiaohei
定义时同时赋值
  1. declare -A people=(['num1']='xiaohong' [num2]='xiaobai' [num3]=xiaohei)

6.2 访问关联数组元素

情形
脚本
结果
获取关联数组的元素值 $(array_name["index"])

echo ${people[num1]}-----> xiaohong

获取所有元素的下标 ${!array_name[@]}
${!array_name[*]}

echo ${people[*]}----->xiaohong xiaobai xiaohei

echo ${people[@]}----->xiaohong xiaobai xiaohei

获取所有元素的值 ${array_name[@]}
${array_name[*]}

echo ${!people[*]}-----> num1 num2 num3

echo ${!people[@]}-----> num1 num2 num3

获取关联数组长度

与获取普通数组长度的方式相同

${#array_name[*]}
${#array_name[@]}

echo ${#people[*]}-----> 3

echo ${#people[@]}-----> 3

举栗
  1. #!/bin/bash
  2. declare -A people=(['num1']='xiaohong' [num2]='xiaobai' [num3]=xiaohei)
  3. echo '联合数组的key值为:'${!people[*]}
  4. echo '联合数组元素值为:'${people[*]}
  5. echo '联合数组的长度为:'${#people[@]}
  6. echo '循环打印联合数组的key值'
  7. for item in ${!people[@]}
  8. do
  9. echo ${item}
  10. done
  11.  
  12. echo '循环打印联合数组的元素值'
  13. for item in ${people[*]}
  14. do
  15. echo ${item}
  16. done
  17.  
  18. echo '循环打印联合数组的键值对'
  19. for item in ${!people[@]}
  20. do
  21. echo ${item}--\>${people[$item]}
  22. done
运行结果:
  • 联合数组的key值为:num1 num2 num3
  • 联合数组元素值为:xiaohong xiaobai xiaohei
  • 联合数组的长度为:3
  • 循环打印联合数组的key值
  • num1
  • num2
  • num3
  • 循环打印联合数组的元素值
  • xiaohong
  • xiaobai
  • xiaohei
  • 循环打印联合数组的键值对
  • num1-->xiaohong
  • num2-->xiaobai
  • num3-->xiaohei

Guess you like

Origin www.cnblogs.com/mianbaoshu/p/12069491.html