BASH/C3/Basics-of-functions/English-timed

From Script | Spoken-Tutorial
Jump to: navigation, search
Time Narration
00:01 Dear friends, welcome to the spoken tutorial on Basics of functions in Bash.
00:08 In this tutorial, we will learn:
00:11 Importance of Functions
00:13 To declare a function
00:15 To call a function
00:17 Work flow of a function
00:19 with the help of an example.
00:22 To follow this tutorial, you should have knowledge of Shell Scripting in BASH.
00:28 If not, for relevant tutorials, please visit our website which is as shown. http://www.spoken-tutorial.org
00:34 For this tutorial, I am using Ubuntu Linux 12.04 Operating System.
00:40 Till now we were using GNU BASH version 4.1.10
00:46 From now onwards, we will use GNU BASH version 4.2
00:52 Please note, GNU Bash version 4 or above is recommended for practice.
00:58 Let us see what is a function and its usage.
01:03 A function is a collection of commands or an algorithm.
01:08 It is intended to do a specific task.
01:12 It is used to break-up a complex program into separate tasks.
01:18 This improves overall script readability and ease of use.
01:24 There are two syntaxes for function declaration.
01:28 First syntax is-function space function_name
01:32 within curly brackets,
01:34 the commands to be executed.
01:37 The second syntax is-
01:39 function_name open and close round brackets
01:42 within curly brackets,
01:44 the commands to be executed.
01:47 Function call-Function can be called anywhere within the program.
01:53 Type the function name at the location where you want to call it.
01:58 The syntax is the function_name itself.
02:02 Let us understand this with the help of a simple example.
02:07 I have already typed the code in a file function.sh.
02:12 This is the shebang line.
02:14 Function is declared by the keyword function followed by function name.
02:21 Here, the function name is machine.
02:26 The contents within curly brackets is called function definition.
02:32 I have displayed various machine details like-
02:36 uname hyphen a gives machine information.
02:41 w hyphen h gives users logged onto system.
02:46 uptime gives the time since machine was on.
02:51 free gives memory status.
02:54 df hyphen h gives filesystem status.
02:57 The main program starts here.
03:01 We display the message “Beginning of main program” .
03:06 Here, machine is a function call.
03:09 Then we display the message “End of main program”.
03:13 Let us understand the workflow.
03:16 When the bash interpreter visits the function definition, it simply scans the function.
03:23 The function is called only when its name appears within the script.
03:28 When the interpreter reads the function name, it executes the function definition.
03:36 The interpreter treats function name as a command.
03:41 Please remember that we have to define the function before calling it.
03:47 Now go to the terminal. To make this code file executable,
03:52 type: chmod space plus x space function dot sh
03:59 Press Enter.
04:01 Type: dot slash function dot sh
04:05 Press Enter.
04:07 The output that is the machine details of my system are seen on the terminal.
04:14 Please Note: The output will vary from system to system.
04:19 This brings us to the end of this tutorial.
04:22 Come back to our slides.
04:24 Let us summarize. In this tutorial, we learned:
04:28 Importance of functions
04:30 Function declaration
04:32 Function call Work flow of function
04:35 with an example.
04:37 As an assignment-write a program with two functions.
04:42 The first function should display the disk-space usage in human readable form (Hint: df hyphen h).
04:51 The second function should display filesystem usage in human readable form (Hint: du hyphen h).
05:00 Watch the video available at the link shown below.
05:03 It summarizes the Spoken-Tutorial project.
05:07 If you do not have good bandwidth, you can download and watch it.
05:12 The Spoken Tutorial Project team: Conducts workshops using spoken tutorials;
05:17 Gives certificates to those who pass an online test.
05:21 For more details, please write to contact@spoken-tutorial.org
05:29 Spoken Tutorial Project is a part of the Talk to a Teacher project.
05:33 It is supported by the National Mission on Education through ICT, MHRD, Government of India.
05:41 More information on this mission is available at the link shown below: http://spoken-tutorial.org/NMEICT-Intro
05:47 The script has been contributed by FOSSEE and spoken-tutorial team.
05:52 This is Ashwini Patil from IIT Bombay, signing off.
05:56 Thank you for joining.

Contributors and Content Editors

Gaurav, PoojaMoolya, Pratik kamble, Sandhya.np14