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 scripts 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. This approach of using functions would provide better readability, which is easier to understand, as well these functions could be handled 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 scripts.
The basic syntax of declaring a function inside a shell script is:
function_name()
{
< statement1 >
< statement2 >
.....
.....
}
The declared function would be called inside main function using function_name, and any required 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 the arguments to username, password & comments from the main program. So, when the scripts gets executed, it would create users "mpatrick" & "ssmurthy" with specified parameters. Also, note that I've used the shift command twice which would make it easy to consider rest of arguments passed after password as comments.
The below snap shows how it was 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 to check if that gets failed and accordingly it would print a failed message. Otherwise, print a 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 also 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.
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.
1 comment:
It is really a great work and the way in which you are sharing the knowledge is excellent.
aws training in chennai with placement | best aws training in chennai | aws certification exam centers in chennai
Post a Comment