while loop

Used for executing same sets of commands for a variable with multiple values.

Syntax 1:
Output | while read <variable>
do
<commands>
done

Syntax 2:
while read <variable>
do
<commands>
done < <filename>

Example:
[root@PC1 ~]# ls
anaconda-ks.cfg  names.txt  name.txt
[root@PC1 ~]# cat names.txt
Amar Kumar
Shahzahan Akbar
Alexendera The Great
[root@PC1 ~]# cat names.txt | while read line
> do
> echo "$line"
> done
Amar Kumar
Shahzahan Akbar
Alexendera The Great
[root@PC1 ~]# while read line
> do
> echo "$line"
> done < names.txt
Amar Kumar
Shahzahan Akbar
Alexendera The Great
[root@PC1 ~]#


WHILE LOOP in Unix Shell Scripting: