Array Foundation for shell Script Advancement

SHELL script -- shell array base

The difference between arrays and variables: variables occupy discrete space in memory, arrays open up a continuous large memory space in the inner rough, and then each element in the array is put into the array memory. Array elements are index ed.

There are two kinds of arrays in bash: ordinary arrays and associative arrays. Ordinary arrays can only be indexed by integer class values, and associative arrays can be indexed by strings. The other three terms of associative array: dict ionary, hash structure and map are one-to-one correspondences between key and value.

I. Ordinary arrays

1.1 Defines ordinary arrays

Method 1:

[root@linux1 ~]# age=(18 22 36 28)
[root@linux1 ~]# echo ${age[3]}
28

Method 2: Customize index bits

[root@linux1 ~]# age[5]=15
[root@linux1 ~]# age[6]=76

1.2 Print all values of the array

[root@linux1 ~]# echo ${age[*]}
18 22 36 28 15 76
[root@linux1 ~]# echo ${age[@]}
18 22 36 28 15 76

1.3 View Array Index Number

[root@linux1 ~]# echo ${!age[@]}
0 1 2 3 5 6

1.4 Number of elements in an array of statistics (only elements whose statistics are not empty)

[root@linux1 ~]# echo ${#age[@]}
6
[root@linux1 ~]# echo ${#age[*]}
6

Length of variables with subscripts of 1.5 statistical arrays

[root@linux1 ~]# echo ${#age[1]}
2

II. Association Array

2.1 Declare associative arrays

[root@linux1 ~]# declare -A array_dep

2.2 Array assignment

[root@linux1 ~]# array_dep=([name1]=zhaosi [name2]=zhangsan)

Separate assignment

[root@linux1 ~]# array_dep[name3]=wangwu
[root@linux1 ~]# array_dep[name4]=wangba

2.3 View all the values of the array

[root@linux1 ~]# echo ${array_dep[*]}
wangwu zhangsan zhaosi wangba
[root@linux1 ~]# echo ${array_dep[@]}
wangwu zhangsan zhaosi wangba

2.3 View Array Index Number

[root@linux1 ~]# echo ${!array_dep[@]}
name3 name2 name1 name4
[root@linux1 ~]# echo ${!array_dep[*]}
name3 name2 name1 name4

2.4 Statistical Array Length

[root@linux1 ~]# echo ${#array_dep[*]}
4
[root@linux1 ~]# echo ${#array_dep[@]}
4

3. Interception and Replacement of Array Elements

Similar to interception and substitution of variables

[root@linux1 ~]# age=(12 15 34 23 56 21)
[root@linux1 ~]# age0=${age[*]:2:2}     #Cut back two elements from the second element of the array (that is, 3423)
[root@linux1 ~]# echo ${age0[*]}
34 23
[root@linux1 ~]# age1=${age[*]/15/51}   #Replace 15 in the array with 51
[root@linux1 ~]# echo ${age1[*]}
12 51 34 23 56 21

Delete from left match and from match again

[root@linux1 ~]# array=(one two three foue five)
[root@linux1 ~]# array1=${array[*]#*o}      #The front of o has been deleted, leaving only the right side of O (the shortest match)
[root@linux1 ~]# array2=${array[*]##*o}     #The front of o has been deleted, leaving only the right side of O (longest match)
[root@linux1 ~]# array3=${array[*]%o}       #The last one is deleted, leaving only the front one (the shortest match)
[root@linux1 ~]# array4=${array[*]%o}       #The last one is deleted, leaving only the front one (the longest match)
[root@linux1 ~]# echo ${array1[*]}
ne three ue five
[root@linux1 ~]# echo ${array2[*]}
ne three ue five
[root@linux1 ~]# echo ${array3[*]}
one tw three foue five
[root@linux1 ~]# echo ${array4[*]}
one tw three foue five

IV. for loop traversal array

The first way of writing is:

for i in ${array[*]};do  
    echo $i
done

The second way of writing:

for i in ${!array[*]};do
    echo ${array[$i]}
done

The Third Writing Style: c Language Style

for ((i=0;i<${#array[*]};i++));do    # Traversing arrays in the number of array variables
    echo ${array[$i]}
done

Tags: Linux shell C

Posted on Tue, 27 Aug 2019 00:15:14 -0700 by juliston