Shell script tutorial: Shell script Function

The function is one of the most powerful feature of the shell script. Function is the line of codes between two parenthesis which is executed when it is called. The functions in shell script can or cannot contains the arguments. For example: we can create a function with the function name ‘addition’ which will add the two numbers. The function do the specific thing that it is assigned to. Function remains lazy unless it is called. First, we define or create the function and later we call that specific function. If you are new to Linux shell script first, visit our beginner shell script tutorials. Let’s see an example of simple function without the argument:

Function example without Argument:

First, let’s see an example of the simple function. We are going to create a function called addition. The addition function will ask the user to input two numbers, add those two numbers and display the result. Let’s code:

#!/bin/sh
function add(){                #Function declared
echo Enter first no:
read fno
echo Enter second no:
read sno
tno=`expr $fno + $sno`
echo $tno
}
add                #Function called

The new line for you is the second line of the code and the last line of the code. The function called add is declared. So, the syntax to declare the function is:

function function_name()

At the last line, the function add is called. So, the syntax to call the function is simply:

function_name

See the output:
function output in shell script

 Function example with argument:

Function with argument is what I like to call a dynamic function. You can define the value by using the argument while calling the function. Let’s see an example. It’s a simple one.

#!/bin/sh
function argument(){            #Function decleared
echo "Welcome:$1"                       #First argument
echo "Your visiting id is: $2"           #Second argument
}
argument user1 10
argument user2 20
argument user3 30

First we defined the function called argument which simply welcome users. $1 and $2 are the first and the second argument. We will provide the value to each argument while calling the function. You might wonder why should I put $1 and $2 instead of writing just the user name and the visiting id. You won’t have only one visitor. To every visitor, you don’t want to code all these lines. So, the advantage of using function with argument is that we can call the same function with different argument each time. It’s saves us lot of time, effort and space. See the output:

function with argument example

Now, I want you to look at how to make a calculator using shell script and try making the calculator using the function. Create four different functions: addition, multiplication, subtraction and division. Add little code to each function, call them and you made yourself a calculator.

 

0saves
Posted in Shell Scripting | Tagged , , , , | Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>