BASH/C3/Basics-of-functions/English

From Script | Spoken-Tutorial
Revision as of 17:51, 4 March 2014 by Ashwini (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Title of script: Basics of functions in Bash

Author: Lavitha Pereira

Keywords: Video tutorial, Bash shell, function, function call


Visual Cue
Narration
Display Slide 1 Dear friends, Welcome to the spoken tutorial on Basics of functions in Bash.
Display Slide 2 In this tutorial, we will learn
  • Importance of Functions
  • To declare a function
  • To call a function
  • Work flow of a function
  • with the help of an example


Display Slide 3Prerequisites


To follow this tutorial you should have knowledge of Shell Scripting in BASH.

If not, for relevant tutorials please visit our website which is as shown,(http://www.spoken-tutorial.org)



Display Slide 4

System requirements

For this tutorial I am using Ubuntu Linux 12.04 Operating System and

Till now we were using GNU BASH version 4.1.10

From now onwards we will use GNU BASH version 4.2

Please note, GNU Bash version 4 or above is recommended to practice this tutorial.

Let us see what is a function and its usage.
Display Slide 5

Functions


* A function is a collection of commands or an algorithm.
  • It is intended to do a specific task.
  • It is used to break up a complex program into separate tasks.
  • This improves overall script readability and ease of use.


There are two syntaxes for function declaration.
Display Slide 6

Function declaration

Syntax 1:


function function_name

{

Commands

}

First syntax is

function space function_name

within curly brackets,

the commands to be executed

Display Slide 7

Function declaration

Syntax 2:

function_name()

{

commands

}

The second syntax is

function_name open and close round brackets

within curly barckets,

the commands to be executed

Display Slide 8

Function call


Function_name

Function call

Function can be called anywhere within the program.


Type the function name at the location where you want to call it.


The syntax is the function name itself.

Let us understand
  • declaration of a function” and
  • calling a function
  • with the help of a simple example.


Terminal:gedit function.sh I have already typed the code in a file named function.sh
#!/usr/bin/env bash This is the shebang line
[Highlight]

function machine

#Function declaration

Function is declared by the keyword function followed by function name

Here, function name is machine.

{

# Beginning of Function definition

echo -e "\nMachine information:" ; uname -a

echo -e "\nUsers logged on:" ; w -h

echo -e "\nMachine status :" ; uptime

echo -e "\nMemory status :" ; free

echo -e "\nFilesystem status :"; df -h

# End of Function definition

}

The content within curly brackets is called function definition.

Here,

I have displayed various machine details like-

uname -a gives machine information.

w -h gives users logged on to system.

uptime gives the time since machine was on.

free gives memory status.

df -h gives filesystem status.

[Highlight]

echo “Beginning of main program”


The main program starts here.

Here we display the message

Beginning of main program”.

machine #This is a function call Here, machine is a function call.
echo “End of main program” Here we display the message

“End of main program”

Display Slide 8

Workflow


Let us understand the workflow


  1. When the bash interpreter visits the function definition, it simply scans the function.
  2. The function is called only when its name appears within the script.
  3. When the interpreter reads the function name, it executes the function definition.



Display slide 9

Workflow


# The interperter treats function name as a command.
  1. Please remember that we have to define the function before calling it.


Switch to terminal>> Type chmod +x function.sh>>Press Enter>>Type


Now go to the terminal.

To make this code file executable, type:chmod space plus x space function.shPress Enter.

./function.sh>>Press Enter Now type dot slash function.sh .Press Enter.
Terminal

[Output]


The output i.e. the machine details of my system are seen on the terminal.


Please Note: The output will vary from system to system.

This brings us to the end of this tutorial.

Come back to our slides to summarize.

Display Slide 8

Summary

Let us summarize.

In this tutorial we learnt,

  • Importance of functions
  • Function declaration
  • Function call
  • Work flow of function
  • with an example


Display Slide 9

Assignment

As an assignment.

Write a program with two functions

  1. The first function should display the diskspace usage in human readable form(Hint: df -h)
  2. The second function should display the filesystem usage in human readable form(Hint: du -h)


Display Slide 10

http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial

About the Spoken Tutorial Project

Watch the video available at the link shown below

It summarises the Spoken Tutorial project

If you do not have good bandwidth, you can download and watch it

Display Slide 11

Spoken Tutorial Workshops

The Spoken Tutorial Project Team

Conducts workshops using spoken tutorials

Gives certificates to those who pass an online test

For more details, please write to

contact@spoken-tutorial.org

Display Slide 12

Acknowledgement

Spoken Tutorial Project is a part of the Talk to a Teacher project

It is supported by the National Mission on Education through ICT, MHRD, Government of India

More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro

Display Slide 13 The script has been contributed by FOSSEE and spoken-tutorial team

This is Ashwini Patil from IIT Bombay.

Signing off

Thank you for joining.

Contributors and Content Editors

Ashwini, Nancyvarkey