Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

SLAX Operators

SLAX provides a variety of operators, which add great versatility to the SLAX scripting language. Table 1 summarizes the available operators and provides an example and an explanation of each.

Table 1: SLAX Operators

Name

Operator

Example / Explanation

Addition

+

var $result = 1 + 1;

Return the sum of the operands. This example assigns a value of 2 to the $result variable.

And

&&

($byte-count > 500000) && ($byte-count < 1000000)

Evaluate two expressions and return one boolean result. If either of the two expressions evaluates to false, then the combined expression evaluates to false.

Assignment

=

var $mtu = 1500;
mvar $mtu2 = 48;
set $mtu2 = 1500;

Assign a value to a variable or parameter or assign a namespace to a prefix. The example assigns a value of 1500 to both the $mtu variable and the $mtu2 mutable variable. $mtu2 was originally initialized with a value of 48.

Conditional

?:

var $result = ($a < 10) ? $b : $c;

Provide conditional assignment based on the boolean value of the evaluated condition. If the conditional expression evaluates to true, the entire expression assumes the value of the operand to the left of the colon. If the conditional expression evaluates to false, the entire expression assumes the value of the operand to the right of the colon. This operator was introduced in version 1.1 of the SLAX language, which is supported starting with Junos  OS Release 12.2.

In the example, if the value stored in the variable $a is less than 10, $result is assigned the value stored in $b. Otherwise, $result is assigned the value stored in $c.

Division

div

<output>$bit-count div 8;

Return the result of dividing the left operand by the right operand. If the remainder of the division is nonzero, the result is expressed in decimal floating-point notation. The example divides the $bit-count variable by eight, returning the byte count (requires that $bit-count has been initialized).

Equality

==

$mtu == 1500

Return true if the values of the left and right operands are equal; otherwise, the expression returns false. In the example, if $mtu equals 1500, then the expression resolves to true; otherwise, it returns false (requires that $mtu has been initialized).

Greater than

>

$hop-count > 0

Return true if the value of the left operand is greater than the value of the right operand; otherwise, the expression returns false. In this example, if $hop-count is greater than zero, the expression returns true (requires that $hop-count has been initialized).

Greater than or equal to

>=

$hop-count >= 1

Return true if the value of the left operand is either greater than or equal to the value of the right operand; otherwise, the expression returns false. In this example, if $hop-count is 1 or greater, the expression returns true (requires that $hop-count has been initialized).

Inequality

!=

$mtu != 1500

Return true if the values of the left and right operands are not equal; otherwise, the expression returns false. In the example, if $mtu does not equal 1500, then the expression resolves to true; otherwise, the expression returns false (requires that $mtu has been initialized)

Iteration

...

for $i (1 ... 10) { 
  <player number=$i>;
}

Iterate through a range of integer values with a start value equal to the left operand and an end value equal to the right operand. If the left operand is greater than the right, the numbers are generated in decreasing order. The operator translates into an XPath function that generates the sequence as a node set. This operator was introduced in version 1.1 of the SLAX language, which is supported starting with Junos OS Release 12.2.

Less than

<

$hop-count < 15

Return true if the value of the left operand is less than the value of the right operand; otherwise, the expression returns false. In this example, if $hop-count is less than 15, the expression returns true (requires that $hop-count has been initialized).

Less than or equal to

<=

$hop-count <= 14

Return true if the value of the left operand is either less than or equal to the value of the right operand; otherwise, the expression returns false. In this example, if $hop-count is 14 or less, the expression returns true (requires that $hop-count has been initialized).

Modulo

mod

<output> 10 mod 3;

Return the division remainder of two numbers. In this example, the expression outputs a value of 1.

Multiplication

*

<output> 5 * 10;

Return the product of the operands. In this example, the expression outputs a value of 50.

Node Set, append to

+=

mvar $block = <block> "start here";
append $block += <block> "next block";

Append a value to a node set contained in a mutable variable, which is defined with the mvar statement. This operator was introduced in version 1.1 of the SLAX language, which is supported starting with Junos  OS Release 12.2.

Node Set Conversion

:=

var $new-node-set := $rtf-variable;

Convert a result tree fragment into a node set. A result tree fragment contains an unparsed XML data structure. It is not possible to retrieve any of the embedded XML information from this data type. A script can convert the result tree fragment into a node set and then search the node set for the appropriate information and extract it. This operator is supported in Junos OS Release 9.2 and later releases.

Or

||

($mtu-size != 1500) || ($mtu-size > 2000)

Evaluate two expressions and return one boolean result. If either of the two expressions evaluates to true, then the combined expression evaluates to true.

Parentheses

( )

var $result = ( $byte-count * 8 ) + 150;

Create complex expressions. Parentheses function the same way as in a mathematical expression, where the expression within the parentheses is evaluated first. Parentheses can be nested; the innermost set of parentheses is evaluated first, then the next set, and so on.

String concatenation

_(under-score)

var $combined-string = $host-name _ " is located at “ _ $location;

Concatenate multiple strings (note that strings cannot be combined using the + operator in SLAX). In the example, if $host-name is “r1” and $location is “HQ”, then the value of $combined-string is “r1 is located at HQ”.

Subtraction

-

var $result = 64 - 14;

Return the difference between the left operand and the right operand. This example assigns a value of 50 to the $result variable.

Unary Minus

-

mvar $number = 5.
set $number = - $number;

Negate the value of the operand, changing a positive value to a negative value or a negative value to a positive value. The example negates the value stored in $number and reassigns the new value of -5 to the variable.

Union

|

var $all-interface-nodes = $fe-interface-nodes | $ge-interface-nodes;

Create a union of two node sets. All the nodes from one set combine with the nodes in the second set. This is useful when a script needs to perform a similar operation over XML nodes that are pulled from multiple sources.