if

Overview

The if statement is used to conditionally execute one or more statements.

Syntax

if (conditional expression) {
    <statements ...>
} [else {
    <statements ...>
}]

Conditional Expressions

A conditional expression (hereafter referred to as simply an expression) is evaluated to provide a TRUE or FALSE result which in turn determines whether one or more statements are to be executed or not. The following are examples of a valid expression:

(${dataDate} == 20180801)

((${dataDate} >= 20180801) && ([hostname] == "templateVM"))

An expression used by the if statement may contain:

  • Numeric and string literals

  • Regular expressions

  • Variables

  • Operators

  • Functions

Numeric and string literals

A literal is a specified value, such as 4.5 or "hostname". Literals may be numbers or strings (text).

If a literal is non-quoted then it will be treated as a number if it represents a valid decimal integer or floating point number (in either regular or scientific notation), else it will be treated as a string.

If a literal is quoted then it is always treated as a string, thus 3.1515926 is a number and "3.1415926" is a string.

Regular expressions

Regular expressions must be enclosed within forwarding slashes (/), and are assumed to be in ECMAScript format.

If present, a regular expression must be used on the right-hand side of either an !~ or an =~ operator, and when evaluated it will be applied to the value on the left-hand side of an operator, for example:

if (${dataDate} =~ /[0-9]{4}01/) {
    var first_day_of_month = yes
} else {
    var first_day_of_month = no
}

As the forward slash is used as a delimiter for the expression, any literal forward slashes required by the expression should be escaped with a backslash: \/

Variables

Variables can be used within expressions, in which case they are replaced with their values. Once expanded, these values are treated as literals.

Operators

Operators are evaluated according to the operator precedence rules in the table below (where the highest precedence is evaluated first), unless parentheses are used to override them. Operators with the same precedence are evaluated from left to right.

Precedence

Operator

Meaning

1

!

Unary negation

2

*

Multiplication

2

/

Division

2

%

Modulo

3

+

Addition

3

-

Subtraction

4

<

Less than

4

<=

Less than or equal to

4

>

Greater than

4

>=

Greater than or equal to

5

==

Is equal to

5

!=

Is not equal to

5

=~

Matches regular expression

5

!~

Does not match regular expression

6

&&

Boolean AND

7

`

`

Boolean OR

Although expressions are evaluated based on the precedence of each operator as listed in the above table, it is recommended that parenthesis are used within the expression in order to remove any ambiguity on the part of a future reader.

Last updated