The Linux shell is much more powerful in programming than in Windows batching, whether in loops or operations. There is no comparison of data types. The following is a summary of how an individual operates on an array while in use.
1. Array definition
[Chengmo@centos5 ~]$ a= (1 2 3 4 5)
[Chengmo@centos5 ~]$ Echo $a
A pair of parentheses is an array, and the array elements are separated by a "space" symbol.
2. Array reading and Assignment
Get the length:
[Chengmo@centos5 ~]$ echo ${#a [@]}
5
Array length can be obtained with ${#数组名 [@ or *]}
Read:
[Chengmo@centos5 ~]$ Echo ${a[2]}
3
[chengmo@centos5 ~]$ echo ${a[*]}
1 2 3 4 5
With ${array name [subscript]} subscript is starting from 0 subscript is: * or @ Get the whole array content
Assign value:
[Chengmo@centos5 ~]$ a[1]=100
[chengmo@centos5 ~]$ echo ${a[*]}
1 3 4 5
[chengmo@centos5 ~]$ a[5]=100
[chengmo@centos5 ~]$ Echo ${a[*]}
1 100 3 4 5 100
It can be assigned directly through the array name [subscript], and if the subscript does not exist, a new array element is automatically added
Delete:
[Chengmo@centos5 ~]$ a= (1 2 3 4 5)
[chengmo@centos5 ~]$ unset a
[chengmo@centos5 ~]$ echo ${a[*]}
[Chengmo@ce Ntos5 ~]$ a= (1 2 3 4 5)
[chengmo@centos5 ~]$ unset a[1]
[chengmo@centos5 ~]$ echo ${a[*]}
1 3 4 5
[che Ngmo@centos5 ~]$ echo ${#a [*]}
4
Direct through: unset array [subscript] You can clear the corresponding elements, without the subscript, clear the entire data.
3. Special use
Sharding:
[Chengmo@centos5 ~]$ a= (1 2 3 4 5)
[Chengmo@centos5 ~]$ Echo ${a[@]:0:3}
1 2 3
[Chengmo@centos5 ~]$ Echo ${a[@ ]:1:4}
2 3 4 5
[chengmo@centos5 ~]$ c= (${a[@]:1:4})
[Chengmo@centos5 ~]$ echo ${#c [@]}
4
[ Chengmo@centos5 ~]$ Echo ${c[*]}
2 3 4 5
Directly through the ${array name [@ or *]: Start position: Length} Slice the original array, return is a string, the middle with "space" separate, so if you add "()", you will get the slice array, above example: C is a new data.
Replace:
[Chengmo@centos5 ~]$ a= (1 2 3 4 5)
[Chengmo@centos5 ~]$ Echo ${a[@]/3/100}
1 2 4
[chengmo@centos5 ~]$] echo ${a[@]}
1 2 3 4 5
[chengmo@centos5 ~]$ a= (${a[@]/3/100})
[Chengmo@centos5 ~]$ echo ${a[@]}
1 2 1 00 4 5
Call method is: ${array name [@ or *]/lookup character/substitution character} This operation does not change the original array contents, if you need to modify, you can look at the example above to redefine the data.
From the above, you can find that the Linux shell array is already very powerful, the common operation is more than sufficient.