SHELL script --shell array base

The difference between arrays and variables are: Variable occupied in the memory space is discrete, the array is to open up a contiguous large memory space in memory, then each element in the array into an array in memory. Array elements using array index logo.

bash, there are two kinds of arrays: general arrays and associative arrays. Ordinary arrays can only be used as an array index integer value, a string may be used as an associative array index. The so-called associative array, its three other title: dictionary (dict), hash structure and map (Map), and the key value is a relationship of one to one.

Ordinary arrays                            

 One way to define an array:     

[root@localhost ~]#  array_test=(1 2 3 4)

They are stored in bits 0-3 of the location index is array_test [0] to array_test [3] corresponding to the value. At this time array_test [0] is represented by a variable, so the reference to use $. Reference array: $ {array_name [index]}.

[root@localhost ~]# echo ${array_test[2]}
3

Note that the array is a space defined as a separator defined in parentheses, rather than the comma. If you use a comma, they will as a whole, that is, the array index value of zero. If you use a comma, then:

[root@localhost ~]# array_test=(1,2,3,4) 
[root@localhost ~]# echo ${array_test[0]}
1,2,3,4

Define an array of two: You can customize the index bits.

[root@localhost ~]# array_test1[1]=1
[root@localhost ~]# array_test1[2]=2
[root@localhost ~]# array_test1[3]=3
[root@localhost ~]# array_test1[4]=4
[root@localhost ~]# echo ${array_test1[*]}
1 2 3 4

But the definition array_test1 [7] = 7 after four index bits 5 and 6 represents an array variable undefined, i.e. does not exist, which can be verified by the number of elements of the statistical variables. However, in the shell is undefined variables can be referenced directly, but their initial values ​​or 0 is empty.

(1) Print an array of all values.

[root@localhost ~]# echo ${array_test1[*]}
1 2 3 4

Or use the @ symbol.

[root@localhost ~]# echo ${array_test1[@]}
1 2 3 4

(2) View array index number.

[root@localhost ~]# echo ${!array_test1[*]}
1 2 3 4
[root@localhost ~]# echo ${!array_test1[@]}
1 2 3 4

(3) the array length and variable length arrays.

[the root @ localhost ~] # echo $ {# array_test1 [ 1 ]} # display subscript character length of the array variable 1
 1 
[the root @ localhost ~] # echo $ {# array_test1 [* ]} # display array number (statistic not only empty element) element
 4 
[the root @ localhost ~] # echo $ {# array_test1 [@]} # the number of display elements in the array (only the statistical value is not null elements)
 4

Associative array                            

Associative array support strings as an array index. You must use an associative array using declare -A declare it.

[root@localhost ~]# declare -A array_dep
[root@localhost ~]# array_dep=([name1]=longshuai [name2]=xiaofang)

Name1 and name2 which is an associative array index. When referring to an array index requires the use of a reference value corresponding to the variable.

[root@localhost ~]# echo ${array_dep[name1]}
longshuai

It can be assigned separately.

[root@localhost ~]# array_dep[name3]=zhangsan
[root@localhost ~]#  array_dep[name4]=lisi
[root@localhost ~]# echo ${array_dep[name4]}
lisi

(1) View all array values.

[the root @ localhost ~] # echo $ {array_dep [@]} 
zhangsan Xiaofang longshuai Lisi
[the root @ localhost ~] # echo $ {array_dep [* ]} 
zhangsan Xiaofang longshuai Lisi

(2) View array index number.

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

(3) Statistics array length.

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

Taken array elements, replacing                        

And intercepting and replacement variables are similar.

[the root @ localhost ~] Array # = ( . 1  2  . 3  . 4  . 5  . 6 ) 
[the root @ localhost ~] = $ {# ARRAY0 Array [*]: 2 : 2 } # all elements from an array of two elements taken back two elements out (i.e.,. 3 . 4 ) 
[the root @ localhost ~] = $ {# array1 array [*] / 5 / 6 } # in the array to replace said 5 6

There are deleted from left to right match to match delete, and variables are the same.

[the root @ localhost ~] # = Array (Three One TWO foue Five) 
[the root @ localhost ~] = $ {# array1 Array [*] * # O #} and left to remove all non-greedy matching array variable matching content 
[ @ localhost the root ~] = $ {# array2 array [*] ## * O #} greedy match the left and delete the contents of all array variable matching 
[the root @ localhost ~] = $ {# array2 array [*]% O } # right and delete all non-greedy matching array variable matching content 
[the root @ localhost ~] = $ {# array2 array [*] %% # O} right greedy matching and delete all array variable matches

for loop through the array                          

In the cycle structure of the shell can be used to represent the entire array name array variables.

[root@localhost ~]# for i in ${array[*]};do   echo $i; done
one
two
three
foue
five

Or have i become the method of the array index:

[root@localhost ~]# for i in ${!array[*]};do
> echo $i
> done
0
1
2
3
4

Here are three common usage through the array summary:

Array = ($ ( LS / Boot)) 

for I in $ {Array [*]}; do   # manner array of values directly iterate
     echo $ I
 DONE 

for ((I = 0 ; I <$ {# Array [* ]}; I ++)); do     # manner number of array variable iterate
     echo $ {array [$ I]}
 DONE 

for I in $ {array [*]};! do       # an array index of the way to iterate
     echo Array {$ [$ I]}
 DONE

The following is an example of an array traversal: the number of statistics file duplicate rows. Assume a.log file reads as follows.

[root@localhost ~]# vi a.log
portmapper
portmapper
portmapper
portmapper
portmapper
portmapper
status
status
mountd
mountd
mountd
mountd
mountd
mountd
nfs
nfs
nfs_acl
nfs
nfs
nfs_acl
nlockmgr
nlockmgr
nlockmgr
nlockmgr
nlockmgr
nlockmgr

The following is a script array traversal.

[root@localhost ~]# vi shuzu.sh
#!/bin/bash

declare -A array_test  # 定义关联数组

for i in `cat ~/a.log`
do
    let ++array_test[$i]
done

for j in ${!array_test[*]}
do
    printf "%-15s %3s\n" $j :${array_test[$j]}
done

The script for the first cycle in order to make the contents of the file index of the array, each traversed from operations plus one index on a mathematical calculation of the index. This process is the process through the file contents and an array variable assignment.

This resulted in the following results:

array_test[status]=2
array_test[nfs]=4
array_test[portmapper]=6
array_test[nlockmgr]=6
array_test[nfs_acl]=2
array_test[mountd]=6

The second for loop array_test array is an array of all the variables and their values ​​taken out, this step is to iterate the process. So to get the final result:

[root@localhost ~]# bash shuzu.sh 
status           :2
nfs              :4
portmapper       :6
nlockmgr         :6
nfs_acl          :2
mountd           :6

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/liujunjun/p/11999968.html