System information

Manual:Scripting
29
Other Operators
Opearator Description Example
[] command substitution. Can contain only single command line :put [ :len "my test string"; ];
() sub expression or grouping operator :put ( "value is " . (4+5));
$ substitution operator :global a 5; :put $a;
~ binary operator that matches value against POSIX extended regular
expression
Print all routes which gateway ends with 202
/ip route print where gateway~"^[0-9
\\.]*202"
-> Get an array element by key [admin@x86] >:global aaa {a=1;b=2}
[admin@x86] > :put ($aaa->"a")
1
[admin@x86] > :put ($aaa->"b")
2
Variables
Scripting language has two types of variables:
global - accessible from all scripts created by current user, defined by global keyword;
local - accessible only within the current scope, defined by local keyword.
Note: Starting from v6.2 there can be undefined variables. When variable is undefined parser will try to look
for variables set, for example, by DHCP lease-script or Hotspot on-login
Every variable, except for built in RouterOS variables, must be declared before usage by local or
global keywords. Undefined variables will be marked as undefined and will result in compilation
error. Example:
# following code will result in compilation error, because myVar is used without declaration
:set myVar "my value";
:put $myVar
Correct code:
:local myVar;
:set myVar "my value";
:put $myVar;
Exception is when using variables set, for example, by DHCP lease-script
/system script
add name=myLeaseScript policy=\
ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api \
source=":log info \$leaseActIP\r\
\n:log info \$leaseActMAC\r\
\n:log info \$leaseServerName\r\
\n:log info \$leaseBound"
/ip dhcp-server set myServer lease-script=myLeaseScript
Valid characters in variable names are letters and digits. If variable name contains any other character, then variable
name should be put in double quotes. Example: