You get a bonus - 1 coin for daily activity. Now you have 1 coin

22 - bash. Cycles and functions

Lecture



In this lecture, we continue to get acquainted with bash . I want to remind you that we are considering those elements of bash that will help us understand the scripts of the operating system. These elements are certainly cycles and functions. If someone has studied programming, then there will be no difficulty in understanding these issues.

Cycle for

The bash for loop has two types. Consider first the classic version for . The general view is as follows:

one
2
3
four

for variable in sequence of values
do
teams
done

Between the for and in elements, a variable is set, which in turn takes a value from a sequence of values ​​specified between in and do . Between do and done, there are commands that are executed every time the variable changes its value. The loop stops running when the variable takes the last value from the sequence. Values ​​in the sequence are separated by spaces.

Here is a practical example:

one
2
3
four
five

#! / bin / bash
for i in 1 2 3 abc
do
echo i = $ i
done

As you remember from the last lecture, variables in bash are of no type, so there can be both numbers and strings or characters in the sequence.

If we run such a script for execution, we get the following result:

one
2
3
four
five
6
7

igor @ ubuntu: ~ / linux $ ./testfor.sh
i = 1
i = 2
i = 3
i = a
i = b
i = c

The sequence of values ​​can be specified in different ways. Explicitly - as in the example above, or with the help of other variables, or with the help of special commands. Consider some examples. Since the values ​​are separated by spaces, there can be any variable that contains a string with spaces:

one
2
3
four
five
6

#! / bin / bash
S = "1 2 3 abc"
for i in $ s
do
echo i = $ i
done

The result will be the same as in the first example.

If you need to specify a sequence of numbers, you can use the com *** th seq and substitution mechanism. Com *** and seq returns to the screen a sequence of numeric values. The syntax is simple and will be clear from the example below:

one
2
3
four
five
6
7
eight
9
ten
eleven
12
13
14
15

igor @ ubuntu: ~ / linux $ seq 6
one
2
3
four
five
6
igor @ ubuntu: ~ / linux $ seq 4 6
four
five
6
igor @ ubuntu: ~ / linux $ seq 1 2 6
one
3
five

Using the substitution mechanism, namely the construction of $ (), we obtain the following example:

one
2
3
four
five

#! / bin / bash
for i in $ (seq 2 2 10)
do
echo i = $ i
done

Result:

one
2
3
four
five
6

igor @ ubuntu: ~ / linux $ ./testfor.sh
i = 2
i = 4
i = 6
i = 8
i = 10

Here I want to remind you of double quotes. If we take the construction of $ (seq 2 2 10) in double quotes, then the result returned by a *** n seq 2 2 10 , namely 2 4 6 8 10, will be interpreted as one element:

one
2

igor @ ubuntu: ~ / linux $ ./testfor.sh
i = 2 4 6 8 10

Let's return to the second type for . Often in the scripts you can find the so-called C-like variant for , which is used for cycles based on numbers. Consider immediately an example:

one
2
3
four
five

#! / bin / bash
for ((i = 1; i <6; i ++))
do
echo i = $ i
done

As you can see, the i in $ (seq 5) construction in this example has been replaced by ((i = 1; i <6; i ++)) . Which way to choose is up to you.

While loop

General form:

one
2
3
four

while expression
do
teams
done

The loop is executed while the condition being checked in the expression is true. As soon as the expression returns false, the loop is terminated.

Practical example:

#! / bin / bash
i = 1
while [$ i -lt 7]
do
echo $ i
let i = i + 1
done

In our example, it is checked that the variable i is less (-lt), the number 7, and if so, the value of the variable is displayed on the screen. The expression let i = i + 1 , increases the variable by one, checks again, and so on. let tells the interpreter that arguments should be recognized as numeric values. This string could be written as let i ++ (c-like variant). If you increase the number by more than one, you can write this: let i + = 2 - in this case, i will increase in increments of 2. Another option to increase the variable is to use the built-in calculator (it works only with integers). The calculator can be accessed through double brackets: i = $ (($ i + 1)) or square: i = $ [$ i + 1] You can also use the calculator on the command line:

one
2
3
four
five
6

igor @ ubuntu: ~ / linux $ echo $ ((23 * 345))
7935
igor @ ubuntu: ~ / linux $ echo $ ((345/5))
69
igor @ ubuntu: ~ / linux $ echo $ ((69 * 5))
345

Until cycle

It looks like while with the only difference that in it the commands inside the loop are executed when the condition is not met. The syntax is the same, only until is used until . Example:

one
2
3
four
five
6
7

#! / bin / bash
i = 1
until [$ i -gt 6]
do
echo "until $ i"
i = $ [$ i + 1] # or you can write let i ++
done

With cycles, you need to be careful not to get a variant of an infinite loop. By the way, to debug bash scripts, you can change the first line to #! / Bin / bash -x or run the script com *** th bash -x :

igor @ ubuntu: ~ / linux $ bash -x ./testfor.sh
+ i = 1
+ '[' 1 -gt 5 ']'
+ echo i = 1
i = 1
+ let i = i + 1
+ '[' 2 -gt 5 ']'
+ echo i = 2
i = 2
+ let i = i + 1
+ '[' 3 -gt 5 ']'
+ echo i = 3
i = 3
+ let i = i + 1
+ '[' 4 -gt 5 ']'
+ echo i = 4
i = 4
+ let i = i + 1
+ '[' 5 -gt 5 ']'
+ echo i = 5
i = 5
+ let i = i + 1
+ '[' 6 -gt 5 ']'

Be sure to practice writing small scripts to consolidate your understanding of how loops work in bash .

Functions in bash

Functions are used in bash very widely. Functions are described in two ways: with the function keyword, and without it.

The first way:

function function_name
{
function body
}

The second way:

function_name ()
{
function body
}

A function is called by name anywhere in the script, but only after the description of the function itself. Functions You can also pass parameters that are specified a space after the call (name) of the function. Consider an example bash script:

#! / bin / bash
function primer
{
if [$ # -ne 0]
then
local a = 1
echo "Number of parameters passed - $ #"
for i in $ @
do
echo "$ a-th parameter - $ i"
let a ++
done
return 0
else
echo "Parameters not passed"
return 1
fi
}
echo "Call a function with parameters:"
primer abc
echo $?
echo "Call a function without parameters:"
primer
echo $?

In this example, a function named primer is given . Function call with parameters: primer abc and without parameters: primer . In the function body, all constructs should be familiar to you, with the exception of $ # , $ i, and $ @ . $ # - returns the number of parameters passed to the function. In our example, this will be the number 3 . $ @ returns all parameters in a single line. In the example, this will be abc . And through $ 1 , $ 2 , $ 3 , etc. You can access each parameter individually. $? - contains the code for the execution of the last command. In our example, the function execution code.

The function can also return a numeric value through the keyword return . As a rule, 0 is returned if the function is executed without errors or a nonzero value, if something went wrong. In the example, in the case of a function call with parameters, the return value is 0, and if the function was called without parameters, then the return code will be 1.

Everything related to passing parameters to a function works the same way for a script. You can also pass parameters to a script in the same way and manipulate them in the same way with $ #, $ @, $ N. From the same category and option - $ 0 - which returns the name of the command that launched the script. If the script was started by the command ./script.sh , then echo $ 0 returns the value ./script.sh , and if by the command /home/igor/linux/script.sh , the value / home / igor / linux / script will be returned . sh .


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

LINUX operating system

Terms: LINUX operating system