In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to understand Shell script array". In daily operation, I believe many people have doubts about how to understand Shell script array. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "how to understand Shell script array". Next, please follow the editor to study!
Linux Shell is much more powerful in programming than Windows batches, whether in loops or operations.
Bash supports one-dimensional arrays (not multidimensional arrays) and does not limit the size of the array. Similar to the C language, the subscripts of array elements are numbered starting with 0. The subscript is used to get the elements in the array, which can be an integer or an arithmetic expression whose value should be greater than or equal to 0.
Define array
In Shell, arrays are represented by parentheses, and array elements are separated by a "space" symbol. The general form of defining an array is:
The code is as follows:
Array name = (value 1, value 2. Value n)
For example:
The code is as follows:
Array_name= (value0 value1 value2 value3)
Or
The code is as follows:
Array_name= (
Value0
Value1
Value2
Value3
)
You can also define individual components of an array:
The code is as follows:
Array_name [0] = value0
Array_name [1] = value1
Array_ name [n] = valuen
There can be no use of consecutive subscripts, and there is no limit to the range of subscripts.
Read array
The general format for reading array element values is:
The code is as follows:
${Array name [subscript]}
For example:
The code is as follows:
Valuen=$ {array_ name[n]}
Use the @ symbol to get all the elements in the array, such as:
The code is as follows:
Echo ${array_name [@]}
Get the length of the array
Get the length of an array in the same way as you get the length of a string, for example:
The code is as follows:
# get the number of elements in the array
Length=$ {# array_name [@]}
# or
Length=$ {# array_name [*]}
# get the length of a single element in the array
Lengthn=$ {# array_ name[n]}
Attached: summary of shell array
I don't know when I wrote it, but it was found by archaeology when I was sorting out the documents. For those who are idle and sore, laugh it off. If the mistakes in this article bring you all the mental loss, please go to the insurance company to accompany! Of course you can tell me (confide)
As a special data structure, array has its place in any programming language, and bash shell is no exception. This article makes a small summary on the shell array.
Here we only talk about one-dimensional arrays, not multi-dimensional arrays (in fact, you have to simulate them with one-dimensional arrays). This includes copying, calculating, deleting, and replacing arrays.
Array declaration:
The code is as follows:
Array [key] = value # array [0] = one,array [1] = two
Declare-an array # array is used as an array name
Array= (value1 value2 value3...)
Array= ([1] = one [2] = two [3] = three...)
Array= "one two three" # echo ${array [0 | @ | *]} treats the array variable as an array, but the array element is only the string itself
Array access:
The code is as follows:
${array [key]} # ${array [1]}
Deletion of array
The code is as follows:
Unset array [1] # Delete the first element in the array
Unset array # Delete the entire array
Calculate the length of the array:
The code is as follows:
${# array}
${# array [0]} # Ibid. ${# array [*]}, ${# array [@]} Note the difference with # {array:0}
Extraction of array
Extract from the tail:
The code is as follows:
Array= ([0] = one [1] = two [2] = three [3] = four)
${array [@]: 1} # two three four, all elements after the first element is removed, then ${array [@]: 0} represents all elements
${array [@]: 0:2} # one two
${array [@]: 1:2} # two three
Substring deletion
The code is as follows:
[root@localhost dev] # echo ${array [@]: 0}
One two three four
[root@localhost dev] # echo ${array [@] # tweee} # the shortest match begins on the left: "tweee", which will match to "thre"
One two e four
[root@localhost dev] # echo ${array [@] # # tweee} # the longest match begins on the left, which will match to "three"
[root@localhost dev] # array= ([0] = one [1] = two [2] = three [3] = four)
[root@localhost dev] # echo ${array [@]% o} # shortest match starting at the end of the string
One tw three four
[root@localhost dev] # echo ${array [@]% o} # longest match starting at the end of the string
One tw three four
Substring substitution
The code is as follows:
[root@localhost dev] # array= ([0] = one [1] = two [2] = three [3] = four)
The first match will be deleted.
The code is as follows:
[root@localhost dev] # echo ${array [@] / obox}
Mne twm three fmur
All matches will be deleted.
The code is as follows:
[root@localhost dev] # echo ${array [@] / / obox}
Mne twm three fmur
If no replacement substring is specified, the matching substring is deleted
The code is as follows:
[root@localhost dev] # echo ${array [@] / / o /}
Ne tw three fur
Replace the front terminal string of the string
The code is as follows:
[root@localhost dev] # echo ${array [@] / # oplink}
Kne two three four
Replace the string after the terminal string
The code is as follows:
[root@localhost dev] # echo ${array [@] /% oplink}
One twk three four
At this point, the study on "how to understand the Shell script array" 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.