Linux -Shell function
3K views
Nov 28, 2024
Linux - Shell function
View Video Transcript
0:00
now here in this video we are going to
0:02
talk about these shell functions by the
0:04
name itself functional means we are
0:06
going to process something so as been by
0:09
default in Linux you have multiple
0:11
functions which are inbuilt into the
0:12
shell itself now if I'm going to use
0:15
those functions this will be a very big
0:16
functions which I cannot use completely
0:18
at a time or if I don't want to use the
0:21
whole function at a time what I can do I
0:23
can break down those functions into
0:25
multiple parts that's what we are going
0:27
to look over here so now you can see
0:29
shell functions generally enable you to
0:32
break down any particular functionality
0:35
which we generally have into a shell
0:37
into a smaller logical subsections and
0:40
this subsections can be used to perform
0:42
any particular task individually when it
0:45
is required by the user or the system so
0:49
this the function is now here we are
0:51
going to see what are the types of
0:53
functions we have as well as what are we
0:55
shell and what are the functions can do
0:57
into your programs so very first thing
1:00
you can also create a new function if
1:01
suppose I do not want to use the
1:03
existing functions I can call so create
1:05
my own function a smaller function to
1:08
perform a task also we have the passed
1:11
parameters of this function now
1:13
functions we can also pass multiple
1:16
parameters multiple options we can set
1:18
into the functions or the existing
1:20
functions we have we can also return the
1:22
value to the functions now returning
1:25
value again we have seen into the loop
1:27
how we can return the value into the
1:29
loop same kind of return values we are
1:31
going to add also in the functions then
1:34
we are going to create nested functions
1:36
we are going to add a new functions into
1:39
the existing functions this way we can
1:41
also create a nested functions as we
1:44
have used into the if-else value same
1:46
wave functions can call from the prompt
1:49
itself so this functions like already
1:51
have created the function and wherever
1:53
in any of the terminal if you want to
1:55
call this particular function so to
1:57
perform your tasks you can call it so
1:59
let us see how we can create the
2:01
function and how we can use it into our
2:03
prompt of your shell so again I'm going
2:06
to create a function in a shell itself
2:10
shell script itself so I'm going to
2:13
create
2:13
- as such and now fortifying the
2:16
function what you need to do is you need
2:18
to define a name of the function now for
2:20
example I'm defining the name as hello
2:22
so I want to create a function with name
2:24
hello so for that you need to define in
2:27
such a way that you are going to call
2:29
this hello and you are going to add into
2:31
the hello function so what you need to
2:33
add I want to add echo hello what okay
2:42
so this actually I want to add for
2:44
adding it again we need to do one thing
2:47
that is we'll just add a curly braces
2:51
over here that we are going to start
2:52
adding something into it right either
2:55
here or at the starting generally see
2:58
you can add anywhere but if you line it
3:01
well so you won't have any particular
3:04
confusion while you are writing any
3:06
particular script or any particular
3:07
functions so once you have address with
3:09
that I need to close the braces once you
3:12
have closed the presses just hit enter
3:16
and again here itself you can also
3:18
invoke here
3:20
function hello so then walk hello
3:24
function so we can give the command only
3:28
hello so automatically it will get
3:31
involved let us check as such function
3:35
dot Sh
3:37
so it does invoke and you got the value
3:39
as hello world so this is how then
3:41
really you create a small functions into
3:43
your Linux operating systems now again
3:47
you can also add the variables into the
3:50
functions like if I want to call a
3:52
particular variables I can also call it
3:54
like I will just define like we have
3:57
already used in to our previous example
4:01
I I am going to call the parameters like
4:06
so we have seen how we can call the
4:10
parameter that is first parameter and
4:13
your second parameter I'll just save it
4:17
I just gave as such function dot Sh
4:21
trainer or I can say
4:26
technical trainer hit enter Nagin's it
4:30
is calling booty
4:34
just a sec I we need to check it to show
4:37
us body parameters as well
4:45
you
4:57
okay so haven't we need to do is like
5:00
you know we are getting going to move
5:02
into the function door as such itself
5:04
and in spite of invoking it you know
5:06
from outside I'll just invoke it from
5:08
here itself okay so what the output you
5:13
should get so I'm just going to define
5:15
the parameters over here itself they
5:17
technical
5:24
hit enter save it read it out as such
5:29
functions are as trade will show you
5:31
hello wall to it first parameter and we
5:34
can see if these second parameters over
5:36
here if you can add multiple parameters
5:38
you can also add multiple parameters and
5:40
you can call it over here itself so this
5:43
is the function which we have seen now
5:44
again we can also return the value into
5:47
the functions so for that what we can do
5:50
is we are going to just capture the last
5:53
command and return the value to it so
5:55
for that what I am going to do I will
5:57
just move into the function dot SH come
6:01
on I just
6:04
capture
6:07
the value return or returned by last
6:15
command and now return the value so for
6:19
returning the value I'm just going to
6:20
add one of my characters sure that has
6:26
any particular value any particular
6:27
special director's if you have or if any
6:30
particular you know variables you have
6:32
so it should fetch those particular
6:34
thing and then we'll be using echo
6:36
command echo returning
6:41
to return value so return value I will
6:48
be using by using the variable which I
6:53
have defined here that is body T once
6:57
you have done with that close it and hit
7:02
enter
7:03
save it save the file
7:06
yes tell go and check for this file so
7:11
you can say there's no returning the
7:12
value T 0 because we haven't got any
7:14
particular or potential you know you can
7:16
also define the numbers we can also
7:17
define three-ton values now if I want to
7:20
define the return values what you can do
7:21
is move into the function again okay
7:25
just me find the return values here
7:29
under the echo command which you have
7:32
defined into the function I'm going to
7:34
return the value 210 return J once you
7:40
have done with that save it as such
7:42
functions done that's it so I can see
7:45
the return value you have that is 10 so
7:47
if you have certainly done value it will
7:49
directly take you know the return value
7:51
if you haven't set anything it will
7:52
directly move on to the value 0 from the
7:55
last output after the last output we can
7:58
see so this is how we can return your
8:01
values to another value same way if in a
8:05
function if I want to call multiple
8:07
function so I'm going to create multiple
8:09
functions into our same file and I'm
8:11
going to call one function from the
8:13
other function so for that I'm going to
8:16
again create a file which is no multi
8:18
function file mmm function this is the
8:21
name I am going to define and this is
8:24
the H file I'm going to create and now
8:26
I'm going to create a functions by
8:31
calling one function to another function
8:37
it's right another function so very
8:41
first function I'm going to define which
8:43
I am going to call from the another
8:45
function so that I am going to define
8:46
the first
8:51
function
8:55
which will be called from the second
9:03
function so this we can define so I have
9:06
to combine check it out so I'm going to
9:09
define the function 1
9:14
okay and I'm going to start with the
9:17
function as we have that to the previous
9:20
function I'm going to use the brackets
9:25
into the brackets I need to define our
9:27
open D bracket here and post that I will
9:30
add the first function the description
9:35
of it this is the
9:37
first function okay done with that enter
9:45
it and you can define the other function
9:49
so this is the calling function we can
9:51
say like I want to call the other
9:52
function
9:58
the second function I can see so here
10:01
I'm going to call the second function
10:03
which I'm going to create later on so
10:05
I'll define the second function by a
10:07
named function - so I'll be defining the
10:12
name of the function as function - once
10:14
you have done with this I'm just going
10:16
to close the bracket here so this is
10:18
completely closed then I'm going to
10:20
define the function
10:26
so you should be you know very
10:28
particular about the name because you
10:29
have used this particular name here so
10:31
function - I'm going to define and do
10:34
that again I'm going to start with the
10:36
know and drink the echo command into it
10:39
and I'm going to add this second
10:45
function so once you have done with that
10:48
again I'm going to close this so you're
10:50
gonna have I know close the second
10:52
function and I'm going to call
10:59
function1 right so for that I'll use
11:03
function one so I'll just invoke this so
11:09
if we are invoking it you over here
11:11
itself so hit enter and give command
11:15
multi pal function shell so you can see
11:19
you have two different functions okay so
11:22
our function one was not called okay so
11:27
once the function once has been called
11:29
so it is again showing you a different
11:33
function so just showing you body
11:35
functions because your function one has
11:36
already called your function to write so
11:40
what we have done is we have you know
11:44
function one and function 2 we have
11:46
defined now I have just called a
11:48
function to from the function one and I
11:50
have run just function one so
11:52
automatically or function two is been
11:54
run now if suppose I remove this can
11:57
also remove it
12:00
if I do not call it from the function
12:04
one right and if I give come on
12:08
same and function it will show me only
12:10
first function which is running right
12:12
now never warned what the function
12:14
should be working together so I'll which
12:16
is defining the functions now for
12:19
example again I want to
12:22
for example I'm going to call function
12:29
- from function1
12:35
and again the function one is calling
12:38
your function
12:42
now let's check the entries there both
12:45
are calling each other
12:49
Chansey now this will create a loop
12:53
right so this kind of cases should not
12:55
happen if it has happened we have to
12:56
define our break statements so this will
13:00
form a loop unless we are going to
13:02
control this particular loop so it won't
13:04
be happening it so so both the loops are
13:07
calling each other and they are like you
13:08
know going on creating their loops into
13:10
it
13:11
so always avoid doing this like I will
13:14
be just defining one function here and
13:17
unless I have removed it
13:24
I just remember one of the line from
13:26
here okay done now again if I run this
13:30
thing it will work well right it has
13:32
quality turning value so this is we will
13:36
just run the multi functions so now here
13:39
we can see the second value or the
13:41
second function is again well it over
13:42
here so this is how generally we create
13:44
the functions this is how generally be
13:46
you know create a functions and call the
13:49
other functions into the existing
13:50
functions as well so here we have talked
13:53
about all the functions which we have
13:55
seen and you know how we can create the
13:58
functions and as your even the machine
14:00
or your Linux operating system has some
14:03
built-in power functions they are like
14:05
exp which we generally use they are
14:07
dependent functions even we use your abs
14:10
which is a built-in function so do we
14:12
have lot of built-in functions into the
14:14
machine so by default your system calls
14:17
then when it is required so here we have
14:21
talked much about your functions and
14:23
then we are going to see something about
14:25
the administration and how to get help
14:27
for the different commands which we have
14:28
used into the next weight here so keep
14:31
watching
#Programming
#Scripting Languages