If...Then...Fi

You can use the If statement to conditionally execute a block of statements.

Syntax

If bool_expression Then
 code;
Elif bool_expression Then
 code;
Elif bool_expression Then
  code;
Else
  code;
Fi;

If bool_expression Then is the first expression.

code; is executed if the first expression is True.

Elif bool_expression Then is the second expression.

code; is executed if the first expression is False and the second is True.

Else is executed if none of the preceding expressions is True.

The instruction If … Then … Fi can contain zero, one or more Elif … Then … parts, which specify alternative conditions selected if none of the preceding conditions evaluated to True.

The instruction If … Then … Fi can optionally end with an Else … statement, which is executed if none of the preceding conditions evaluated to True.

Note the closing semicolon of each expression and the closing semicolon after the Fi.

KCM Core first evaluates the bool_expression between the If and Then statements:

  • If this expression evaluates to True, KCM Core executes the instructions following the Then statement up to the next Elif, Else, or Fi and then continues executing instructions after Fi.
  • If this expression evaluates to False, KCM Core skips the following instructions and continues at the next Elif, Else, or Fi. If the next instruction is an Elif statement, KCM Core repeats this process by evaluating the expression between the Elif and Then statements. If the next instruction is an Else statement, KCM Core executes the instructions between Else and Fi.

The conditional blocks of statements can contain any valid KCM Core instructions including other conditional statements. The only exception is the Parameter statement, which you can only use at the beginning of the script.