In this lecture, we'll be talking about functions. We use functions to organize our code in blocks 1
that can be later reused. This is helpful because we want a better readability for our code, modularity 2
and also to be timesaving when designing and running code. 3
Functions allow us to create blocks of code that can be easily executed many times without the need 4
to constantly rewrite or copy paste that block of code. We create a function that executes that block 5
of code and after that we call the function. 6
Note that compared to most programming languages, bash functions are somehow limited. 7
I'll cover the basics of bash functions and show you how to use them in your scripts. 8
Creating a function is fairly easy. 9
There are two different formats to declare a function, so function 10
(this is a keyword) and the name of the function, let's say print_something, a pair of parentheses and 11
a pair of curly braces. 12
The commands between the curly braces represent the body of the function and are executed when the function 13
is called so echo 14
"I'm a simple function." 15
We can also start defining the function by giving it a name, for example, display_something 16
and echo "Hello functions." 17
Either of the above methods of specifying a function is valid; both operate the same and there is no 18
advantage or disadvantage to one over the other. 19
It's really just a personal preference. 20
In order to execute the body of the function, I have to call it. To call a function write its name without 21
any parentheses like print_something. I've just called the first function or display_something 22
The second function was called. Let's test it! 23
And the functions printed out those two strings. In other programming languages, it's common to have 24
arguments passed to the function listed inside the parentheses. In Bash 25
the parentheses are there only for decoration and you never put anything inside them. 26
If you want a function to process some data, then you send the data to the function in a similar way 27
to passing command line arguments to a script. 28
Use $1 for the first argument of the function, $2 for the second one and so on. 29
Let's create a function that creates 2 files and then change their permissions. 30
The file names will be given as arguments to the function, so create_files, parentheses and curly 31
braces. 32
I'm printing out the message for the user "Creating $1" the first argument. Here 33
D$1 is not the first argument of the script but it is the first argument of the function specified 34
when the function is called and touch $1 35
I'm also changing its permissions chmod 400$1 and I'm creating the second file. 36
touch $2 chmod 600 $2 37
And I'm calling the function with two arguments, which are the files that will be created: create_ 38
files, aa.txt bb.txt 39
I'm saving the script and running it in the other terminal. 40
Let's check if the files are created! 41
And look, this is the first file and this is the second one. 42
Let's talk about the return value. Most of the other programming languages have the concept of a return value 43
of a function. 44
However, bash functions don't allow us to return a value. When doing so using the return keyword we are 45
setting, in fact, the status of the last commandt executed inside the function. 46
Remember that the variable dollar andthe question mark contains the return status of the previously 47
run command or function. Typically a return status of zero indicates that everything went successfully 48
and a non-zero value indicates that an error has occurred. 49
For example, inside this function, I'm writing return 10 and after calling the function, I want 50
to print out the status of the last command executed. 51
So echo $? and it will print out 10 52
If you want a real return value, like in any other programming language, then you could try a command substitution. 53
For example, the following function will return the number of lines in a file that contains a specific 54
string value. 55
The string will be the first argument of the function 56
so $1 inside the function and the file will be the second argument of the function 57
so $2 inside it; function 58
(this time I'm using the function keyword) lines_in_file 59
and the command grep-c, it will print out only the number of occurrences of $1, 60
$1 is the string, found inside $2, which is the file. And I'm calling it like this n= 61
-and the command substitution (the name of the function because I'm calling it) lines_in_file 62
the first argument, let's say "usb" the string and the second argument, a log file for example, 63
var/ log /dmesg So the variable n will store how many times the string usb occurs in this file. 64
echo $n 65
And I've got the value 22; if I run the command in a terminal, I'll get the same value. 66
We'll take a short break, and in the next lecture, we'll talk about the variable scope in functions.