In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "the use of arrays in Bash Shell scripts". In daily operation, I believe many people have doubts about the use of arrays in Bash Shell scripts. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about the use of arrays in Bash Shell scripts. Next, please follow the editor to study!
An array is a variable that contains multiple values, which can be of the same type or different types. There is no limit to the size of the array, and there is no limit to requiring member variables to be continuously indexed or allocated. The array index starts at 0.
1. Declare an array and assign a value
In bash, an array is automatically created when using variables in the following format:
Name [index] = value
Name is the name of the array.
Index can be any number or expression, and the value must be equal to or greater than zero.
To access array elements, use curly braces, such as ${name [index]}. Here is the second element in the Unix array accessed, assuming that the array index starts at 0, so Unix [1] is the second element.
[root@localhost ~] # cat arraymanip.sh #! / bin/bash Unix [0] = 'Debian' Unix [1] =' Red hat' Unix [2] = 'Ubuntu' Unix [3] =' Suse' echo ${Unix [1]}
Execute the script, and here is the output:
[root@localhost ~] #. / arraymain.sh Red hat
two。 Initialize an array during a declaration
Instead of initializing each element of the array individually, you can declare and initialize the array by specifying a list of elements separated by spaces using parentheses. Here is the syntax:
Declare-an arrayname= (element1 element2 element3)
If the element has space characters, you need to use quotation marks:
[root@localhost ~] # cat arraymain2.sh #! / bin/bash declare-a Unix= ('Debian'' Red hat' 'Ubuntu'' Suse' 'Fedora') echo ${Unix [4]}
Here is the output:
[root@localhost ~] #. / arraymain2.sh Fedora
Declare-a declares an array, and all the elements in parentheses are elements of the array.
3. Print the entire array
There are several ways to print an entire array. If the index number is @ or *, all members of the array are referenced. You can use the loop statement in bash shell to traverse the array elements and print them.
[root@localhost ~] # cat arraymain.sh #! / bin/bash Unix [0] = 'Debian' Unix [1] =' Red hat' Unix [2] = 'Ubuntu' Unix [3] =' Suse' echo ${Unix [@]}
Here is the output:
[root@localhost ~] #. / arraymain.sh Debian Red hat Ubuntu Suse
4. Get the length of the array
You can use the special parameters of $and # to get the length of the array. ${# arrayname [@]} can get the array length.
[root@localhost ~] # cat arraymain2.sh #! / bin/bash declare-a Unix= ('Debian'' Red hat' 'Ubuntu'' Suse' 'Fedora') echo ${# Unix [@]} # number of elements in the array echo ${# Unix} # the number of characters of the first element in the array.
Here is the output, and you can see that the first line of output parameters is 5. The second line outputs the number of characters of the first element is 6.
[root@localhost] #. / arraymain2.sh 5 6
5. The length of an element in the array [root@localhost ~] # cat arraymain.sh #! / bin/bash Unix [0] = 'Debian' Unix [1] =' Red hat' Unix [2] = 'Ubuntu' Unix [3] =' Suse' echo ${# Unix [3]} # the length of the element at index 3.
Here is the output, and you can see that the character length of "Suse" with an index of 3 is 4.
[root@localhost ~] #. / arraymain.sh 4
6. Extract by offset and length of the array
The following example shows how to extract two elements from an array named Unix, starting at index position 3.
[root@localhost ~] # cat arraymain.sh #! / bin/bash Unix= ('Debian'' Red hat' 'Ubuntu'' Suse' 'Fedora'' UTS' 'OpenLinux') echo ${Unix [@]: 3:2}
Here is the output
[root@localhost ~] #. / arraymain.sh Suse Fedora
The above example returns the values of the third and fourth indexes. The index always starts with zero.
7. For specific elements of an array, extract using offset and length
Extract only the first four elements from the array elements. For example, get the second element in the array and get the first three characters of this element:
[root@localhost ~] # cat arraymain.sh #! / bin/bash Unix= ('Debian'' Red hat' 'Ubuntu'' Suse' 'Fedora'' UTS' 'OpenLinux') echo ${Unix [1]: 0:3}
The following is the output:
[root@localhost ~] #. / arraymain.sh Red
8. Search for and replace array elements
The following example searches an array for Ubuntu and replaces it with the word "FreeBSD".
[root@localhost ~] # cat arraymain.sh #! / bin/bash Unix= ('Debian'' Red hat' 'Ubuntu'' Suse' 'Fedora'' UTS' 'OpenLinux') echo ${Unix [@] / Ubuntu/FreeBSD}
The following is the output:
[root@localhost ~] #. / arraymain.sh Debian Red hat FreeBSD Suse Fedora UTS OpenLinux [root@localhost ~] #
Note that the array substitution is not written to the array.
9. Add elements to an existing array
The following example shows how to add an element to an existing array.
[root@localhost ~] # cat arraymain.sh #! / bin/bash Unix= ('Debian'' Red hat' 'Ubuntu'' Suse' 'Fedora'' UTS' 'OpenLinux') Unix= ("${Unix [@]}"AIX"HP-UX") echo ${Unix [@]}
The following is the output:
[root@localhost ~] #. / arraymain.sh Debian Red hat Ubuntu Suse Fedora UTS OpenLinux AIX HP-UX
In the array named Unix, the elements "AIX" and "HP-UX" are added to the 7th and 8th indexes, respectively. And output all array elements.
10. Delete an element from the array
Unset is used to remove elements from an array. Unset has the same effect as assigning a value to the element "Null".
[root@localhost ~] # cat arraymain.sh #! / bin/bash Unix= ('Debian'' Red hat' 'Ubuntu'' Suse' 'Fedora'' UTS' 'OpenLinux') unset Unix [1] echo ${Unix [@]} echo ${Unix [1]}
The following is the output:
[root@localhost ~] #. / arraymain.sh Debian Ubuntu Suse Fedora UTS OpenLinux
The above script will print the entire array, as well as a value with an index of "1" and a value with an index of "1" is null.
The following example shows one way to completely delete an element from an array, and here is to delete an element with an index of 1.
[root@localhost ~] # cat arraymain.sh #! / bin/bash Unix= ('Debian'' Red hat' 'Ubuntu'' Suse' 'Fedora'' UTS' 'OpenLinux') pos=1 Unix= (${Unix [@]: 0:$pos} ${Unix [@]: $($pos + 1)) echo ${Unix [@]}
The following is the output:
[root@localhost ~] #. / arraymain.sh Debian Ubuntu Suse Fedora UTS OpenLinux
In this example, the value of ${Unix [@]: 0:$pos} gets the element of the first index, while ${Unix [@]: $(($pos + 1))} will go from the third index to the last index. And merge the above two outputs. This is one of the solutions for removing elements from an array.
11. Use regular expressions to delete elements from an array
In the search criteria, you can give a regular expression and store the remaining elements in another array, as shown below.
[root@localhost ~] # cat arraymain2.sh #! / bin/bash declare-a Unix= ('Debian'' Red hat' 'Ubuntu'' Suse' 'Fedora') declare-a pattern= (${Unix [@] / Red*/}) echo ${pattern [@]}
The following is the output:
[root@localhost ~] #. / arraymain2.sh Debian Ubuntu Suse Fedora
The element containing the "Red" character is removed from the above example. You actually replace "Red*" with an empty character.
twelve。 Copy array
The following example is to copy the Unix array to the Linux array:
[root@localhost ~] # cat arraymain.sh #! / bin/bash Unix= ('Debian'' Red hat' 'Ubuntu'' Suse' 'Fedora'' UTS' 'OpenLinux') Linux= ("${Unix [@]}") echo ${Linux [@]}
The following is the output:
[root@localhost ~] #. / arraymain.sh Debian Red hat Ubuntu Suse Fedora UTS OpenLinux
13. The association of two arrays
Expand the elements of the two arrays and assign them to the new array:
[root@localhost ~] # cat arraymain.sh #! / bin/bash Unix= ('Debian'' Red hat' 'Ubuntu'' Suse' 'Fedora'' UTS' 'OpenLinux'); Shell= (' bash' 'csh'' jsh' 'rsh'' ksh' 'rc'' tcsh'); UnixShell= ("${Unix [@]}" ${Shell [@]} ") echo ${UnixShell [@]} echo ${# UnixShell [@]}
The following is the output:
[root@localhost ~] #. / arraymain.sh Debian Red hat Ubuntu Suse Fedora UTS OpenLinux bash csh jsh rsh ksh rc tcsh 14
This instance prints the elements of both the array "Unix" and the "Shell" array, and the number of elements of the new array is 14.
14. Delete the entire array
Unset is used to delete the entire array.
[root@localhost ~] # cat arraymain.sh #! / bin/bash Unix= ('Debian'' Red hat' 'Ubuntu'' Suse' 'Fedora'' UTS' 'OpenLinux'); Shell= (' bash' 'csh'' jsh' 'rsh'' ksh' 'rc'' tcsh'); UnixShell= ("${Unix [@]}" ${Shell [@]} ") unset UnixShell echo ${# UnixShell [@]}
The following is the output:
[root@localhost ~] #. / arraymain.sh 0
After the unset array, its length will be zero, as shown above.
15. Load the contents of the file into an array
You can load the contents of the file into an array line by line.
[root@localhost ~] # cat loadcontent.sh #! / bin/bash file= (`file. / pic.txt`) for i in "${file [@]}" do echo $i done echo-e "\ 033 [31m Read file content!\ 033 [0m"
The following is the output:
[root@localhost ~] #. / loadcontent.sh https://www.linuxprobe.com/wp-content/uploads/2021/01/windows7.png https://www.linuxprobe.com/wp-content/uploads/2016/12/bigdata.jpg https://www.linuxprobe.com/wp-content/uploads/2021/01/write-games-and-learn-python.jpg https://www.linuxprobe.com/wp-content/uploads/2021/01/data-center-inspection.jpg https:/ / www.linuxprobe.com/wp-content/uploads/2020/03/devolop-like-linux-09.jpg Read file content!
In the above example, a for loop is used to display the contents of each line in the file one by one.
At this point, the study on "how to use arrays in Bash Shell scripts" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.