Difference between revisions of "BASH/C2/More-on-Loops/English"
Nancyvarkey (Talk | contribs) |
|||
(2 intermediate revisions by one other user not shown) | |||
Line 42: | Line 42: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To learn this tutorial, | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To learn this tutorial, | ||
− | you should be familiar with | + | you should be familiar with '''loops''' in '''Bash'''. |
Line 53: | Line 53: | ||
− | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us start with an introduction to '''nested loop.'''. | |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us start with an introduction. | + | |
A loop within a loop is known as '''nested loop.''' | A loop within a loop is known as '''nested loop.''' | ||
Line 60: | Line 59: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Display Slide 6 | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Display Slide 6 | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | Outer for loop | + | '''Outer for loop''' |
− | expression 1, 2, 3 | + | '''expression 1, 2, 3''' |
− | Inner for loop | + | '''Inner for loop''' |
− | expression 1, 2, 3 | + | '''expression 1, 2, 3''' |
− | statement 1 | + | '''statement 1''' |
− | statement 2 | + | '''statement 2''' |
− | Closing | + | '''Closing Inner for loop''' |
− | Closing | + | '''Closing Outer for loop''' |
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Open the file for-new.sh | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Open the file for-new.sh | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us see an example on '''for''' | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us see an example on '''nested for loop'''. |
|- | |- | ||
Line 101: | Line 89: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| First let us go through the directory structure. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| First let us go through the directory structure. | ||
− | Here is a directory on the '''Desktop''' named '''simple-nested-for. Let's open it. | + | Here is a directory on the '''Desktop''' named '''simple-nested-for'''. Let's open it. |
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Open '''test '''directory to show the text files. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Open '''test '''directory to show the text files. | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We have subdirectories '''test''', '''test2''' and '''test3''' and a | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We have subdirectories '''test''', '''test2''' and '''test3''' and a '''Bash script'''. |
− | In each sub-directory there are multiple text files. | + | In each sub-directory, there are multiple''' text files'''. |
|- | |- | ||
Line 118: | Line 106: | ||
Please note that the same can be acheived with a single line command | Please note that the same can be acheived with a single line command | ||
− | '''ls -1 -R test*''' | + | '''ls -1 (hyphen one) -R test*(test asterix)''' |
− | + | ||
− | + | ||
− | + | ||
+ | But we will do it using a '''for loop'''. | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Point to the filename. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Point to the filename. | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Note that the name of our | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Note that the name of our''' Bash script''' is''' nested-for.sh''' |
|- | |- | ||
Line 134: | Line 120: | ||
'''<nowiki>#!/usr/bin/env bash</nowiki>''' | '''<nowiki>#!/usr/bin/env bash</nowiki>''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is our shebang line. | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is our''' shebang line'''. |
|- | |- | ||
Line 149: | Line 135: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is the outer '''for loop'''. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is the outer '''for loop'''. | ||
− | This '''for loop''' will check for directories starting with the name '''test '''. | + | This '''for loop''' will check for directories starting with the name '''test'''. |
− | First 'echo' line will display the sub-directories | + | First ''''echo'''' line will display the sub-directories names. |
− | The second 'echo' line will create a blank line. | + | The second ''''echo'''' line will create a blank line. |
|- | |- | ||
Line 160: | Line 146: | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is the inner '''for | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is the inner '''for loop'''. |
It will check for files present within the directories. | It will check for files present within the directories. | ||
Line 166: | Line 152: | ||
'''ls '''displays the directory content. | '''ls '''displays the directory content. | ||
− | '''-1''' is used to list one file per line. | + | '''-1 (hyphen one)''' is used to list one file per line. |
|- | |- | ||
Line 178: | Line 164: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we list the files. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we list the files. | ||
− | 'done' ends the inner '''for loop'''. | + | ''''done'''' ends the inner '''for loop'''. |
− | This command prints a vertical line after completion of every cycle of ''' | + | This command prints a vertical line after completion of every cycle of outer '''for-loop.''' |
− | 'done' ends the ''' | + | ''''done'''' ends the outer '''for loop'''. |
|- | |- | ||
Line 188: | Line 174: | ||
'''Ctrl+alt+t''' | '''Ctrl+alt+t''' | ||
+ | |||
+ | Type: | ||
+ | |||
+ | cd Desktop/simple-nested-for | ||
+ | |||
+ | |||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute the program. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute the program. | ||
Open the '''terminal '''by pressing '''ctrl+alt+t''' keys simultaneously on your keyboard. | 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.''' | ||
|- | |- | ||
Line 202: | Line 204: | ||
'''chmod +x nested-for.sh''' | '''chmod +x nested-for.sh''' | ||
− | |||
− | '''./nested-for.sh''' | + | Press '''Enter.''' |
+ | |||
+ | |||
+ | Type: '''./nested-for.sh''' | ||
+ | |||
+ | |||
+ | Press '''Enter.''' | ||
|- | |- | ||
Line 234: | Line 241: | ||
In this tutorial we learnt, | In this tutorial we learnt, | ||
− | Nested for loop | + | '''Nested for''' loop |
− | + | ||
Latest revision as of 11:47, 7 January 2014
Title of script: Nested loop in Bash
Author: Ashwini Patil
Keywords: video tutorial, for loop
|
|
---|---|
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
|
Display Slide 3 | For this tutorial I am using
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.
|
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
|
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.
ls -1 (hyphen one) -R test*(test asterix)
| |
Point to the filename. | Note that the name of our Bash script is nested-for.sh |
Highlight
|
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
|
Highlight
Output |
The output is displayed.
It shows
|
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. |