Difference between revisions of "BASH/C3/Basics-of-functions/English"
Nancyvarkey (Talk | contribs) |
Nancyvarkey (Talk | contribs) |
||
Line 172: | Line 172: | ||
'''df -h''' gives filesystem status. | '''df -h''' gives filesystem status. | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
|- | |- |
Latest revision as of 10:56, 7 March 2014
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 for practice. |
Let us see what is a function and its usage. | |
Display Slide 5
Functions
|
|
There are two syntaxes for function declaration. | |
Display Slide 6
Function declaration Syntax 1:
|
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 this with the help of a simple example. | |
Terminal:gedit function.sh | I have already typed the code in a file 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.
uname -a gives machine information. w -h gives users logged onto 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.sh Press Enter. |
./function.sh>>Press Enter | Now type dot slash function.sh
|
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. |
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. |