1. if

Conditional binary structure: depending on whether a condition is true or false, we execute a block or we don’t.

1
2
3
if condition ; then
    instruction
fi

If the condition is true, then instruction (which can be a block of instructions) is executed.

1
2
3
4
5
if condition ; then
    instruction1
else
    instruction2
fi

For the same construction but with multiple conditions:

1
2
3
4
5
if [condition1] || [condition2] ; then
    instruction1
else
    instruction2
fi

If the condition is true, then instruction1 is executed and the second is ignored, otherwise instruction2 is executed and the first is ignored.

1
2
3
4
5
6
7
if condition1 ; then
    instruction1
elif
    condition2
then
    instruction2
fi

elif is equivalent to else if. Thus instruction2 is only executed if condition1 and condition2 are both true at the same time.

2. for

Repetitive bounded structure: loop for which we know the total number of iterations before the first pass.

1
2
3
for variable in set ; do
    instructions
done

We therefore vary the variable by making it take all the values of the set successively.

3. while

Repetitive structure as long as the condition is true.

1
2
3
while condition ; do
    instructions
done

Loop whose instructions are executed as long as the condition expression is true.

Example:

1
2
3
4
5
6
nb_cdk=3;
i=0;
while [ "$i" -lt "$nb_cdk" ];do
        echo "salut bahan\n"
        i=$(expr $i + 1)
done

or

1
2
3
4
5
6
nb_cdk=3;
declare -i i; i=0
while [ "$i" -lt "$nb_cdk" ];do
        echo "salut bahan\n"
        i=$i+1
done

4. until

Repetitive structure until the condition is true (i.e. as long as it is false).

1
2
3
until condition ; do
    instructions
done

Loop whose instructions are executed as long as the condition expression is false.

5. case

Multiple choice conditional structure: depending on the value of the string expression, we can execute a wide range of instructions.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
case string in
    pattern_1)
      instruction1
    ;;
    pattern_2)
      instruction2
    ;;
    *)
      the rest
    ;;
esac

If the string is similar to pattern_1 (which is a path expansion string, accepts meta characters * ?, [], {} and ~) then instruction1 is executed.

The string can perfectly verify several different patterns simultaneously, in this case the corresponding instructions (or blocks of instructions) will all be executed.

6. Tests

The conditions of the structures can be evaluated using the test command.

6.1 String Tests

1
test -z string

Returns true if string is empty.

1
test -n string

Returns true if string is not empty.

1
test string_1 = string_2

Returns true if the two strings are equal.

1
test string_1 != string_2

Returns true if the two strings are not equal.

6.2 Numeric Tests

1
test string_1 operator string_2

Returns true if strings string_1 and string_2 are in the relational order defined by the operator which can be one among those in the table below.

OperatorMeaning
-eq=
-ne<>
-lt<
-le<=
-gt>
-ge>=

6.3 File Tests

1
test condition file

Returns true if the file meets the condition which can be one among those described in the table below.

OperatorReturns true if
-pit’s a named pipe
-fregular file
-ddirectory
-cspecial file in character mode
-bspecial file in block mode
-rread access
-wwrite access
-xexecution access
-snon-empty file

6.4 Other Operators

It is possible to combine tests by using parentheses and the boolean operators of the following table:

OperatorMeaning
!negation
-aconjunction (and)
-odisjunction (or)

Last updated 21 Oct 2008, 15:22 CEST. history