Functions
Like real programming languages, shell has functions, though in a somewhat limited implementation. A function is a subroutine, a code block that implements a set of operations, a black box that performs a specified task.
On this page
Wherever there is repetitive code, when a task repeats with only slight variations in procedure, then consider using a function.
function function_name {
# command...
}
This second form will cheer the hearts of C programmers (and is more portable).
As in C, the function’s opening bracket may optionally appear on the second line.
function_name ()
{
# command...
}
A function may be compacted
into a single line.
fun () { echo "This is a function"; echo; }
In this case, however, a semicolon must follow the final command in the function. Here is an example to show how functions are defined:
stat() {
if [ -d $1 ]
then
echo "$1 is a directory" return 0
else
echo "$1 is not a directory" return 1
fi;
}
This function tests the filename to see if it is a directory. If it is it returns a status of 0
Otherwise it returns status 1. Do not forget to place the semi-colon (;)
at the end of the last line. You can type your function in its entirety at the beginning of the shell program. When you wish to access it, you use the following format:
name [ parameter . . . ]
where name is the name of the function, and [parameter…] refers to any optional positional parameters you wish to include. When calling a function, no parentheses are used; when defining a function, parentheses are necessary.
When specifying parameters in a function call, be aware that the positional parameters ($1
, $2
, . .. ) for the entire shell program will be reset to these parameters, and the original values will be lost.
Calling Functions
To invoke the function, type the name of the function followed by any arguments that need to be passed to the function. Following is a function that takes a file name ($1) as an argument and checks whether it is executable. If it tests true, it prints out that the file is executable.
#script name is fun1.sh function exef
{
if [ -x $1 ]
then
echo $1 is executable
fi
}
exef script # calling the function with argument as the filename
Give the execute permission and execute the script like the following:
./fun1.sh
script is executable
The above script checks whether the file script
is executable or not. In a larger program this function is easily called by specifying the function name and the argument list:
#script name is fun2.sh function exef
{
if [ -x $1 ]
then
echo $1 is executable fi
}
for file in `ls`
do
exef $file
done
where exef $file is the function call.
The above script checks each and every file in the current working directory whether it is having execute permission or not, and if it is executable, it echoes the particular file is executable.
This program explains the concept of recursion, which means calling the same function.
#script name is fact1.sh
fact() {
if [ $1 –gt 1 ]
then
i=`expr $1 – 1`
j=`fact $i`
k=`expr $1 \* $j`
echo $k
else
echo 1 fi
}
echo –e "enter the number to find factorial:\c" read x
fact $x
Execute the above script with –x
options, which is used for debugging the script. Then, the flow of the program is easily understood.
Accessing a function which is present in different file in the current script. Inorder to access a function present in other file, first execute the file in which the function is present in current file, so the code is available to the current script.
The advantage of functions is code reusability means if many programs require the common code, then write that code in a file, so that file is used by as many programs which needs that code.
The following example shows the above scenario which converts the .txt files to .bak files or .html files to .html-bak files
.
#script name is comfun1.sh
rename() {
from=$1
to=$2
for i in *from
do
j=`basename $i $from`
mv $i ${j}$to
done
}
#script name is txt-bak.sh
. comfun1.sh # execute the function script here
rename txt bak #calling the function by passing arguments
#script name is html-html-bak.sh . comfun1.sh
rename html html-bak