The largest Interview Solution Library on the web


« Previous | 1 | 2 | 3 | Next »

Unix – Shell Loops


Loops are a powerful programming tool that enable you to execute a set of commands repeatedly. In this tutorial, you would examine the following types of loops available to shell programmers:

The while loop

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

Here Shell command is evaluated. If the resulting value is true, given statement(s) are executed. If command is false then no statement would be not executed and program would jump to the next line after done statement.

Example:

Here is a simple example that uses the while loop to display the numbers zero to nine:

#!/bin/sh
a=0
while [ $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done

This will produce following result:

0
1
2
3
4
5
6
7
8
9

Each time this loop executes, the variable a is checked to see whether it has a value that is less than 10. If the value of a is less than 10, this test condition has an exit status of 0. In this case, the current value of a is displayed and then a is incremented by 1.

The for loop

The for loop operate 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 for loop to span through the given list of numbers:

#!/bin/sh
for var in 0 1 2 3 4 5 6 7 8 9
do
echo $var
done

This will produce following result:

0
1
2
3
4
5
6
7
8
9

Following is the example to display all the files starting with .bash and available in your home. I'm executing this script from my root:

#!/bin/sh
for FILE in $HOME/.bash*
do
echo $FILE
done

This will produce following result:

/root/.bash_history
/root/.bash_logout
/root/.bash_profile
/root/.bashrc

The until loop

The while loop is perfect for a situation where you need to execute a set of commands while some condition is true. Sometimes you need to execute a set of commands until a condition is true.

Syntax:

until command
do
Statement(s) to be executed until command is true
done

Here Shell command is evaluated. If the resulting value is false, given statement(s) are executed. Ifcommand is true then no statement would be not executed and program would jump to the next line after done statement.

Example:

Here is a simple example that uses the until loop to display the numbers zero to nine:

#!/bin/sh
a=0
until [ ! $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done

This will produce following result:

0
1
2
3
4
5
6
7
8
9

The select loop

The select loop provides an easy way to create a numbered menu from which users can select options. It is useful when you need to ask the user to choose one or more items from a list of choices.

Syntax:

select 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.

For every selection a set of commands would be executed with-in the loop. This loop was introduced in ksh and has been adapted into bash. It is not available in sh.

Example:

Here is a simple example to let the user select a drink of choice:

#!/bin/ksh
select DRINK in tea cofee water juice appe all none
do
case $DRINK in
tea|cofee|water|all)
echo "Go to canteen"
;;
juice|appe)
echo "Available at home"
;;
none)
break
;;
*) echo "ERROR: Invalid selection"
;;
esac
done

The menu presented by the select loop looks like the following:

$./test.sh
1) tea
2) cofee
3) water
4) juice
5) appe
6) all
7) none
#? juice
Available at home
#? none
$

You can change the prompt displayed by the select loop by altering the variable PS3 as follows:

$PS3="Please make a selection => " ; export PS3
$./test.sh
1) tea
2) cofee
3) water
4) juice
5) appe
6) all
7) none
Please make a selection => juice
Available at home
Please make a selection => none
$

You would use different loops based on dfferent situation. For example while loop would execute given commands until given condition remains true where as until loop would execute until a given condition becomes true.

Once you have good programming practice you would start using appropriate loop based on situation. Here while and for loops are available in most of the other programming languages like C, C++ and PERL etc.

Nesting Loops:

All the loops support nesting concept which means you can put one loop inside another similar or different loops. This nesting can go upto unlimited number of times based on your requirement.

Here is an example of nesting while loop and similar way other loops can be nested based on programming requirement:

Nesting while Loops:

It is possible to use a while loop as part of the body of another while loop.

Syntax:

while command1 ; # this is loop1, the outer loop
do
Statement(s) to be executed if command1 is true
while command2 ; # this is loop2, the inner loop
do
Statement(s) to be executed if command2 is true
done
Statement(s) to be executed if command1 is true
done

Example:

Here is a simple example of loop nesting, let's add another countdown loop inside the loop that you used to count to nine:

#!/bin/sh
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
echo
a=`expr $a + 1`
done

This will produce following result. It is important to note how echo -n works here. Here -n option let echo to avoid printing a new line character.

0
1 0
2 1 0
3 2 1 0
4 3 2 1 0
5 4 3 2 1 0
6 5 4 3 2 1 0
7 6 5 4 3 2 1 0
8 7 6 5 4 3 2 1 0
9 8 7 6 5 4 3 2 1 0
« Previous | 1 | 2 | 3 | Next »


copyright © 2014 - all rights riserved by javatechnologycenter.com