Monday, March 9, 2015

CONDITIONAL STATEMENT

Shell supports decision-making using if statement. The if statement like its counterpart in
programming languages has the following formats. The first construct executes the
statements when the condition is true. The second construct adds an optional else to the
first one that has different set of statements to be executed depending on whether the
condition is true or false. The last one is an elif ladder, in which conditions are tested in
sequence, but only one set of statements is executed.
if [ condition ]
then
statements
fi
if [ condition ]
then
statements
else
statements
fi
if [condition ]
then
statements
elif [ condition ]
then
statements
.. .
else
statements
fi

The set of relational and logical operators used in conditional expression is given below. The numeric comparison in the shell is confined to integer values only.

Operator Description
-eq          Equal to
-ne         Not equal to
-gt          Greater than
-ge         Greater than or equal to
-lt           Less than
-le         Less than or equal to
-a          Logical AND
-o          Logical OR
!            Logical NOT

No comments:

Post a Comment