Managing and Using Arrays in Shell Scripts (page 2)

botond published 2019. 03. 11., h - 17:09 time

2. page content

 

Continuation

The description on the first page we looked at how to create indexed arrays and how to write data to or read from it. On this page, we continue to get acquainted with indexed arrays by using arrays in branches and functions.

 

 

Indexed arrays

Using arrays in junctions

There are many ways to use arrays in junctions.

Logical examination

The logical examination checks for the presence of an element of the array with a given index, which happens as follows:



Here are three examples of the study. The first two are the single-line test, where we simply print out the appropriate result, and then a multi-line version, where we can place additional lines of code in the branches.

The logical reversal is a ! can also be used in the multi-line version if, for example, we only need the false branch during verification. At that time it is else without a section, you can make a shorter code.

Value testing

When examining a value, we compare the value of a given element of the array with something:



Here, too, we see examples of value checks. And it can be varied in many ways. (You can copy and run the example program one-by-one in the shell)

In character-based tests, it is easy to slip into error, as figures can be compared according to the deceptive example above. However, he still compares the two pages as strings, which in many cases gives a mathematically incorrect result, which is still correct for the strings.
I only added this for the sake of clarity, that when performing tests with arrays (or any other variables), make sure to compare the types of values ​​contained in them properly.

Using Arrays in Functions

You can also pass one or more arrays to functions as parameters, which you can then use in the function.

Manage parameters as an array

There are cases when it is necessary to collect all the parameters of a function into a single array. These can include custom values ​​or even arrays that contain additional value fields. The following example illustrates how this is done:



The output of the sample program is:

 0:           1
 1:           2
 2:           3
 3:         100
 4:         200
 5:         500
 6:        1000
 7:         150
 8:         350
 9:         220
10:           4
11:           5
12:           6

It can be seen here that all the mixed parameters are included in the array used in the function.

The solution is based on calling the function between the given parameters when calling the function "$ {Array [@]}" we give it in the form, which expands all the elements of the array in the way we know before (also working with multi-word elements), that is, as if we had given them to the function one by one. Then in the function a $@ parameters are loaded into an array.

This method can come in handy, for example, when a function needs to be built without knowing the number of parameters obtained in advance, but we need to be able to process them.

For the sake of practice, in the following example program, the function also collects its numerical parameters into an array and then calculates their sum, average, and determines their minimum and maximum values:



The output of this is:

Tömb elemek:
--------------
 0:         100
 1:         200
 2:         500
 3:        1000
 4:         150
 5:         350
 6:         220
 7:           1
 8:           2
 9:           3
---------------
Összeg:    2526
Átlag:   252.60
Minimum:      1
Maximum:   1000
---------------

Here, too, we can observe that whether we assign individual numeric values ​​to the function or an array, as well as the order of their choice, the function processes all the numeric values ​​obtained.

With this solution we can dynamically manage the obtained parameters, no matter their quantity, but there are also limitations: we cannot handle the array parameters separately, so we can only use them for common tasks in the function. Therefore, a solution may be needed where the resulting array parameters are processed separately.

 

 

Manage multiple array parameters separately

It is also possible within the function to handle the resulting array parameters separately, thus providing other options, configuration commands, etc., in addition to the data parameters given to the function. The following example shows:



And the output is:

-----------------------------
1. Paramétertömb kilistázása:
-----------------------------
0: 100
1: 200
2: 500
3: több szavas
4: másik három szavas

-----------------------------
2. Paramétertömb kilistázása:
-----------------------------
0: másik tömb
1: 2000
2: ebben is vannak több szavas elemek
3: 2500
4: 3000
5: a-b c

-----------------------------
Egyéb paraméterek kiírása:   
-----------------------------
3. paraméter: beállítás 1
4. paraméter: beállítás 2

The essence of this method is that in the parameters of the function, the arrays were passed by name, not by their extracted values. Then, in rows 12 and 13 in the function, we create the internal variables based on the names obtained.

The disadvantage of this approach, however, is that here we have to work with a fixed parameter sequence in the function, so as we encode it into the algorithm, the parameters must also arrive in that order, otherwise errors will occur. Sure, there are ways to go through all the parameters and examine their type, but that's another story ...

Deleting array elements and arrays

To delete an item, use the following syntax:

unset tomb[4]

In this case, without the dollar sign and the parentheses, we refer to the variable as a store, not to the value in it.

And to delete the whole array, enter:

unset tomb

Here, similarly, we refer to the whole array without the dollar sign and brace.

 

next page we continue to learn about associative arrays ...

 

 

Navigation

This description consists of several pages: