...
Code Block | ||
---|---|---|
| ||
{ :local a 3; { :global b 4; } :global b; :put ($a+$b); } | ||
Info |
Code Block | ||
---|---|---|
| ||
:local name aa; /file/print where name=$name
|
Correct.
Code Block | ||
---|---|---|
| ||
:local nm aa; /file/print where name=$nm |
Keywords
The following words are keywords and cannot be used as variable and function names:
...
Command | Syntax | Description | Example | |||||||
---|---|---|---|---|---|---|---|---|---|---|
/ | go to the root menu | |||||||||
.. | go back by one menu level | |||||||||
? | list all available menu commands and brief descriptions | |||||||||
global | :global <var> [<value>] | define a global variable | :global myVar "something"; :put $myVar; | |||||||
local | :local <var> [<value>] | define the local variable | { :local myLocalVar "I am local"; :put $myVar; } | |||||||
beep | :beep <freq> <length> | beep built-in speaker | ||||||||
convert | :convert from=[arg] to=[arg] | Converts specified value from one format to another. By default uses an automatically parsed value, if the "from" format is not specified (for example, "001" becomes "1", "10.1" becomes "10.0.0.1", etc.). from specifies the format of the value - base32, base64, hex, raw, rot13, url. to specifies the format of the output value - base32, base64, hex, raw, rot13, url. transform to transform values - lc (transforms value to be in lowercases), uc (uppercases), lcfirst (first value to lowercase), ucfirst (first value to uppercase) |
| |||||||
delay | :delay <time> | do nothing for a given period of time | ||||||||
environment | :environment print <start> | print initialized variable information | :global myVar true; :environment print; | |||||||
error | :error <output> | Generate console error and stop executing the script | ||||||||
execute | :execute <expression> | Execute the script in the background. The result can be written in the file by setting a "file"parameter or printed to the CLI by setting "as-string". When using the "as-string" parameter executed script is blocked (not executed in the background). Executed script can not be larger than 64kB |
| |||||||
find | :find <arg> <arg> <start> | return position of a substring or array element | :put [:find "abc" "a" -1]; | |||||||
jobname | :jobname | return current script name |
| |||||||
len | :len <expression> | return string length or array element count | :put [:len "length=8"]; | |||||||
log | :log <topic> <message> | write a message to the system log. Available topics are "debug, error, info and warning" | :log info "Hello from script"; | |||||||
onerror | :onerror <var_name> in={<command>} do={<expression>} | The command used to catch errors and get error details. The do={...} block is executed, when in={...} block has an error, and error details are written in <var_name> variable. warningParameter order is important. The "error" parameter must be set before "do" block, otherwise do block will not see the local variable.
| :onerror errorName in={ :error "failure" } do={ :put "Critical $errorName" } | |||||||
parse | :parse <expression> | parse the string and return parsed console commands. Can be used as a function. | :global myFunc [:parse ":put hello!"]; | |||||||
pick | :pick <var> <start>[<end><count>] | return range of elements or substring. If the end is count is not specified, will return only one element from an array.
|
| |||||||
put | :put <expression> | put the supplied argument into the console | :put "Hello world" | |||||||
resolve | :resolve <arg> | return the IP address of the given DNS name | :put [:resolve "www.mikrotik.com"]; | |||||||
retry | :retry command=<expr> delay=[num] max=[num] on-error=<expr> | Try to execute the given command "max" amount of times with a given "delay" between tries. On failure, execute the expression given in the "on-error" block | :retry command={abc} delay=1 max=2 on-error={:put "got error"}
| |||||||
typeof | :typeof <var> | the return data type of variableserialize | : serialize put [ <value>]:typeof 4]; | |||||||
rndnum | :rndnum from to=[ arg] | Serialize specified value/array to JSON format. Specify the format using to=json. | num] to=[num] | random number generator Code Block | | language | ros:put [: serialize value=a,b,c to=json]
["a","b","c"] | |||
deserialize | :deserialize [<value>] from=[arg] | Deserialize specified value/array from JSON format. Specify the format using from=json. |
| |||||||
typeof | :typeof <var> | the return data type of variable | :put [:typeof 4]; | |||||||
rndnum | :rndnum from=[num] to=[num] | random number generator | :put [:rndnum from=1 to=99]; | rndstr | :rndstr from=[str] length=[num] | Random string generator. from specifies characters to construct the string from and defaults to all ASCII letters and numerals. | ||||
set | :set <var> [<value>] | assign value to a declared variable. | :global a; :set a true; | terminal | :terminal | terminal related commands | ||||
time | :time <expression> | return interval of time needed to execute the command | :put [:time {:for i from=1 to=10 do={ :delay 100ms }}]; | timestamp | :timestamp | returns the time since epoch, where epoch is January 1, 1970 (Thursday), not counting leap seconds |
Code Block | ||
---|---|---|
| ||
[admin@MikroTik] > :put [:timestamp]
2735w21:41:43.481891543
or
[admin@MikroTik] > :put [:timestamp]
2735w1d21:41:43.481891543
with the day offset |
rndnum from=1 to=99];
:rndstr from=[str] length=[num]
Random string generator.
from specifies characters to construct the string from and defaults to all ASCII letters and numerals.
length specifies the length of the string to create and defaults to 16.
:put [:rndnum from="abcdef%^&
" length=33];
:set <var> [<value>]
:global a; :set a true;
:serialize [<value>] to=[arg]
Serialize specified value/array to JSON or dsv (delimeter separated values) format.
to specifies the format - json, dsv
delimeter sets the "separator".
oder specifies the order for variables.
options specifies additional options - json.pretty (makes the JSON output more visually appealing), dsv.wrap-strings (wraps string values inside quatation marks), dsv.ignore-size (if array values have different sizes, e.g. a=(1,2);b=(3,4);c=(5,6,7)
,this option will work around array size mismatch
error and set "empty" values in those slots).
Code Block | ||
---|---|---|
| ||
:put [:serialize value=a,b,c to=json]
["a","b","c"]
:local test {a=(1,2,3);b=(4,5,6);c=(7,"text",9)}; :put [ :serialize to=dsv delimiter=";" value=$test order=("c","a","b") ]
c;a;b
7;1;4
text;2;5
9;3;6 |
:deserialize [<value>] from=[arg]
Deserialize specified value/array from JSON or dsv (delimeter separated values) format.
from specifies the format - json, dsv
See "serialize" above for more parameters.
Code Block | ||
---|---|---|
| ||
:put [:deserialize from=json value="[\"a\",\"b\",\"c\"]"]
a;b;c |
:time <expression>
:put [:time {:for i from=1 to=10 do={ :delay 100ms }}];
:timestamp
Code Block | ||
---|---|---|
| ||
[admin@MikroTik] > :put [:timestamp]
2735w21:41:43.481891543
or
[admin@MikroTik] > :put [:timestamp]
2735w1d21:41:43.481891543
with the day offset |
:toarray <var>
:tobool <var>
:toid <var>
:toip <var>
:toip6 <var>
:tonum <var>
:tostr <var>
:totime <var>
:toarray <var>
:tobool <var>
:toid <var>
:toip <var>
:toip6 <var>
:tonum <var>
:tostr <var>
:totime <var>
Code Block | ||
---|---|---|
| ||
[admin@191] > :put [ totime "1970-01-11 12:33" ]
1w3d12:33:00
[admin@191] > :put [ totime "1970-02-11" ]
5w6d00:00:00
[admin@191] > :put [ totime 3 ]
00:00:03
[admin@191] > :put [ totime "1000d" ]
142w6d00:00:00 |
:tocrlf [<value>]
:tolf [<value>]
Menu specific commands
Common commands
...