The bash shell supports only one-dimensional arrays, but there are no restrictions on the number of arguments.
Declare an array:
Declare-a Array
(In fact, you don't have to declare that you can assign a value directly to a variable in an array, and bash knows that it's 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
To calculate the number of array elements:
${#array [@]} or ${#array [*]}
Bash's special parameters @ and * All represent the "extended positional argument, starting at 1", but the form is slightly different, but it seems to be common to use in arrays.
Referencing arrays:
Copy Code code as follows:
To traverse an array:
Copy Code code as follows:
Filename= (' ls ')
For Var in ${filename[@]};d o
Echo $var
Done
Here are some small examples of shell arrays.
1, from "standard input" read n times string, each input string stored in array
Copy Code code as follows:
#!/bin/bash
I=0
N=5
While ["$i"-lt $n]; Todo
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"
Copy Code code as follows:
#!/bin/bash
chars= ' ABCDEFGHIJKLMNOPQRSTUVWXYZ '
for ((i=0; i<26; i++)); Todo
array[$i]=${chars: $i: 1}
echo ${array[$i]}
Done
What's interesting 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//Not enough string to get the
Here are some examples of applying arrays to shell environment variables.
3. Apply the array to the shell environment variable (1)
Copy Code code as follows:
Array Assignment:
[Root@pps ~]# season= ("srping" "Summer" "Autumn" "Winter")
When you find that the assignment is wrong, you can also immediately correct the new assignment, as Spring above is written as srping.
Re-assignment: (The original value is overridden)
[Root@pps ~]# season= ("Spring" "Summer" "Autumn" "Winter")
Check out 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
To display an array element:
[Root@pps ~]# echo ${season[3]}
Winter
To assign a value to a single array element:
[Root@pps ~]# season[0]= "New_spring"
Look at the array again:
[Root@pps ~]# echo ${season[*]}
New_spring Summer Autumn Winter
Clears the specified 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)
To convert a carriage return in a file to a space using the TR command:
Copy Code code as follows:
[Root@pps ~]# Cat/etc/shells | TR "\ n" "" ">/tmp/tmp.file
To assign the contents of a file to an array: (before the first return character is encountered)
Copy Code code as follows:
[Root@pps ~]# read-a SHELLS </tmp/tmp.file
To view array assignments:
Copy Code code 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 later apply this array environment variable to other shell scripts or applications.
Summary: With these content, later in the Shell programming, the logarithmic group, should not be what the problem, you did it?!