Professional Documents
Culture Documents
ANDHRA PRADESH
Name : A V N L Sarojini
Designation : Lecturer
Branch : Computer Engineering
Institute : A.A.N.M. & V.V.R.S.R. Poly.,
Gudlavalleru.
Year/Semester : III Semester
Subject : UNIX & C
Subject Code : CM-302
Topic : Shell Programming &
Filtering Techniques
Duration : 50 Min
Sub Topic : Looping statements
1
CM304.15
Objective
CM304.15 2
Recap
CM304.15 3
Introduction to loop control structures (or)
Iterative structures
CM304.15 4
Various loops in shell
CM304.15 5
The while loop
while <<condition>>
do
<<set of commands>>
done
CM304.15 6
While loop
entry
Test false
condition
true
Fig.1 CM304.15
exit 7
Contd..
The while loop
CM304.15 8
The while loop example program
Write a program to find sum of ‘n’ natural numbers.
echo enter n value
read n
x=1
sum=0
while test $x -le $n
do
sum=`expr $sum + $x`
x=`expr $x + 1`
done
echo sum is $sum
CM304.15 9
The while loop example program
CM304.15 10
Contd..
The while loop example program
do
p=`expr $c \* $n`
echo $n x $c = $p
c=`expr $c + 1`
done
n=`expr $n + 1`
read abc
c=1
done
CM304.15 11
The until loop
CM304.15 12
The until loop
Syntax
until <<condition>>
do
<<set of commands>>
done
CM304.15 13
Until loop entry
Test true
condition
false
exit
CM304.15 14
fig.2
Contd..
The until loop
CM304.15 15
The until loop
CM304.15 16
The until loop example program
Write a shell program to calculate nth table.
read n
x=1
until test $x -gt 10
do
p=`expr $x \* $n`
echo $n x $x = $p
x=`expr $x + 1`
done
CM304.15 17
Summary
CM304.15 18
Exercise Program
CM304.15 19
Exercise Program
CM304.15 20
Quiz
CM304.15 21
Quiz
CM304.15 22
Quiz
CM304.15 23
Quiz
CM304.15 24
Frequently asked questions