Linux shell array and linuxshell Array
Introduction
Working on the Linux platform, we often need to use shell to write some useful and meaningful script programs. Sometimes we often use shell arrays. So how is the array in shell defined? Next we will explain one by one, the array in shell.
Array Definition
What is an array? All those who have studied computer programming languages know that the array feature is a set of identical data types (excluding the associated array concept proposed by some programming languages ). How is the array defined in shell? Let's look at two data types: numerical and string. Although shell itself is weak, however, this can also be distinguished.
Numeric array:A pair of parentheses indicates an array. Elements in the array are separated by spaces.
Give a column:
Arr_number = (1 2 3 4 5 );
String type array:Similarly, an array is represented by a pair of parentheses. Elements in the array are enclosed by double quotation marks or single quotation marks, and are separated by spaces.
Arr_string = ("abc" "edf" "sss"); or arr_string = ('abc' 'def' sss ');
Array Operations
We use the value array arr_number = (1 2 3 4 5) as the source array for explanation: Get the length of the array, read the value of a certain subscript, assign a value to a certain subscript, delete, assign, replace, and traverse. Here is a knowledge point. We want to get the value of a variable in shell and start with the $ character, for example, $ a or $ {.
Get array Length
Arr_length = $ {# arr_number [*]} or $ {# arr_number [@]} can be in the following format: $ {# array name [@/*]} can get the length of the array.
Reads the value of a subobject.
Arr_index2 =$ {arr_number [2]}, in the form of $ {array name [subscript]}
Assign a value to a subscript
Here we need to raise two questions:
The first problem isWhat if the subscript element already exists?
A:The value of this submark is changed to a new specified value.
For example, if arr_number [2] = 100, the array is modified to (1 2 100 4 5)
The second problem isWhat if the specified subscript exceeds the size of the current array, for example, if the size of arr_number is 5 and the subscript is 10, 11, or greater than 5?
A:The newly assigned value is appended to the end of the array.
For example: arr_number [13] = 13, the array is modified to (1 2 100 4 5 13)
Delete operation
Clear an element: unset arr_number [1]. Here, the array with the subscript of 1 is cleared;
Clear the entire array: unset arr_number;
Multipart access
Multipart access is in the form of $ {array name [@ or *]: Start Subscript: end subscript}. Note that the value of the end subscript element is not included.
For example: $ {arr_number [@]: 1: 4}. Here, the results of the multipart access start subscript from 1 to 4 are output as a new array, it does not include values with the subscript 4.
Mode replacement
Format: $ {array name [@ or *]/mode/New Value}
Example: $ {arr_number [@]/2/98}
Array Traversal
We use the for statement to demonstrate array traversal:
For v in $ {arr_number [@]}; do
Echo $ v;
Done
Conclusion
Through the above, we have explained the definition of an array in shell. We have obtained the length of an array, read a tag value, assign values to a subscript, delete, assign, replace, and traverse it. Through the above explanation, I hope to help readers use shell on the Linux platform. Thanks for your patience.