BASH/C2/Conditional-Loops/English

From Script | Spoken-Tutorial
Revision as of 15:52, 30 December 2013 by Ashwini (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Title of script: Basic If loop in BASH

Author: Ashwini Patil

Keywords: video tutorial, for loop, while loop


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

loops in BASH

Display Slide 2 In this tutorial, we will learn
  • for loop and
  • while loop
    with a few examples


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 practice this tutorial.

Display Slide 4


Let us start with an introduction to loops


Loops are used to execute a group of statements repeatedly.

Display Slide 5 Let us start with for loop.

Syntax of for loop is as follows:

for ((exp1; exp2; exp3))

do

statement1

statement2

statement3

done

Display Slide An alternate syntax of for loop is:

for variable in sequence/range

do

statement1

statement2

statement3

done

Switch to the program in gedit. Let us see an example of for loop using the 1st syntax.


In this program, we will calculate the sum of the first n numbers.

Point cursor to the filename for.sh Note that our filename is for.sh
Highlight #!/bin/bash This is our shebang line.
Highlight

read -p “Enter number: ” y

sum=0

The variable y will store the value given by user. The value is an integer here.


Now, we initialize the variable sum as 0.

Highlight

for ((i=1;i<=y;

Do not highlight

i++))

This is where we begin the for loop.

First, we initialize i to the value 1.

Then we check whether i is less than or equal to y.

Highlight

do

echo $((sum=sum+i))

done

Now here, we calculate the sum as sum plus i

and then we print it.

Highlight i++)) After this, we increment the value of i by 1.



Highlight i<=y; And then we check the same condition till this condition is false .
Highlight

echo "Sum of first n numbers is $sum"

On exiting the for loop, this message is printed.
Let us execute the program and observe what happens.
Switch to the terminal and type:

chmod +x for.sh

Type:

./for.sh

On the terminal type -

chmod +x for.sh

Then type:

./for.sh

Type: 5

Highlight the output

I will enter 5 as the input number.

The sum that is calculated for each value of i is displayed.

After that, the last line of the output is displayed:

Sum of first 5 numbers is 15

Resize the terminal window and the editor. Now let us see the flow of the program.

Let me resize the window.

Highlight

for ((i=1;

First we have value of i as 1.
Highlight i<=y; Then we check whether 1 is less than or equal to 5.
Highlight

do

echo $((sum=sum+i))

done

Since the condition is true, we calculate sum as 0 + 1.


Now we have sum as 1.

Then we print sum i.e 1.

Highlight i++)) Next, i is incremented by 1 and the new value of i is 2.
Highlight 3 in the output Then we check whether 2 is less than or equal to 5.

The condition is true and now sum will be 1+2 ie 3.

Highlight i++)) i is then incremented by 1 and the new value of i is 3.

And we get sum as 6.

The script will continue to add the next value of i to the previous value in sum.

This will continue till i<=5 is false.

Highlight

echo "Sum of first n numbers is $sum"

On exiting the for loop, the final message is printed.


<PAUSE>

Open file for-loop.sh >> Point cursor to the filename. Let us see another example of for loop using the 2nd syntax.

I have written the code in this file and named it as for-loop.sh


This simple program will list the files in a directory.

Highlight #!/bin/bash This is the shebang line.
Highlight

for file in $(ls -1);


Then we have for loop.

ls command lists the directory content.

-1 lists one file per line.

Highlight

do

echo $file;

done

This will list all the files present in your home directory.


This is the end of for loop.

On the terminal, type

chmod +x for-loop.sh

./for-loop.sh

Let us execute the script on the terminal by typing -

chmod +x for-loop.sh

./for-loop.sh

Show the output This will display all the files present in the home directory.
Switch to the slides Now, we will learn about the while loop.

Let us understand the syntax first.

I will switch back to the slides

Display Slide 6 Syntax:

while [ condition ]

do

statement1

statement2

statement3

done


This means that the while loop will execute as long as the condition is true.

Point cursor to the filename

while.sh

Let us see an example of while loop.

I have the program here and I have named it as while.sh


In this program, we will calculate the sum of even numbers within a given range.

Highlight

read -p “Enter number:” num

Let us go through the code.

Here, we accept a number from the user and store it in variable num.

Highlight

i=0

sum=0

Next, we declare variables i & sum and initialize them to 0.
Highlightwhile [ $i -le $num ]

do

Now this is the while condition.

Here we check whether i is less than or equal to value of num given by the user.

Highlight

echo $((sum=sum+i))

Then we calculate the sum by adding value of i to value of sum.
Highlight

i=$[$i+2]

done

Next, we increment the value of i with 2.

This will ensure that we only add the even numbers .


And the while loop is repeated till the value of i exceeds the value of num.

echo "Sum of even numbers within the given range is $sum" When we exit the while loop, we print the sum of all the even numbers within the given range.
On the terminal Let us execute the program.
Type:

chmod +x while.sh

Type:

./while.sh

Type:

chmod +x while.sh

Type:

./while.sh

Type: 15 I will give 15 as my input.
Highlight the output The last line of the output is:

Sum of even numbers within the given range is 56.

Resize the editor window and the terminal Let me resize the window and explain the output.
Highlightwhile [ $i -le $num ] First we check whether i which is 0, is less than or equal to num, which is 15.
Highlight

$((sum=sum+i))

The condition is true, hence sum will be 0+0 ie 0.

And we print 0.

Highlight

i=$[$i+2]

Now i will be incremented by 2 and the new value of i is 2.
Highlightwhile [ $i -le $num ] Then we check if 2 is less than or equal to 15.



Highlight

$((sum=sum+i))

The condition is again true; so we add 0+2.

Now sum has the value 2.

Highlight

i=$[$i+2]

And the value of i will be incremented by 2.

So now value of i will be 2+2 ie 4.

Highlight the output And the next value of sum will be 4+2 ie 6.
In the same way, the script will continue to add 2 to the previous value of i, till it exceeds 15.


And we get the total value in sum as 56.


<PAUSE>

Display Slide 8

Summary

Summary

In this tutorial we learnt 2 different syntax of for loop and we also learnt about the while loop.

Display Slide 9

Assignment

As an assignment -

Find the sum of the first n prime numbers.

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

This is Ashwini Patil from IIT Bombay

Thank You for joining.

Contributors and Content Editors

Ashwini, Nancyvarkey