The Linux shell is much more powerful in programming than Windows batch processing, both in loops and operations. Data types are not comparable. Here's a summary of some of the things that an individual does when it comes to working with arrays.
1. Array definitions
[Email protected] ~]$ a= (1 2 3 4 5)
[Email protected] ~]$ echo $a
1
A pair of parentheses indicates an array, and the elements of the array are separated by a "space" symbol.
2. Array reading and Assignment
Get Length:
[[email protected] ~]$ echo ${#a [@]}
5
Use ${#数组名 [@ or *]} to get the array length
Read:
[[email protected] ~]$ echo ${a[2]}
3
[[email protected] ~]$ echo ${a[*]}
1 2 3) 4 5
Using the ${array name [subscript]} subscript is starting from 0 subscript is: * or @ Get the entire array contents
Assignment value:
[Email protected] ~]$ a[1]=100
[[email protected] ~]$ echo ${a[*]}
1 100 3) 4 5
[Email protected] ~]$ a[5]=100
[[email protected] ~]$ echo ${a[*]}
1 100 3 4 5 100
It can be referenced directly by the array name [subscript], and if the subscript does not exist, a new array element is added automatically
Delete:
[Email protected] ~]$ a= (1 2 3 4 5)
[Email protected] ~]$ unset a
[[email protected] ~]$ echo ${a[*]}
[Email protected] ~]$ a= (1 2 3 4 5)
[Email protected] ~]$ unset a[1]
[[email protected] ~]$ echo ${a[*]}
1 3 4 5
[[email protected] ~]$ echo ${#a [*]}
4
Directly through: unset array [subscript] can clear the corresponding element, without subscript, clear the entire data.
3. Special use
Sharding:
[Email protected] ~]$ a= (1 2 3 4 5)
[[email protected] ~]$ echo ${a[@]:0:3}
1 2 3
[[email protected] ~]$ echo ${a[@]:1:4}
2 3 4 5
[[email protected] ~]$ c= (${a[@]:1:4})
[[email protected] ~]$ echo ${#c [@]}
4
[[email protected] ~]$ 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 "()", will get the slice array, the above example: C is a new data.
Replace:
[Email protected] ~]$ a= (1 2 3 4 5)
[[email protected] ~]$ echo ${a[@]/3/100}
1 2 100) 4 5
[[email protected] ~]$ echo ${a[@]}
1 2 3) 4 5
[[email protected] ~]$ a= (${a[@]/3/100})
[[email protected] ~]$ echo ${a[@]}
1 2 100) 4 5
The call method is: ${array name [@ or *]/find character/substitution character} This operation will not change the original array contents, if you need to modify, you can see the above example, redefine the data.
As you can see from the above, the array of Linux shells is already powerful, and the usual operations are more than enough.
Reprint http://www.cnblogs.com/chengmo/archive/2010/09/30/1839632.html
Shell reads and saves the array by row
Read IP from Ip.txt. Then assign the IP address to an array.
The IP files are as follows:
address:220.181.26.163
address:220.181.26.174
address:220.181.26.175
address:220.181.26.176
address:220.181.19.228
address:220.181.19.229
address:220.181.26.161
address:220.181.26.162
Method One:
For x in ' awk ' {print $} ' ip.txt '
{
Echo $x
}
Method Two:
Array= ($ (awk ' {print $} ' Ip.txt))
Method Three:
N=0;while read a b;do array[$n]= $b;((n++));d One
Method Four:
N=1
while ((n<=$ (cat ip.txt|wc-l)))
Do
ipaddr[$n]=$ (cat ip.txt|sed-n "${n}p" |awk ' {print $} ')
((n+=1))
Done
n= ' Expr $n-1 '
Practical examples of arrays
1. Read n string from "standard input", and each input string is saved in array
I=0
N=5
While ["$i"-lt $n]; Do
echo "Please input strings ... ' expr $i + 1 '"
Read array[$i]
b=${array[$i]}
echo "$b"
i= ' expr $i + 1 '
Done
2. Put the letters in the string into the array, and output to the "standard output"
chars= ' ABCDEFGHIJKLMNOPQRSTUVWXYZ '
for ((i=0; i<26; i++)); Do
array[$i]=${chars: $i: 1}
echo ${array[$i]}
Done
The interesting place here is ${chars: $i: 1}, which means to get 1 characters starting from the $i position of the chars string. If you change 1 to 3, you get 3 characters ~ The result is:
Abc
Bcd
...
Vxy
Xyz
YZ//There is not enough string to get the
z//There is not enough string to get the
3. Apply the array to the shell environment variable
3. Apply the array to the shell environment variable (1)
Array Assignment:
[[email protected] ~]# season= ("srping" "Summer" "Autumn" "Winter")
When you find that the assignment is wrong, you can immediately correct it from the new assignment, as the above Spring is written as srping.
Re-assignment: (The original value is overridden)
[[email protected] ~]# season= ("Spring" "Summer" "Autumn" "Winter")
Look at the environment variables:
[[Email protected] ~]# Set | grep SEASON
season= ([0]= "Spring" [1]= "Summer" [2]= "Autumn" [3]= "Winter")
Show the entire array:
[[email protected] ~]# echo ${season[*]} or echo ${season[@]}
Spring Summer Autumn Winter
To display an array element:
[[email protected] ~]# echo ${season[3]}
Winter
Assigning a value to a single array element:
[Email protected] ~]# season[0]= "new_spring"
Look at the array again:
[[email protected] ~]# echo ${season[*]}
New_spring Summer Autumn Winter
Clears the specified single array element:
[Email protected] ~]# unset season[2]
Clear the entire array:
[Email protected] ~]# unset SEASON
4. Apply the array to the shell environment variable (2) "This is a good use!" to the original author praise one! "
Use the TR command to convert a carriage return from a file into a space:
[Email protected] ~]# Cat/etc/shells | TR "\ n" "" >/tmp/tmp.file
Assigns the contents of a file to an array: (the content before the first carriage return)
[Email protected] ~]# read-a Shells </tmp/tmp.file
To view array assignment conditions:
[[Email protected] ~]# Set | grep "Shells"
shells= ([0]= "/bin/sh" [1]= "/bin/bash" [2]= "/sbin/nologin" [3]= "/bin/tcsh" [4]= "/bin/csh" [5]= "/bin/ksh")
You can apply this array environment variable to other shell scripts or applications later.
Linux shell array creation and usage tips