• No results found

The while-do-done Loop

In document Rehman_HP (Page 134-138)

Chapter review questions

Chapter 7. File Permissions Chapter Syllabus

11.2 The while-do-done Loop

The while-do-done loop is used to test a condition before the execution of the block of commands contained inside the loop. The command block is executed if the test is successful and returns a true value. It may happen that the command block never executes if the test fails the very first time. The loop continues to execute as long as the condition remains true. The general syntax of the while-do-done loop is shown here.

while condition do

command block done

The condition is usually an expression containing a test or let command. Both of these commands are usually used in implicit mode. The while-do-done loop can be represented as a flow diagram as shown in Figure 11-1.

Figure 11-1. The while-do-done loop.

Let us see an example of the loop. We start with assigning the value 1 to a variable VAR1. Every time the loop executes, we double the value of the variable. The loop continues as long as the value of the variable is less than 100. As soon as the variable value reaches this limit, the loop execution terminates, and the next command after the done keyword is executed. The shell program script-20 follows.

#!/usr/bin/sh

echo "The while loop example" echo

VAR1=1

while ((VAR1 < 100)) do

echo "Value of the variable is : $VAR1" ((VAR1 = VAR1 * 2))

done echo

echo "The loop execution is finished"

You can also use the test command instead of the let command in the comparison made in the while condition. In that case, this line will be:

while [ VAR1 -lt 100 ]

When you execute this program, you will see the output shown here. $ ./script-20

The while loop example Value of the variable is : 1 Value of the variable is : 2 Value of the variable is : 4 Value of the variable is : 8 Value of the variable is : 16 Value of the variable is : 32 Value of the variable is : 64 The loop execution is finished $

A while loop may become an infinite loop if you make a mistake while making a test decision. For example, consider the following program where you start with a value of VAR1 equal to 1. You add 2 to the value of VAR1 at each step. You compare the value of the variable with 10. This condition is never fulfilled because the value of the variable never becomes 10. It goes from 9 to 11, skipping the value to which the comparison is made. By changing "!=" to "<=", you can solve the problem. The program script-21 is shown here.

#!/usr/bin/sh

echo VAR1=1

while ((VAR1 != 10)) do

echo "Value of the variable is : $VAR1" ((VAR1 = VAR1 + 2))

done echo

echo "The loop execution is finished"

Another example of an infinite loop is when you forget to modify the control variable inside the loop, such as in the code segment that follows.

VAR1=1

while ((VAR1 != 10)) do

echo "Value of the variable is : $VAR1" done

Here the value of VAR1 is always 1, and the condition remains true, resulting in an infinite loop. 11.3 The until-do-done Loop

The until-do-done loop is like the while-do-done loop. The only difference is that it tests the condition and goes on executing as long as the condition remains false. It terminates execution as soon as the condition becomes true. The general syntax of this loop is:

until condition do

command block done

The flow diagram of the until-do-done loop is shown in Figure 11-2. Figure 11-2. The until-do-done loop.

As you may have noticed, the only difference between Figure 11-1 and Figure 11-2 is that the "True" and "False" positions have been interchanged. Here is script-22, which has the same result as script-20 but was implemented using an until-do-done loop.

#!/usr/bin/sh

echo "The until loop example" echo

VAR1=1

until (( VAR1 > 100 )) do

echo "Value of the variable is : $VAR1" ((VAR1 = VAR1 * 2))

done echo

echo "The loop execution is finished" 11.4 The for-do-done Loop

The for-do-done loop is executed on a list of elements. The list of elements is assigned to a variable one-by-one. The value of this variable is processed inside the loop. The loop continues to execute until all of the list elements are processed and there are no more elements in the list. The general syntax of the for-do-done loop is:

for var in list do

done

The for-do-done loop flow diagram is shown in Figure 11-3. Figure 11-3. The for-do-done loop.

As an example of the use of this loop, if you want to list all executable files in your home directory, you can use the following program (script-23) for this purpose.

#!/usr/bin/sh

echo "List of all executable files in home directory" cd $HOME for F in * do if [ -x $F ] then ll $F fi done

The asterisk character represents all files in this directory. When you run this program, the result is shown as follows. You may have a different result on your system. There may be other uses of this program. You can utilize this script to find all files that have the SUID bit set or some other type of file with slight modifications.

$ ./script-23

List of all executable files in home directory

-rwxr-xr-x 1 boota users 267 Oct 18 19:23 script-00 -rwxr-xr-x 1 boota users 131 Oct 18 19:53 script-01 -rwxr-xr-x 1 boota users 198 Oct 18 20:01 script-02 -rwxr-xr-x 1 boota users 100 Oct 18 20:07 script-03 -rwxr-xr-x 1 boota users 121 Oct 18 20:16 script-04 -rwxr-xr-x 1 boota users 132 Oct 18 21:25 script-05 -rwxr-xr-x 1 boota users 232 Oct 18 23:11 script-06 -rwxr-xr-x 1 boota users 177 Oct 18 22:04 script-07 -rwxr-xr-x 1 boota users 142 Oct 19 17:43 script-08 -rwxr-xr-x 1 boota users 170 Oct 19 18:04 script-09 -rwxr-xr-x 1 boota users 638 Oct 19 18:30 script-10 -rwxr-xr-x 1 boota users 313 Oct 19 19:31 script-11 -rwxr-xr-x 1 boota users 195 Oct 20 23:16 script-20 -rwxr-xr-x 1 boota users 193 Oct 20 23:00 script-21 -rwxr-xr-x 1 boota users 195 Oct 21 17:04 script-22 -rwxr-xr-x 1 boota users 140 Oct 21 17:07 script-23 $

The script-24 is another example of the for-do-done loop, where a list is provided to the for command. This list contains the names of weekdays, which the program reads one-by-one and prints them on your terminal screen.

#!/usr/bin/sh

for DAY in Sunday Monday Tuesday Wednesday Thursday Friday Saturday

do

echo "The day is : $DAY" done

The result of this program is: $ ./script-24

The day is : Sunday The day is : Monday The day is : Tuesday The day is : Wednesday The day is : Thursday The day is : Friday The day is : Saturday $

Changing File Access Date and Time

Let's suppose you want to change the access time of all files in your current directory to the current time. You can use the touch command with a small shell script as shown here.

for FILE in * do

touch $FILE done

Accessing Command Line Parameters

To process all command line parameters one-by-one using a for-do-done loop, the following code segment may be used.

for ARG in $* do

echo $ARG done

You can replace the echo command with any command or a block of commands to get a desired result.

Study Break Use of Shell Loops

All of the three shell loops have their own applications. However, the while-do- done and until-do-done loops can be used interchangeably in many cases. Let's have some practice with these loops. Using a while-do-done loop, write a shell program that takes a number as input and then prints its table from 1 to 10. Now change the while-do-done loop to an until-do-done loop to get the same functionality. Use the for-do-done loop and pass a list of numbers from 1 to 10 to the for statement. Again print the table with this arrangement.

In document Rehman_HP (Page 134-138)