In this section, we will consider in detail about the shell functions. Functions allow you to break down the overall functionality of a script into smaller, consistent subsections, which can then be called upon to execute their entity tasks when essential.
Using functions to perform recurring tasks is an outstanding way to generate code reuse. This is an important part of modern object-oriented programming principles.
Shell functions are related to subroutines, events, and functions in other programming languages.
To state a function, basically use the following syntax –
The name of your function is function name, and that's what you will use to call it from elsewhere in your scripts. The function name must be followed by parentheses, followed by a list of commands enclosed within braces.
Following instance shows the use of function –
Upon execution, you will receive the following output –
You can classify a function that will recognize parameters while calling the function. These parameters would be representing by $1, $2 and so on.
Following is an instance where we pass two parameters Zara and Ali and then we confine and print these parameters in the function.
Upon execution, you will obtain the following result –
If you perform an exit command from inside a function, its consequence is not only to end execution of the function but also of the shell program that called the function.
If you in its place want to just terminate implementation of the function, then there is way to come out of a defined function.
Base on the condition you can return any value from your function using the return command whose syntax is as follows –
Here code can be anything you decide here, but visibly you should desire something that is meaningful or useful in the environment of your script as a whole.
Following function returns a value 1 –
Upon effecting, you will accept the following result –
One of the more attractive features of functions is that they can call themselves and also other functions. A function that calls itself is known as a recursive function.
Following instance demonstrate nesting of two functions –
In the lead implementation, you will accept the following result −
You can put definitions for normally used functions inside your .profile. This definition will be available whenever you log in and you can use them at the command prompt.
Instead, you can group the definitions in a file, say test.sh, and then perform the file in the present shell by typing –
This has the result of causing functions defined inside test.sh to be read and define to the present shell as follows –
To eliminate the definition of a function from the shell, use the unset command with the .f selection. This command is also used to eliminate the definition of an inconsistent to the shell.
Unix/Linux Related Interview Questions
|Perl Scripting Interview Questions||Shell Scripting Interview Questions|
|Python Interview Questions||Core Java Interview Questions|
|Linux Interview Questions||Linux Embedded systems Interview Questions|
|Red Hat Linux System Administration Interview Questions||IBM AIX Interview Questions|
|Sybase Interview Questions||IBM AIX 7 Administration Interview Questions|
|Unix Shell Scripting Interview Questions||Linux Shell Scripting Interview Questions|
Unix/Linux Related Practice Tests
|Perl Scripting Practice Tests||Shell Scripting Practice Tests|
|Python Practice Tests||Core Java Practice Tests|
|Linux Practice Tests||Linux Embedded systems Practice Tests|
|Red Hat Linux System Administration Practice Tests||IBM AIX Practice Tests|
|Sybase Practice Tests||IBM AIX 7 Administration Practice Tests|
|Autosys Practice Tests|
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.