Pages

Tuesday, May 3, 2016

Working with function

Some points to be remembered while working with functions,

  • Whenever you have a set of commands to be used repetitively in that case defining function for those set of commands can be beneficial
  • Function name should be with Lower-case, with underscores to separate words. Separate libraries with ::
  • Parentheses are required after the function name.
  • The keyword function is optional, but must be used consistently throughout a project.
  • If you're writing single functions, use lowercase and separate words with underscore.
  • If you're writing a package, separate package names with :: , Braces must be on the same line as the function name and no space between the function name and the parenthesis.
  • Function should be used post it has been declared or initialized. If you try to use if before initialization you can expect error as “Command not found”



# # Single function
my_func() {
#Function body
}

## Part of a package
mypackage::my_func() {
#Function body
}

Simple example of function usage,

# cat simple_function.sh

#!/bin/bash
## Single function
my_func()
{
echo "Test Function"
}
## Calling function
my_func
Sample output --

# sh simple_function.sh
Test Function

Using function before its initialization,

# cat simple_function.sh

#!/bin/bash
## Calling function
my_func
## Single function
my_func()
{
echo "Test Function"
}
Sample output --

# sh simple_function.sh
simple_function.sh: line 3: my_func: command not found

Passing arguments to function

# cat simple_function.sh

#!/bin/bash
## Single function
my_func()
{
echo "Inside Function"
echo "arguments received are :"
echo "First=$1, Second=$2 Third=$3, Forth=$4, Fifth=$5, Sixth=$6, Seventh=$7, Eightth=$8 Nineth=$9, Tenth=$10"
}
## Calling function
my_func One Two Three Four Five Six Seven Eight Nine Ten
Sample output –

sh simple_function.sh
Inside Function
arguments received are :
First=One, Second=Two Third=Three, Forth=Four, Fifth=Five, Sixth=Six, Seventh=Seven, Eightth=Eight Nineth=Nine, Tenth=One0

Passing script arguments to function as arguments

cat simple_function.sh

#!/bin/bash
## Single function
my_func()
{
echo "Inside Function"
echo "arguments received are :"
echo "First=$1, Second=$2 Third=$3, Forth=$4, Fifth=$5, Sixth=$6, Seventh=$7, Eightth=$8 Nineth=$9, Tenth=$10"
}
## Calling function
my_func $@

Sample output –

sh simple_function.sh 1 2 3 4 5 6 7
Inside Function
arguments received are :
First=1, Second=2 Third=3, Forth=4, Fifth=5, Sixth=6, Seventh=7, Eightth= Nineth=, Tenth=10

Importing a function defined in another file

cat simple_function

#!/bin/bash
## Single function
my_func()
{
echo "Inside Function"
echo "arguments received are :"
echo "First=$1, Second=$2 Third=$3, Forth=$4, Fifth=$5, Sixth=$6, Seventh=$7, Eightth=$8 Nineth=$9, Tenth=$10"
}

cat calling_function.sh

#!/bin/bash
## importing function file
. simple_function
## Calling function
my_func $@

Sample output –

sh calling_function.sh MY NAME IS JOHN
Inside Function
arguments received are :
First=MY, Second=NAME Third=IS, Forth=JOHN, Fifth=, Sixth=, Seventh=, Eightth= Nineth=, Tenth=MY0

No comments:

Post a Comment