UDM Command Format - Expressions

UDM Command Format - Expressions

The following basic rules apply to expressions in UDM commands.

Delimiters

All expressions must be bound by left angle ( < ) and right angle ( > ) brackets.

For example:

set value=<2 + 2>

Operand / Operator Delimiters

Operands and operators in an expression must be separated by a space.

For example, the following is not legal:

<2+4>

There must be a space before and after the operator < + >:

<2 + 4>

Appearance

An expression can appear either as a parameter or its value. It must comprise the entire parameter or value in which it appears, not just part of it.

For example, in the following command, <2 + 2> is not an expression:

echo "2 + 2 = <2 + 2>"

It is merely part of the quoted string, and the output would be:

2 + 2 = <2 + 2>

In order to treat <2 + 2> as an expression, the command must be:

echo "2 + 2 = " <2 + 2>

The output of this command is:

2 + 2 = 4

Integer Only

Although floating point number are allowed in expressions, everything is evaluated as an integer. The only exception is that the EQ - Equal and NE - Not Equal comparators can be used to compare strings as well as numbers.

Operator Precedence

The operator order of precedence (and reading left to right) is:

  1. NOT
  2. *, /, and %
  3. + and -
  4. EQ, NE, LT, GT, LE, and GE
  5. AND, OR, and XOR

To manually indicate that an operation is of higher precedence, enclose it in parentheses: ( and ). An expression is evaluated going from the inner most set of parentheses out. Sets on the same level are evaluated left to right.

The following examples illustrates how parentheses can affect results.

In the following expression, where < * > takes precedence over < + >:

<5 + 4 * 2>

The expression yields the following value:

13

However, when the expression includes the following parentheses:

<(5 + 4) * 2>

The expression yields the following value:

18

Nesting

Expressions can be nested in order to indicate desired change precedence. Nested expressions are bound by parentheses: ( and ).

When nested expressions are a part of an expression, the deepest nested portions are evaluated first. Also, spaces are not required between the ( and ) when they are used for nesting, as they are between operators and operands.

For example:

Operations

All operations take the following form:

left-value operator right-value

The left-value and right-value can be:

  • Strings (EQ and NE only)
  • Numbers
  • Variable references
  • Other operations

The following table identifies and describes all of the operators for UDM command expressions.

Operator

Description

EQ

Compares the value on the left to the value on the right. If the two are equal, the result is 1, otherwise it is 0. Both the left an right values can be strings or numbers. If they are strings, the comparison is case insensitive.

NE

Works like the equal operator, except that it results in 1 if the left and right value are not equal and 0 if they are equal.

LT

Results in a value of 1 if the left value is less than the right value, otherwise it results in 0. This is a numeric operator.

GT

Results in a value of 1 if the left value is greater than the right value, otherwise it results in 0. This is a numeric operator.

LE

Results in a value of 1 if the left value is less than or equal to the right value, otherwise it results in 0. This is a numeric operator.

GE

Results in a value of 1 if the left value is greater than or equal to the right value, otherwise it results in 0. This is a numeric operator.

AND

Results in a value of 1 if both the left value and right value are not 0, otherwise it results in 0.

OR

Results in a value of 1 if either the left value or the right value are not 0, otherwise it results in 0.

XOR

Results in a value of 1 if either the left or the right values are not 0, but not both. If both the left and right values are 0, then the result of the XOR operator is 0.

NOT

Unlike all of the operators, the NOT operator has only one operand that appears to the right of the NOT operator. This operation evaluates to one if the operand is zero and zero if the operand is non-zero.

+

Result is the sum of the left and right values.

-

Result is subtracting the right value from the left value.

*

Result is the product of the left and right values.

/

Result is the left value divided by the right value. Assuming integer-only math, the remainder is discarded.

%

Result is the remainder of the left value divided by the right value.