• No results found

Arithmetic and Logic Operations

In document Hp-Ux Unix Rehman (Page 135-138)

Table 6-2. Meta Characters Used in Regular Expressions Charact

Chapter 7. File Permissions Chapter Syllabus

11.1 Arithmetic and Logic Operations

11.2 The while-do-done Loop

11.3 The until-do-done Loop 11.4 The for-do-done Loop 11.5 Breaking a Loop 11.6 Text Processing

Loops are used to perform an operation repeatedly until a condition becomes true or false. The test or let command is used to check the condition every time a repetition is made. All loop structures used in shell programming start with a keyword. The block of commands that is executed repeatedly is enclosed by the do-done keywords.

There are three basic types of loops. The first one is the for-do-done loop, which is used to execute a block of commands for a fixed number of times. The while-do-done loop checks for a condition and goes on executing a block of commands until that condition becomes false.

The until-do-done loop repeats the execution of a block of commands until a condition becomes true. As soon as the condition becomes true, the loop terminates.

All of these loops are controlled by a variable known as the control variable. This variable gets a new value on every repetition of the loop. The let command is also used to make arithmetic, logic, and assignment operations inside the loops and to change the value of the control variable.

In this chapter, we will start with arithmetic and logic operations performed with the let command. The three loops will be discussed one-by-one. You will find the general syntax of each loop as well as a flow diagram. In the end, you will find some text processing examples and their use in loops.

11.1 Arithmetic and Logic Operations

The let command performs both arithmetic and logic operations. The use of the let command is important because all loops depend on the control variable. The value of this control must be changed during the execution of the loop. Usually this value is incremented or decremented with the help of the let command. The loop structures also need logic operations, used for the testing value of the control variable. This is the second use of the let command. Like the test command, the let command also has explicit and implicit modes.

Explicit Mode let Command

In the explicit mode, the word let is used in the command line. Consider the following example of the use of the command.

$ A=5

$ B=3

$ let "C=A+B"

$ echo $C 8

$

You created two new shell variables A and B and assigned these variables numeric values.

Then you used the let command to sum these two values and assign the result to a third variable C. To display the value of this variable, you used the echo command. Like this arithmetic operation, you can also perform logic operations with the let command as shown in the following example.

$ var1=5

$ var2=3

$ let "var1<var2"

$ echo $?

1

$ let "var1>var2"

$ echo $?

0

$

In this example, you compared two variables. The first comparison was not true, so the result code returned is 1. The second comparison is true and the result code is zero.

Implicit Mode let Command

You can replace the word let with double parentheses on each side of the expression. The above example, where you added two variables, can also be accomplished as follows.

$ A=5

$ B=3

$ ((C=A+B))

$ echo $C 8

$

The let command can also perform complex operations like the one shown here.

((A=A+(3*B)/(A-1)))

While evaluating the result of an expression, the usual arithmetic rules are applied.

Parentheses can be used to alter the order of evaluation.

Table 11-1 lists the operators that can be used with the let command.

The first two operators are unary operators that need only one operand. All other operators are binary operators and need two operands. You will find many examples of the use of the let command in this chapter.

Table 11-1. Operators Used with the let Command

Operator Description

- Unary minus

! Unary negation (same value but with a negative sign)

= Assignment

+ Addition

- Subtraction

* Multiplication / Integer division

% Remainder

< Less than

> Greater than

<= Less than or equal to

>= Greater than or equal to

== Comparison for equality

!= Comparison for nonequality 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 "The while loop example"

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.

In document Hp-Ux Unix Rehman (Page 135-138)