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.
Name |
Operator |
Example / Explanation |
---|---|---|
Addition |
+ |
var $result = 1 + 1; Return the sum of the operands. This example assigns a value
of 2 to the |
And |
&& |
($byte-count > 500000) && ($byte-count < 1000000) Evaluate two expressions and return one boolean result. If either of
the two expressions evaluates to |
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 |
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 In the example, if the value stored in the variable |
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 |
Equality |
== |
$mtu == 1500 Return |
Greater than |
> |
$hop-count > 0 Return |
Greater than or equal to |
>= |
$hop-count >= 1 Return |
Inequality |
!= |
$mtu != 1500 Return |
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 |
Less than or equal to |
<= |
$hop-count <= 14 Return |
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 |
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 |
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 |
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 |
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. |