BASH/C3/Basics-of-functions/English
Title of script: Basics of functions in Bash
Author: Lavitha Pereira
Keywords: Video tutorial, Bash shell, function, function call
|
|
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
|
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.
|
There are two syntaxes for function declaration. | |
Display Slide 6
Function declaration Syntax 1:
{ 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 call
Function can be called anywhere within the program.
|
Let us understand
| |
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
|
Display slide 9
Workflow
|
# The interperter treats function name as a command.
|
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.
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,
|
Display Slide 9
Assignment |
As an assignment.
Write a program with two functions
|
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. |