BASH/C2/More-on-Loops/English

From Script | Spoken-Tutorial
Jump to: navigation, search

Title of script: Nested loop in Bash

Author: Ashwini Patil

Keywords: video tutorial, for loop


Visual Cue
Narration
Display Slide 1 Dear friends, Welcome to the spoken tutorial on

Nested for loop in BASH

Display Slide 2 In this tutorial, we will learn
  • Nested for loop
  • With the help of an example


Display Slide 3 For this tutorial I am using
  • Ubuntu Linux 12.04 Operating System and
  • GNU BASH version 4.1.10

Please note, GNU Bash version 4 or above is recommended to practise this tutorial.

Display slide 4


To learn this tutorial,

you should be familiar with loops in Bash.


For relevant tutorials, please visit our website:

http://spoken-tutorial.org

Display Slide 5


Let us start with an introduction to nested loop..

A loop within a loop is known as nested loop.

Display Slide 6

Outer for loop

expression 1, 2, 3

Inner for loop

expression 1, 2, 3

statement 1

statement 2

Closing Inner for loop

Closing Outer for loop


Open the file for-new.sh Let us see an example on nested for loop.
Show the folder structure


Double-click to open simple-nested-for directory

First let us go through the directory structure.

Here is a directory on the Desktop named simple-nested-for. Let's open it.

Open test directory to show the text files. We have subdirectories test, test2 and test3 and a Bash script.

In each sub-directory, there are multiple text files.

Now we will move on to our code.

This program displays all the files within each subdirectory.


Please note that the same can be acheived with a single line command

ls -1 (hyphen one) -R test*(test asterix)


But we will do it using a for loop.

Point to the filename. Note that the name of our Bash script is nested-for.sh
Highlight


#!/usr/bin/env bash

This is our shebang line.
Highlight

for dir in test*; do

echo "Files in $dir directory:"

echo " "


This is the outer for loop.

This for loop will check for directories starting with the name test.

First 'echo' line will display the sub-directories names.

The second 'echo' line will create a blank line.

for file in $(ls -1 $dir); do


This is the inner for loop.

It will check for files present within the directories.

ls displays the directory content.

-1 (hyphen one) is used to list one file per line.

echo $file

done

echo "------------------------"

done

Here we list the files.

'done' ends the inner for loop.

This command prints a vertical line after completion of every cycle of outer for-loop.

'done' ends the outer for loop.

Open terminal

Ctrl+alt+t

Type:

cd Desktop/simple-nested-for


Let us execute the program.

Open the terminal by pressing ctrl+alt+t keys simultaneously on your keyboard.

Now, let us go to the directory where our Bash script is.

It is on the Desktop.

Type cd Desktop

Let us go in the folder simple-nested-for

Press Enter.

Type:

chmod +x nested-for.sh

./nested-forsh

Type:

chmod +x nested-for.sh


Press Enter.


Type: ./nested-for.sh


Press Enter.

Highlight

Output

The output is displayed.

It shows

  • Files in test directory.
  • Files in test2 directory.
  • And files in test3 directory.


This brings us to the end of this tutorial.

Let us summarize.

Display Slide 7

Summary

Summary

In this tutorial we learnt,

Nested for loop


Display Slide 8

Assignment

As an assignment

Retype the above program using nested while loop.

Save your program with the name 'nested-while.sh'

Display Slide 9

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 10

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 11

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

The script has been contributed by FOSSEE and Spoken-tutorial team.

This is Ashwini Patil from IIT Bombay

Thank You for joining.

Contributors and Content Editors

Ashwini, Nancyvarkey