BASH/C2/More-on-Loops/English-timed

From Script | Spoken-Tutorial
Jump to: navigation, search
Time Narration
00:01 Welcome to the spoken tutorial on Nested for loop in BASH.
00:07 In this tutorial, we will learn Nested for loop with the help of an example.
00:13 To record this tutorial, I am using Ubuntu Linux 12.04 Operating System and GNU BASH version 4.1.10
00:24 Please note, GNU Bash version 4 or above is recommended for practice.
00:31 To learn this tutorial, you should be familiar with loops in Bash.
00:37 For relevant tutorials, please visit our website which is as shown- http://spoken-tutorial.org
00:43 Let us start with an introduction to nested loop.
00:46 A loop within a loop is known as nested loop.
00:51 Let us see the syntax. Outer for loop expression 1, 2, 3;
00:57 inner for loop expression 1, 2, 3;
01:01 statement 1, statement 2
01:04 closing inner for loop, closing outer for loop.
01:09 Let us see an example on nested for loop.
01:12 First, let us go through the directory structure.
01:17 Here is a directory on the Desktop named simple-nested-for. Let's open it.
01:24 We have sub-directories "test", "test2", "test3" and a Bash script.
01:31 In each sub-directory, there are multiple text files.
01:36 Now we will move on to our code.
01:39 This program displays all the files within each sub-directory.
01:45 Please note that the same can be achieved with a single line command ls -1 (hyphen one) -R(hyphen R) test*(test asterisk).
01:53 But we will do it using a for loop.
01:58 Note that the name of our Bash script is nested-(hyphen)for dot sh.
02:05 This is our shebang line.
02:08 This is the outer for loop.
02:10 This for loop will check for directories starting with the name test.
02:15 First echo line will display the sub-directories name.
02:21 The second echo line will create a blank line.
02:25 This is the inner for loop. It will check for the files present within the directories.
02:32 "ls" displays the directory content.
02:36 -1 (hyphen one) is used to list one file per line.
02:41 Here, we list the files. done ends the inner for loop.
02:45 This command prints a horizontal line after completion of every cycle of outer for-loop.
02:53 done ends the outer for loop.
02:57 Let us execute the program.Open the terminal by pressing ctrl+alt+t keys simultaneously on your keyboard.
03:08 Now, let us go to the directory where our Bash script is.
03:13 It is on the Desktop.
03:15 Type: cd Desktop. Let us go in the folder simple-(Hyphen)nested-(Hyphen)for
03:22 Press Enter.
03:24 Type: chmod plus +x nested-(Hyphen)for dot sh
03:32 Press Enter.
03:34 Type: dot slash nested-(Hyphen)for dot sh
03:39 Press Enter.The output is displayed. It shows files in test directory, files in test2 directory and files in test3 directory.
03:52 This brings us to the end of this tutorial.
03:56 Let us summarize. In this tutorial, we learned Nested for loop.
04:02 As an assignment:
04:04 Retype: nested (hyphen)-for dot sh bash script using nested while loop.
04:11 Save your program with the name 'nested-(hyphen)while dot sh'.
04:17 Watch the video available at the link shown below. It summarizes the Spoken-Tutorial project.
04:23 If you do not have good bandwidth, you can download and watch it.
04:28 The Spoken Tutorial Project team:

Conducts workshops using spoken tutorials. Gives certificates to those who pass an online test.

04:37 For more details, please write to contact@spoken-tutorial.org
04:45 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.
04:57 More information on this mission is available at: http://spoken-tutorial.org/NMEICT-Intro
05:03 The script has been contributed by FOSSEE and Spoken-tutorial teams.
05:08 This is Ashwini Patil from IIT Bombay. Thank You for joining.

Contributors and Content Editors

Pratik kamble, Sandhya.np14