Loop -while


The bash while loop can be defined as a control flow statement which allows executing the given set of commands repeatedly 
as long as the applied condition evaluates to true.
while [ expression ];  
do  
commands;  
multiple commands;  
done  

-----------------------------------------------------------------------------------------------------------------
Example
-----------------------------------------------------------------------------------------------------------------

#! bin/bash
# while loop

number=1
while [ $number -lt 5 ]
do
echo "$number"
number=$(( number+1 ))
done



[prabhucloudxlab@cxln4 ~]$ sh hello.sh
1
2
3
4
#Note: 5 not printer, because 5 > 5; its false



----------------------------------------------------------------------------------------------------------------------------------
with Single Condition
----------------------------------------------------------------------------------------------------------------------------------
--Script
[haritaraka12078@cxln4 ~]$ cat>while1.sh

#!/bin/bash

#Script to get specified numbers
read -p "Enter starting number: " snum
read -p "Enter ending number: " enum

while [[ $snum -le $enum ]];
do
echo $snum
((snum++))
done
echo "This is the sequence that you wanted."



[haritaraka12078@cxln4 ~]$ sh while1.sh
Enter starting number: 1
Enter ending number: 5
1
2
3
4
5
This is the sequence that you wanted.


----------------------------------------------------------------------------------------------------------------------------------
with Multiple Condition
----------------------------------------------------------------------------------------------------------------------------------
--Script
[haritaraka12078@cxln4 ~]$ cat>while2.sh

#!/bin/bash

#Script to get specified numbers
read -p "Enter starting number: " snum
read -p "Enter ending number: " enum

while [[ $snum -lt $enum || $snum == $enum ]];
do
echo $snum
((snum++))
done
echo "This is the sequence that you wanted."


[haritaraka12078@cxln4 ~]$ sh while2.sh
Enter starting number: 1
Enter ending number: 5
1
2
3
4
5
This is the sequence that you wanted.


----------------------------------------------------------------------------------------------------------------------------------
infinite while loop
----------------------------------------------------------------------------------------------------------------------------------
--Script
#!/bin/bash  
#An infinite while loop  
  
while :; do echo "Welcome to Javatpoint."; done  



----------------------------------------------------------------------------------------------------------------------------------
with a Break Statement
----------------------------------------------------------------------------------------------------------------------------------
--Script
[haritaraka12078@cxln4 ~]$ cat>while3.sh

#!/bin/bash

#While Loop Example with a Break Statement
echo "Countdown for Website Launching..."
i=10
while [ $i -ge 1 ]
do
        if [ $i == 2 ]
        then
echo "Mission Aborted, Some Technical Error Found."
break
        fi
echo "$i"
(( i-- ))
done



[haritaraka12078@cxln4 ~]$ sh while3.sh
Countdown for Website Launching...
10
9
8
7
6
5
4
3
Mission Aborted, Some Technical Error Found.


----------------------------------------------------------------------------------------------------------------------------------
with a Continue Statement
----------------------------------------------------------------------------------------------------------------------------------
--Script
[haritaraka12078@cxln4 ~]$ cat>while4.sh

#!/bin/bash

#While Loop Example with a Continue Statement
i=0
while [ $i -le 10 ]
do
((i++))
if [[ "$i" == 5 ]];
then
continue
fi
echo "Current Number : $i"
done
echo "Skipped number 5 using Continue Statement."



[haritaraka12078@cxln4 ~]$ sh while4.sh
Current Number : 1
Current Number : 2
Current Number : 3
Current Number : 4
Current Number : 6
Current Number : 7
Current Number : 8
Current Number : 9
Current Number : 10
Current Number : 11
Skipped number 5 using Continue Statement.



----------------------------------------------------------------------------------------------------------------------------------
while script like C program]
----------------------------------------------------------------------------------------------------------------------------------
--Script
[haritaraka12078@cxln4 ~]$ cat>while5.sh

#!/bin/bash

#While loop example in C style
i=1
while((i <= 10))
do
echo $i
let i++
done



[haritaraka12078@cxln4 ~]$ sh while5.sh
1
2
3
4
5
6
7
8
9
10