2. Conditional loop
• Conditional loop is a way for computer
programs to repeat one or more various
steps depending on conditions set either
by the programmer initially or real-time
by the actual program.
• Basically a loop has a conditional
statement or a command and body of that
loop which has some list of commands or
statements to be executed repeatedly.
3. While Loop
• The while loop is used to execute a set of
commands repeatedly until some
condition occurs.
• It is usually used when the value of a
variable has to be manipulated repeatedly.
4. Types
There are 2 types of while loop. They are,
Finite while loop : it is the one in which
the loop or command will be executed
finite times.
Infinite while loop : it is the one in which
the set of commands will be executed
infinite times.
6. Steps to execute a while loop
1. Execute command.
2. If the exit status of command is
nonzero, exit from the while loop
3. If the exit status of command is
zero, execute list.
4. When list finishes execution, return to
step 1.
7. For example,
The output looks like,
x=0 0
while [ $x –lt 10 ] 1
do 2
echo $x 3
x=`echo “$x + 1” | bc` 4
done 5
6
7
8
9
8. Example(2)
c=1 Output,
while [ $c -le 5 ] Welcome 1 times
do Welcome 2 times
echo "Welcome $c Welcome 3 times
times" Welcome 4 times
(( c++ )) Welcome 5 times
done
9. Infinite while loop
It is a type of while loop which repeats its
output infinite times.
We can use some commands to
accomplish infinite loop. They are,
true
false
: etc…
10. Example for infinite while loop
while :
do
echo “jhgiwu“
done
It displays the output repeatedly.
13. Nested while loop
• It is a loop in which a while loop will be a
part of the body of another while loop.
• There is no restrictions for the number of
nested while loops.
• But it will b better to avoid more than 5
nested loops.
14. Syntax is,
while command1 ; # this is loop1, the outer loop
do
list1
while command2 ; # this is loop2, the inner
loop
do
list2
done
list3
done
15. Example,
x=0
while [ "$x" -lt 10 ] ; # this is loop1
do
y="$x"
while [ "$y" -ge 0 ] ; # this is loop2
do
echo "$y c"
y=´echo "$y - 1" | bc´
done
echo
x=´echo "$x + 1" | bc´
done
16. The output will be,
0
10
210
3210
43210
543210
6543210
76543210
876543210
9876543210
17. For loop
• The for loop is used to execute a set of
commands repeatedly for each item in a
list.
• One of its most common uses is in
performing the same set of commands for
a large number of files.
18. The common syntax is,
for name in word1 word2 ... wordN
do
list
done
It can also be written as,
for name in word1 word2 ... wordN ; do list ; done
19. For a simple for loop,
for i in 0 1 2 3 4 5 6 7 8 9 The output is,
do 0
echo $i 1
done 2
3
4
5
6
7
8
9
20. another example,
for i in `cat 1.txt` Output is,
do
echo $i F
done S
T
The contents in 1.txt,
F
S
T
21. Example(3)
alpha="a b c d e" Output,
count=0 a
for l in $alpha
b
do
count=`expr $count + 1` c
echo $l d
done e
22. Example
for color in red green pink white black
do
echo $color
done
echo “Done!!!”
24. Command line arguments
• The arguments used to pass to the shell
scripts while interpreting are called
command line arguments.
• These arguments can be passed using
some positional parameters i.e, $1, $2, ect.
• There are 9 general positional parameters.
If we have to give 10th or more than 10
args we have to use {}. i.e. ${10}.
• Each parameter corresponds to position of
the arguments on the command line.
25. Command line arguments
• $0 indicates the name of the script.
• $1 indicates the 1st argument of that script.
• $2 indicates the 2nd argument.
• $$ used to denote the process ID.
• $# used to count the number of arguments.
• $* denotes all arguments.