Bash shell supports only one-dimensional arrays, but there is no limit on the number of parameters.
Declare an array:
Declare-a array
(In fact, you don't need to declare it. You just need to assign values to variables in the array mode. BASH knows that it is an array)
Array assignment:
(1) array = (var1 var2 var3... varN)
(2) array = ([0] = var1 [1] = var2 [2] = var3... [n] = varN)
(3) array [0] = var1
Arrya [1] = var2
...
Array [n] = varN
Calculate the number of array elements:
$ {# Array [@]} or $ {# array [*]}
BASH's special parameters @ and * both represent "extended location parameters, starting from 1", but the form is slightly different, but it seems that they can be used in arrays.
Reference array:
Copy codeThe Code is as follows: echo $ {array [n]}
Traverse the array:
Copy codeThe Code is as follows: filename = ('LS ')
For var in $ {filename [@]}; do
Echo $ var
Done
The following is a small example of shell array.
1. Read the string n times from "standard input", and each input string is saved in the array
Copy codeThe Code is as follows :#! /Bin/bash
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 an array one by one and output them to "standard output"
Copy codeThe Code is as follows :#! /Bin/bash
Chars = 'abcdefghijklmnopqrstuvwxy'
For (I = 0; I <26; I ++); do
Array [$ I] =$ {chars: $ I: 1}
Echo $ {array [$ I]}
Done
The interesting part here is $ {chars: $ I: 1}, which indicates to get 1 character from the $ I position of the chars string. If you change 1 to 3, you can get 3 characters ~ The result is:
Abc
Bcd
...
Vxy
Xyz
Yz // There are not enough strings to get
Z // There are not enough strings to get
The following describes some examples of applying arrays to shell environment variables.
3. Apply the array to the shell environment variable (1)
Copy codeThe Code is as follows: array assignment:
[Root @ pps ~] # SEASON = ("Srping" "Summer" "Autumn" "Winter ")
When you find that the assignment is wrong, you can immediately correct the assignment. The above Spring is written as Srping.
Re-assign values: (the original value is overwritten)
[Root @ pps ~] # SEASON = ("Spring" "Summer" "Autumn" "Winter ")
Check the environment variables:
[Root @ pps ~] # Set | grep SEASON
SEASON = ([0] = "Spring" [1] = "Summer" [2] = "Autumn" [3] = "Winter ")
Display the entire array:
[Root @ pps ~] # Echo $ {SEASON [*]} or echo $ {SEASON [@]}
Spring Summer Autumn Winter
Display an array element:
[Root @ pps ~] # Echo $ {SEASON [3]}
Winter
Assign values to an array element:
[Root @ pps ~] # SEASON [0] = "New_Spring"
Check the array again:
[Root @ pps ~] # Echo $ {SEASON [*]}
New_Spring Summer Autumn Winter
Clear a single array element:
[Root @ pps ~] # Unset SEASON [2]
Clear the entire array:
[Root @ pps ~] # Unset SEASON
4. Apply the array to the shell environment variable (2)
Use the tr command to convert the carriage return in the file to a space:
Copy codeThe Code is as follows: [root @ pps ~] # Cat/etc/shells | tr "\ n" ">/tmp. file
Assign values to the array in the file: (content before the first carriage return)Copy codeThe Code is as follows: [root @ pps ~] # Read-a SHELLS </tmp. file
View the array assignment:Copy codeThe Code is as follows: [root @ pps ~] # 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.
Conclusion: With the above content, in shell programming, the array should not be a problem. Have you done it ?!