Shell Scripting array


An array is a set of elements of the same data type arranged in a certain order. It is a set that names a limited number of variables of the same type, and then distinguishes their variables with numbers. This name is called the array name, and the numbers are called subscripts. The variables that make up the array are called the elements of the array. Array is a form in which several variables of the same type are organized in an orderly form for convenience in program design.

1、 Array representation

1. Subscript array

The subscript must be an integer. Format: array name [subscript]. The subscript starts from 0 by default.
The shell supports sparse arrays, and subscript integers do not need to be continuous.
Declaration method: declare – a array name

2. Associative array

Associative arrays can use any string as a subscript (not necessarily an integer) to access array elements. Bash 4.0 began to support associative arrays.
Format: array name [arbitrary string]
Declaration method: declare – a associative array
In addition, the shell only supports one-dimensional arrays and does not support multi-dimensional arrays.

2、 Assignment of array

3、 Access to arrays

Access to array elements is realized through operation syntax.

The syntax formats are as follows:

Length of array:

${#array [*]}: displays the number of elements;
${#array [@]}: displays the number of elements;
${#array [#]}: the number of characters of the # th element;
${#array}: the number of characters of the 0th element;

Elements of the array:

${array [*]}: display all element contents;
${array [@]}: display all element contents;
${array [#]}: displays the # th element;
${array}: displays the 0th element;

Key value of array:

$[! Array [*]}: display all key values;
$[! Array [@]}: displays all key values.

Example: write a script to generate 10 random numbers, save them to the array, and then display the elements with even subscripts in the array:

4、 Pick elements from an array

If you want to find some elements from the array, it is troublesome to use subscripts to find them one by one. You can use the offset element function of the array to simplify the operation.

Syntax format:

Offset: number of offset elements;
Number: the number of elements taken out;

Instance: array off_ There are six elements in the array. Suppose we want to take the three values from March to May:

5、 Array copy

[email protected]: each parameter is an independent string, which is recommended;
$*: all elements are treated as a string.

Instance: copy elements with even subscripts in an array to a new array:

6、 Remove element from array

Format: unset array name [subscript]

7、 Comprehensive example

Generate 10 random numbers and sort them.

Recommended Today

5、 Prometheus monitoring redis cluster monitoring (redis exporter)

5. Redis cluster monitoring for Prometheus monitoring (redis exporter) Now let’s use the previous monitoring routineredis-exporter(Note: This is not maintained by Prometheus.) change it: The redis exporter collects redis metrics data (such as uptime, number of commands executed per second, memory utilization, etc.) and exposes them. Most of the data comes from the redis info […]