This video will discussed about whil until and for loops
loops will enables you to execute a set of commands repeatedly .
loop =same line that again agin
The while loop
The for loop
The until loop
The while loop
============
the while loop executes the given commands until the given condition remains true;
The while loop enables you to execute a set of commands repeatedly until some condition occurs.
It is usually used when you need to manipulate the value of a variable repeatedly.
syntax
--------
while command
do
Statement(s) to be executed if command is true
done
If the resulting value is true, given statement(s) are executed.
If command is false then no statement will be executed and the program will jump to the next line after the done statement.
example
#!/bin/sh
a=0
while [ $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
The for loop
=========
The for loop operates on lists of items. It repeats a set of commands for every item in a list.
Syntax
for var in word1 word2 ... wordN
do
Statement(s) to be executed for every word.
done
Here var is the name of a variable and word1 to wordN are sequences of characters separated by spaces (words).
Each time the for loop executes, the value of the variable var is set to the next word in the list of words, word1 to wordN.
Example
Here is a simple example that uses the for loop to span through the given list of numbers −
Live Demo
#!/bin/sh
for var in 0 1 2 3 4 5 6 7 8 9
do
echo $var
done
example2: #!/bin/sh
for FILE in $HOME/.bash*
do
echo $FILE
done
################################
The Until Statements
================
Sometimes you need to execute a set of commands until a condition is true.
The while loop is perfect for a situation where you need to execute a set of commands while some condition is true
syntax:
until command
do
Statement(s) to be executed until command is true
done
If the resulting value is false, given statement(s) are executed.
If the command is true then no statement will be executed and the program jumps to the next line after the done statement.
#!/bin/sh
a=0
until [ ! $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done
loop inside a loop
======================
#!/bin/bash
a=0
while [ "$a" -lt 10 ] # this is loop1
do
b="$a"
while [ "$b" -ge 0 ] # this is loop2
do
echo -n "$b"
b=`expr $b - 1`
done
a=`expr $a + 1`
echo "a=$a"
done
echo "---EOS---"
======================
shell loop control(break)
========================
sometimes you need to stop a loop or skip iterations of the loop
the break statement
used to terminate the execution of the entire loop.
example: break.sh
====================
#!/bin/bash
a=0
while [ $a -lt 10 ]
do
echo $a
if [ $a -eq 5 ]
then
break
fi
a=`expr $a + 1`
done
echo "---EOS---"
==================
countinue statement
===================
causes the current iteration of the loop to exit , rather than the entire a loop.
#!/bin/bash
NUMS="1 2 0 3"
SUM=0
COUNT=0
for NUM in $NUM
do
if [ $NUM -eq 0 ];then
continue
fi
SUM=`expr $SUM + $NUM`
COUNT=`expr $COUNT + 1`
done
echo "SUM=$SUM"
echo "COUNT=$COUNT"
echo "---EOS---"