Difference between revisions of "BASH/C3/Basics-of-functions/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title of script: Basics of functions in Bash ''' '''Author:''' Lavitha Pereira '''Keywords: Video tutorial, Bash shell, function, function call''' {| style="border-spacing…')
 
Line 23: Line 23:
 
* Work flow of a f'''unction'''
 
* Work flow of a f'''unction'''
 
* with the help of an example  
 
* with the help of an example  
 
 
  
 
|-
 
|-
Line 34: Line 32:
  
 
If not, for relevant tutorials please visit our website which is as shown,'''(http://www.spoken-tutorial.org)'''
 
If not, for relevant tutorials please visit our website which is as shown,'''(http://www.spoken-tutorial.org)'''
 
 
 
  
 
|-
 
|-
Line 46: Line 41:
 
Till now we were using '''GNU BASH '''version 4.1.10
 
Till now we were using '''GNU BASH '''version 4.1.10
  
From now onwards we will use '''GNU BASH''' version 4.2
+
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.
+
Please note,''' GNU Bash version 4''' or above is recommended for practice.
  
 
|-
 
|-
Line 61: Line 56:
  
  
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| * A '''function''' is a collection of commands or an algorithm.
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|  
 +
* A '''function''' is a collection of commands or an algorithm.
 
* It is intended to do a specific task.
 
* It is intended to do a specific task.
 
* It is used to break up a complex program into separate tasks.
 
* It is used to break up a complex program into separate tasks.
 
* This improves overall script readability and ease of use.
 
* This improves overall script readability and ease of use.
 
 
  
 
|-
 
|-
Line 74: Line 68:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Display Slide 6
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Display Slide 6
 
 
'''Function declaration'''
 
'''Function declaration'''
 
 
Syntax 1:
 
Syntax 1:
  
  
 
'''function function_name'''
 
'''function function_name'''
 
 
'''{'''
 
'''{'''
 
 
'''Commands'''
 
'''Commands'''
 
 
'''}'''
 
'''}'''
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| First syntax is
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| First syntax is
Line 97: Line 86:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Display Slide 7
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Display Slide 7
 
 
'''Function declaration'''
 
'''Function declaration'''
 
 
Syntax 2:
 
Syntax 2:
  
 
'''function_name()'''
 
'''function_name()'''
 
 
'''{'''
 
'''{'''
 
+
'''Commands'''
'''commands'''
+
 
+
 
'''}'''
 
'''}'''
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The second syntax is
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The second syntax is
Line 124: Line 108:
  
 
'''Function_name'''
 
'''Function_name'''
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Function call
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Function call'''
  
Function can be called anywhere within the program.
+
'''Function''' can be called anywhere within the program.
  
  
Type the '''function name '''at''' '''the location where you want to call it.
+
Type the '''function name '''at the location where you want to call it.
  
  
Line 136: Line 120:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|  
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us understand  
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us understand this with the help of a simple example.
 
+
* “'''declaration of a function'''” and
+
* “'''calling a function'''”
+
* with the help of a simple example.
+
 
+
 
+
  
 
|-
 
|-
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Terminal:'''gedit function.sh'''
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Terminal:'''gedit function.sh'''
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| I have already typed the code in a file named '''function.sh'''  
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| I have already typed the code in a file '''function.sh'''  
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''<nowiki>#!/usr/bin/env bash </nowiki>'''
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''<nowiki>#!/usr/bin/env bash </nowiki>'''
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| This is the''' shebang line'''
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| This is the''' shebang line'''.
  
 
|-
 
|-
Line 158: Line 136:
  
 
<nowiki>#Function declaration </nowiki>
 
<nowiki>#Function declaration </nowiki>
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Function is declared by the keyword '''function''' followed by '''function name'''  
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Function''' is declared by the keyword '''function''' followed by '''function name'''.
  
 
Here, '''function name''' is '''machine'''.
 
Here, '''function name''' is '''machine'''.
Line 182: Line 160:
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The content within curly brackets is called '''function definition'''.
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The content within curly brackets is called '''function definition'''.
  
Here,
 
  
 
I have displayed various machine details like-
 
I have displayed various machine details like-
Line 188: Line 165:
 
'''uname -a '''gives machine information.
 
'''uname -a '''gives machine information.
  
'''w -h '''gives users logged on to system.
+
'''w -h '''gives users logged onto system.
  
 
'''uptime '''gives the time since machine was on.
 
'''uptime '''gives the time since machine was on.
Line 207: Line 184:
 
Here we display the message  
 
Here we display the message  
  
'''Beginning of main program”. '''
+
'''“Beginning of main program” '''
  
 
|-
 
|-
Line 226: Line 203:
  
  
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us understand the workflow
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us understand the workflow.
  
  
 
# When the '''bash interpreter''' visits the '''function definition''', it simply scans the '''function.'''
 
# When the '''bash interpreter''' visits the '''function definition''', it simply scans the '''function.'''
 
# The '''function''' is called only when its name appears within the script.
 
# The '''function''' is called only when its name appears within the script.
# When the '''interpreter''' reads the '''function name''', it executes the '''function definition.'''<br/>
+
# When the '''interpreter''' reads the '''function name''', it executes the '''function definition.'''  
 
+
 
+
 
+
  
 
|-
 
|-
Line 243: Line 217:
  
  
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| # The '''interperter''' treats f'''unction name '''as a command.
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|  
# Please remember that we have to '''define''' the '''function''' before '''calling''' it.
+
The '''interperter''' treats '''function name '''as a command.
  
  
 +
Please remember that we have to '''define''' the '''function''' before '''calling''' it.
  
 
|-
 
|-
Line 255: Line 230:
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now go to the''' terminal.'''
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now go to the''' terminal.'''
  
To make this code file'' ''executable, type:'''chmod space plus x space function.sh'''Press '''Enter.'''
+
To make this code file'' ''executable, type:
 +
 
 +
'''chmod space plus x space function.sh'''
 +
 
 +
Press '''Enter.'''
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''./function.sh>>'''Press '''Enter'''
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''./function.sh>>'''Press '''Enter'''
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now type dot slash''' function.sh''' .'''Press Enter.'''
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now type '''dot slash function.sh'''  
 +
 
 +
 
 +
Press '''Enter.'''
  
 
|-
 
|-
Line 268: Line 250:
  
  
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The output i.e. the machine details of my system are seen on the '''terminal.'''
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The output i.e. the machine details of my system, are seen on the '''terminal.'''
  
  
Line 275: Line 257:
 
This brings us to the end of this tutorial.
 
This brings us to the end of this tutorial.
  
Come back to our slides to summarize.
+
Come back to our slides.
  
 
|-
 
|-
Line 290: Line 272:
 
* Work flow of '''function'''
 
* Work flow of '''function'''
 
* with an example
 
* with an example
 
 
  
 
|-
 
|-
Line 303: Line 283:
 
# The first function should display the diskspace usage in human readable form'''(Hint: df -h)'''
 
# The first function should display the diskspace usage in human readable form'''(Hint: df -h)'''
 
# The second function should display the filesystem usage in human readable form'''(Hint: du -h)'''
 
# The second function should display the filesystem usage in human readable form'''(Hint: du -h)'''
 
 
  
 
|-
 
|-

Revision as of 10:48, 7 March 2014

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 for practice.

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 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.


I have displayed various machine details like-

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.


  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.


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.sh

Press 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.

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