Sunday, May 13, 2018

Usage Of Functions In Shell Scripting

Benefits of using functions inside shell scripting are:

- Less coding.
- Modular approach.
- Easy to understand.
- Exported as required for portability.
- Easy to expand or edit later.

As mentioned before, we could use functions inside shell scripting whenever required such as when there are set of codes which are common across some shell scripts then those common lines could be grouped into a function and called when required. Like-wise this approach of using functions would provide better readability, which is easier to understand, as well these functions could be amended independently. A group of codes put together to perform a specific task and defined with a name could be called as a function in shell scripting.

The basic syntax of declaring a function inside a shell script is:

< statement1 >    
 < statement2 >  

The declared function would be called inside main function using function_name, and any requirements arguments (optional) would be passed:

function_name <argument1, argument2, argument3, ...>

Being said that, let's start creating a simple function and demonstrate the usage. In this example, I'd create a simple shell script to add user which is performed using functions as shown below:

As you could see in the above script, I've passed username, password & comments in the main program. So, when the scripts gets executed, it would create users "mpatrick" & "ssmurthy" with specified parameters. Also, note that there is shift command being used twice which would make it easy to consider rest of arguments passed after password as comments.

The below snap shows how it got executed and users gets added:

So, at first look the script looks to be working as expected, however, there is no error control in this program. If there is an error while executing useradd command it goes unnoticed. Hence, we could add a simple error checking mechanism which could return a value if useradd function fails to the calling program as shown below:

So, in the above script I've added a test condition after the "useradd" command call to check if that got failed and accordingly it would print failed message. Otherwise, print successful message after the changing password for user.

Now, lets re-run the script and check what happens. This time I'd remove user "ssmurthy" from system and then run the script.

From the above snap, we could see that adding user 'mpatrick' failed since the user was already present in the system, however, adding user 'ssmurthy' successful.

We'd could use the return value of a function inside the main script and depending on the result a relevant message could be displayed. So, I've added an if condition in the main script as shown below which validates the return value from the function and prints message:

So, when this script is executed it would show the results as shown below :

This is just an introduction to usage of functions inside shell scripting, there is much more that could be done using functions.


cyber news said...

IMPRESSIVE.thanks for sharing the information.
Indian Cyber Army’s most awaited internship is live now. Summer Internship 2018 on “ Ethical hacking” and book your seats before it runs out.Candidates have to get themselves registered to be a part of this Internship program. Here internship will give you on-the-job experience, help you learn whether you and Cyber security industry are a good match and can provide you with valuable connections and references. Here interns are usually exposed to a wide variety of tasks and responsibilities which allows the intern to showcase their strengths by working on projects for various managers that work on different parts of Indian Cyber Army. Becoming a high performing intern is a fantastic way to improve your employment prospects. This internship can be a great way to get your foot in the door of success with a prestigious or desirable Organization.As career in ethical hacking is most in demand.

cyber news said...

Thanks for the information.It is really nice .Information security is the set of processes that maintain the confidentiality, integrity and availability of business data in its various forms.In this age of Technology advancement, computer and information technology have not only brought convenience to citizens in modern life but also for policemen & various Government officials of the nation to fight cybercrime through various modus operandi. Indian Cyber Army has been dedicated in fighting cyber crime, striving to maintain law and order in cyberspace so as to ensure that everyone remains digitally safe.Read more:- Information Security

cyber news said...

thanks for sharing the more valuable information.
Indian Cyber Army credibility in Ethical hacking training & Cybercrime investigation training is acknowledged across nation as we offer hands on practical knowledge and full assistance with basic as well as advanced level ethical hacking & cybercrime investigation courses. The training is conducted by subject specialist corporate professionals with wide experience in managing real-time ethical hacking/ cyber security projects. Indian Cyber Army implements a blend of academic learning and practical sessions to give the candidate optimum exposure.Ethical hacking training ,
Ethical hacking course