Functions and Arguments.
In the previous video, we've learned
how to debug the script.
Like any other scripting languages,
Bash also supports functions.
In this video, we're going to see how
to define and use functions.
We can create functions to perform tasks
and we can also create functions that take parameters,
also called arguments.
As you can see on the screen.
A function can be defined as...
A function can be invoked just by using its name.
Arguments can be past the functions
and can be accessed by our script.
This is the definition of the function fname.
In the fname function, we have included
various ways of accessing the functioning arguments.
Similarly, arguments can be past the script
and can be accessed by script code on $0,
the name of the script.
$1 is the first argument, $2 is the second argument.
$n is the nth argument.
"$n expands as
$1, $2, $3, and so on.
$* expands as $1c,
$2c, $3, where c
is the first character of IFS.
$@ is used more than $*
since the form provides all arguments as a single string.
Let's explore three more tips on bash functions.
The recursive function.
Functions in bash also support recursion,
the function that can call itself.
For example, f()
{ echo $1; f hello;
sleep 1; }
We can write a recursive function
which is basically a function that calls itself.
Code on open close brackets, curly braces,
code on pipe, code on ampersand,
curly braces, semicolon, code on.
It infinitely spawns processes and ends up
in a deny of service attack.
Ampersand is post-fixed with the function call
to bring the subprocesses to the background.
This is a dangerous code as it forks processes
and therefore is called a fork bomb.
You may find it difficult to interpret the preceding code.
See the Wikipedia page,
http://en.wikipedia.org/wiki
/fork_bomb
for more details
and interpretation of the fork bomb.
It can be prevented by restricting the maximum number
of processes that can be spawned from a config file
that's /etc/security/limits.com.
Exporting functions.
A function can be exported like
environment variables, using export
such that the scope of the function can be extended
to subprocesses as
Reading the return value, status of a command.
We can get the return value of a command
or a function in this way.
This will give you the return value of the command cmd.
The return value is called exit status
and can be used to analyze whether a command
can complete its execution successfully
or unsuccessfully.
If the command exits successfully,
the exit status will be zero.
Otherwise, it will be a non-zero value.
We can check whether a command terminates
successfully or not by using this script.
Passing arguments to commands.
Arguments to commands can be passed in different formats.
Suppose minus p and minus v are the options available
and minus k capital N is another option
that takes a number.
Also, the command takes a foul name as an argument.
It can be executed in multiple ways as
Awesome. We've successfully learned about
functions and arguments.
In the next video, we will learn how to
read the output of a sequence of commands.