Functions
Linux Commands - Shells

Functions:
A function can help you modularize your script.  In addition, the function is executed in the context of the same shell, no child process.  This will save on resources as you use functions.  Functions are a script within a script which can be defined by the user and stored in memory, allowing you to reuse the function repeatedly.  This also provides  a modular aspect that allows you to debug one function at a time be disabling functions.   

Think of functions as miniature shell scripts, or containers inside a shell script where each container functions within itself to provide resources for the larger script.  One big difference between a shell script and a function is that a new instance of the shell is started for a shell script but the function runs in the current shell. 

Lesson 6 | Lesson 8

function in bash


Arguments passed to the function are used as positional parameters within the function.

Command Line Functions
Functions can be defined at the command line or in a script.  If they are defined at the command line they look like this:

dfh() {df -h ; }

Now until you exit the shell every time you use dfh you will get df -h results.  Functions at the command line allow you to create temporary functions that are useful while the shell is open.

Here is a more complex example of functions at the command line.  A bk function is defined within the brackets and then a second command is run to send an email to the root user.

bk() { tar cvf /bk/scripts.tar /root/scripts ; echo "Scripts Backed Up" | mail -s "Backup Status" root ; }

Now the bk function can be called as many times as needed and will work as long as you do not exit the shell.

The purpose of the function is to bind the name of the function to the list of commands that you create.  This enables you to specify the name and all of the commands are executed.

function lsla { ls -la ; }

You can execute the list of commands by calling the name.

lsla

Script Functions
Functions in a script must be defined to be used and can be defined in two ways:

function functioname()

{
shell commands
}


or

functname()

{
shell commands
}

The functions that are defined in the user login session may be viewed with the command:

declare -f

Functions do not run as separate processes as would happen if you ran a script and beware, if there is a function with the same name as a script the function will take precedence.

function work {
cd
echo “ Here is a list of your  files changed in the last 24 hours”
find  -mtime -1
}

In this function which is called “work”, when it is called by the name work all three commands will execute.  

#!/bin/bash
# Timestamp Backup

function backup() {
TIMESTAMP=`date +%Y%m%d_%H%M%S`;
echo $TIMESTAMP

tar czvf /bk/jane_$TIMESTAMP.tar.gz /home/jane
}

backup



In this image you can see the container aspect of functions as multiple functions are placed in the same script.

functions in bash

The function is defined in the script and then will be available by using the function name.  It will be available both to the shell and sub-shells that are started by the script.

Once you have defined a function in a script you can undefine it using the unset command.

unset functionname

Once it has been unset you will not be able to execute it.

Creating a Library
Another way of using functions is when you create more complicated scripts is to create functions that you place in a library so that you can source them from other scripts.    These library functions can be called from a script using “source” or the “dot” command.



Function Chaining
Normally when you use functions you will define the function and then call the function in the script when you need to use it.  With function chaining you can actually call the function from within another function and then define the called function, however, the call must be made after the definition.

Notice that in the example the function rbackup is called within the function backup and then it is defined.  The final function in the script mails the results to the root user.  This works because function backup is not called until the rbackup has been defined above the place in the script where backup is called.  If you move the “backup” call to a location above the definition of rbackup it will fail.



#!/bin/bash
# Timestamp Backup and rsync

function backup() {
TIMESTAMP=`date +%Y%m%d_%H%M%S`;
echo $TIMESTAMP
tar czvf /bk/jane_$TIMESTAMP.tar.gz /home/jane

rbackup
}

function rbackup() {
rsync -av /home/jane /bk/jane
}

function mailme() {
echo "Home Directory for Jane Backed Up" | mail -s "Jane's Home" root
}

backup
mailme



Copyright CyberMontana Inc. and BeginLinux.com
All rights reserved. Cannot be reproduced without written permission. Box 1262 Trout Creek, MT 59874