Difference between revisions of "BASH/C2/Conditional-Loops/English"
(Created page with ''''Title of script:''' '''Basic If loop in BASH ''' '''Author:''' Ashwini Patil '''Keywords: video tutorial, for loop, while loop''' {| style="border-spacing:0;" ! <center>V…') |
Nancyvarkey (Talk | contribs) |
||
Line 49: | Line 49: | ||
| 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 '''for''' '''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 '''for''' '''loop'''. | ||
− | + | Let us see the syntax. | |
− | '''for | + | '''for expression 1, 2, 3''' |
− | ''' | + | '''statement 1, 2, 3''' |
− | ''' | + | And this is the end of '''for loop'''. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 69: | Line 63: | ||
'''for variable in sequence/range''' | '''for variable in sequence/range''' | ||
− | ''' | + | '''statement 1, 2, 3''' |
− | ''' | + | And end of '''for loop'''. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Switch to the program in '''gedit'''. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Switch to the program in '''gedit'''. | ||
− | | 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 of '''for loop '''using the | + | | 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 of '''for loop '''using the first syntax. |
− | In this program, we will calculate the sum of | + | In this program, we will calculate the sum of first '''n''' numbers. |
|- | |- | ||
Line 100: | Line 88: | ||
'''sum=0 ''' | '''sum=0 ''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The variable | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The variable '''number''' will store the value given by user. The value is an integer here. |
− | Now, we initialize the variable '''sum''' as | + | Now, we initialize the variable '''sum''' as zero. |
|- | |- | ||
Line 117: | Line 105: | ||
First, we initialize '''i''' to the value '''1'''. | First, we initialize '''i''' to the value '''1'''. | ||
− | Then we check whether '''i''' is less than or equal to ''' | + | Then we check whether '''i''' is less than or equal to '''number'''. |
|- | |- | ||
Line 134: | Line 122: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''i++))''' | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''i++))''' | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| After this, we increment the value of '''i '''by 1'''.''' | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| After this, we increment the value of '''i '''by 1'''.''' | ||
− | |||
− | |||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''<nowiki>i<=y;</nowiki>''' | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''<nowiki>i<=y;</nowiki>''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| And then we check the same condition till this condition is false . | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| And then we check the same condition till this condition is '''false'''. |
|- | |- | ||
Line 178: | Line 164: | ||
After that, the last line of the output is displayed: | After that, the last line of the output is displayed: | ||
− | '''Sum of first | + | '''Sum of first n numbers is 15''' |
|- | |- | ||
Line 184: | Line 170: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us see the flow of the program. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us see the flow of the program. | ||
− | Let me resize the | + | Let me resize the windows. |
|- | |- | ||
Line 219: | Line 205: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we check whether''' 2 '''is less than or equal to''' 5.''' | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| 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.''' | + | The condition is '''true '''and now sum will be''' 1 + 2 '''ie''' 3.''' |
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''i++))''' | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''i++))''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''i '''is then incremented by '''1''' and the new value of''' | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''i '''is then incremented by '''1''' and the new value of''' i''' is''' 3.''' |
And we get '''sum '''as''' 6.''' | And we get '''sum '''as''' 6.''' | ||
Line 239: | Line 225: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| On exiting the '''for loop, '''the final message is printed. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| On exiting the '''for loop, '''the final message is printed. | ||
− | |||
− | |||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Open file '''for-loop.sh''' >> Point cursor to the filename. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Open file '''for-loop.sh''' >> Point cursor 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;"| Let us see another example of '''for loop '''using the | + | | 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 another example of '''for loop '''using the second syntax. |
I have written the code in this file and named it as '''for-loop.sh''' | I have written the code in this file and named it as '''for-loop.sh''' | ||
Line 266: | Line 250: | ||
'''ls''' command lists the directory content. | '''ls''' command lists the directory content. | ||
− | '''-1''' lists one file per line. | + | '''-1 (hyphen one)''' lists one file per line. |
|- | |- | ||
Line 287: | Line 271: | ||
'''./for-loop.sh''' | '''./for-loop.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 execute the script on the terminal by typing - | + | | 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 script on the '''terminal''' by typing - |
'''chmod +x for-loop.sh''' | '''chmod +x for-loop.sh''' | ||
+ | |||
'''./for-loop.sh''' | '''./for-loop.sh''' | ||
Line 295: | Line 280: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Show the output | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Show the output | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This will display all the files present in the | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This will display all the files present in the '''Home''' directory. |
|- | |- | ||
Line 302: | Line 287: | ||
Let us understand the syntax first. | Let us understand the syntax first. | ||
− | |||
− | |||
|- | |- | ||
| 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;"| |
+ | '''while condition''' | ||
− | ''' | + | '''statement 1, 2, 3''' |
− | ''' | + | End of '''while loop'''. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
Line 330: | Line 306: | ||
| 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 of''' while 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 see an example of''' while loop.''' | ||
− | + | Here I have named it as '''while.sh''' | |
Line 341: | Line 317: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us go through the code. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us go through the code. | ||
− | Here, we accept a number from the user and store it in variable ''' | + | Here, we accept a number from the user and store it in variable '''number'''. |
|- | |- | ||
Line 349: | Line 325: | ||
'''sum=0''' | '''sum=0''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Next, we declare variables '''i''' | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Next, we declare variables '''i''' and '''sum''' and initialize them to''' 0 (zero).''' |
|- | |- | ||
Line 357: | Line 333: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now this is the''' while condition.''' | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now this is the''' while condition.''' | ||
− | Here we check whether '''i''' is '''less than or equal''' to value of ''' | + | Here we check whether '''i''' is '''less than or equal''' to value of '''number '''given by the user. |
|- | |- | ||
Line 363: | Line 339: | ||
'''echo $((sum=sum+i)) ''' | '''echo $((sum=sum+i)) ''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we calculate the '''sum by adding value of i to value of sum'''. | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we calculate the '''sum''' by adding value of '''i''' to value of '''sum'''. |
|- | |- | ||
Line 373: | Line 349: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Next, we increment the value of '''i''' with '''2'''. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Next, we increment the value of '''i''' with '''2'''. | ||
− | This will ensure that we only add the even numbers . | + | 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 ''' | + | And the while loop is repeated till the value of '''i''' exceeds the value of '''number.''' |
|- | |- | ||
Line 389: | Line 365: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type: | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type: | ||
− | chmod +x while.sh | + | '''chmod +x while.sh''' |
Type: | Type: | ||
− | ./while.sh | + | '''./while.sh''' |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type: | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type on the '''terminal''': |
'''chmod +x while.sh''' | '''chmod +x while.sh''' | ||
− | |||
'''./while.sh''' | '''./while.sh''' | ||
Line 418: | Line 393: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight'''<nowiki>while [ $i -le $num ] </nowiki>''' | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight'''<nowiki>while [ $i -le $num ] </nowiki>''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| First we check whether '''i''' which is '''0,''' is less than or equal to ''' | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| First we check whether '''i''' which is '''0,''' is less than or equal to '''number''', which is '''15'''. |
|- | |- | ||
Line 425: | Line 400: | ||
'''$((sum=sum+i)) ''' | '''$((sum=sum+i)) ''' | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The condition is '''true, '''hence '''sum '''will be '''0+0 '''ie '''0.''' | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The condition is '''true, '''hence '''sum '''will be '''0+0 '''ie '''0.''' | ||
− | |||
− | |||
|- | |- | ||
Line 437: | Line 410: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight'''<nowiki>while [ $i -le $num ] </nowiki>''' | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight'''<nowiki>while [ $i -le $num ] </nowiki>''' | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we check if '''2''' is less than or equal to '''15'''. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we check if '''2''' is less than or equal to '''15'''. | ||
− | |||
− | |||
Line 445: | Line 416: | ||
'''$((sum=sum+i)) ''' | '''$((sum=sum+i)) ''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The condition | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The condition again is '''true; '''so we add '''0+2.''' |
Now '''sum '''has the value '''2.''' | Now '''sum '''has the value '''2.''' | ||
Line 453: | Line 424: | ||
'''<nowiki>i=$[$i+2] </nowiki>''' | '''<nowiki>i=$[$i+2] </nowiki>''' | ||
− | | 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;"| Again the value of '''i''' will be incremented by '''2'''. |
So now value of '''i''' will be '''2+2''' ie '''4.''' | So now value of '''i''' will be '''2+2''' ie '''4.''' | ||
Line 468: | Line 439: | ||
And we get the total value in '''sum '''as''' 56.''' | And we get the total value in '''sum '''as''' 56.''' | ||
− | |||
− | |||
|- | |- | ||
Line 477: | Line 446: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Summary | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Summary | ||
− | In this tutorial we learnt | + | In this tutorial we learnt two different syntax of '''for loop '''and we also learnt about the '''while loop.''' |
|- | |- | ||
Line 485: | Line 454: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| As an assignment - | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| As an assignment - | ||
− | Find the sum of the first n prime numbers. | + | Find the sum of the first "n" prime numbers. |
|- | |- |
Latest revision as of 11:33, 7 January 2014
Title of script: Basic If loop in BASH
Author: Ashwini Patil
Keywords: video tutorial, for loop, while loop
|
|
---|---|
Display Slide 1 | Dear friends, welcome to the spoken tutorial on
loops 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 practice this tutorial. |
Display Slide 4
|
Let us start with an introduction to loops
|
Display Slide 5 | Let us start with for loop.
Let us see the syntax. for expression 1, 2, 3 statement 1, 2, 3 And this is the end of for loop. |
Display Slide | An alternate syntax of for loop is:
for variable in sequence/range statement 1, 2, 3 And end of for loop. |
Switch to the program in gedit. | Let us see an example of for loop using the first syntax.
|
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 number will store the value given by user. The value is an integer here.
|
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 number. |
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 n numbers is 15 |
Resize the terminal window and the editor. | Now let us see the flow of the program.
Let me resize the windows. |
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.
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.
|
Open file for-loop.sh >> Point cursor to the filename. | Let us see another example of for loop using the second syntax.
I have written the code in this file and named it as for-loop.sh
|
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 (hyphen one) lists one file per line. |
Highlight
do echo $file; done |
This will list all the files present in your home directory.
|
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
|
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. |
Display Slide 6 |
while condition statement 1, 2, 3 End of while loop.
|
Point cursor to the filename
while.sh |
Let us see an example of while loop.
Here I have named it as while.sh
|
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 number. |
Highlight
i=0 sum=0 |
Next, we declare variables i and sum and initialize them to 0 (zero). |
Highlightwhile [ $i -le $num ]
do |
Now this is the while condition.
Here we check whether i is less than or equal to value of number 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.
|
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 on the terminal:
chmod +x 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 number, which is 15. |
Highlight
$((sum=sum+i)) |
The condition is true, hence sum will be 0+0 ie 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 again is true; so we add 0+2.
Now sum has the value 2. |
Highlight
i=$[$i+2] |
Again 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.
| |
Display Slide 8
Summary |
Summary
In this tutorial we learnt two 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. |