Manual:Scripting: Difference between revisions

From MikroTik Wiki
Jump to navigation Jump to search
mNo edit summary
No edit summary
Line 11: Line 11:


<h4>Physical Line</h4>
<h4>Physical Line</h4>
<p>A physical line is a sequence of characters terminated by an end-of-line (EOL) sequence. Any of the standard platform line termination sequences can be used:<ul>
<p>A physical line is a sequence of characters terminated by an end-of-line (EOL) sequence. Any of the standard platform line termination sequences can be used:
<li> unix     – ASCII LF;
<table>
<li> windows   – ASCII CR LF;
<tr>
<li> mac       – ASCII CR;
<td width="30"></td><td width="150"><b><var>unix</var></b></td><td >– ASCII LF</td>
</ul><br>
</tr><tr>
<tr>
<td width="30"></td><td width="150"><b><var>windows</var></b></td><td >– ASCII CR LF;</td>
</tr><tr>
<tr>
<td width="30"></td><td width="150"><b><var>mac</var></b></td><td >– ASCII CR;</td>
</tr>
</table>
 
Standard C conventions for new line characters can be used ( the \n character).
Standard C conventions for new line characters can be used ( the \n character).
</p>
</p>
Line 85: Line 93:
<h3>Data types</h3>
<h3>Data types</h3>
<p>
<p>
RouterOS scripting language has following data types:<ul>
RouterOS scripting language has following data types:
<li><b>number</b>     - 64bit signed integer, possible hexadecimal input;
<table>
<li><b>boolean</b>     - values can bee <code>true</code> or <code>false</code>;
<tr>
<li><b>string</b>     - character sequence (??? kaads ir memas limits ???);
<td width="30"></td><td width="150"><b><var>number</var></b></td><td >- 64bit signed integer, possible hexadecimal input;</td>
<li><b>IP</b>         - IP address;
</tr><tr>
<li><b>internal ID</b> - hexadecimal value prefixed by '*' sign. Each menu item has assigned unique number - internal ID;
<td width="30"></td><td width="150"><b><var>boolean</var></b></td><td >- values can bee <code>true</code> or <code>false</code>;</td>
<li><b>time</b>       - date and time value;
</tr><tr>
<li><b>array</b>       - sequence of values organized in an array.
<td width="30"></td><td width="150"><b><var>string</var></b></td><td >- character sequence (??? kaads ir memas limits ???);</td>
</ul>
</tr><tr>
<td width="30"></td><td width="150"><b><var>IP</var></b></td><td >- IP address;</td>
</tr><tr>
<td width="30"></td><td width="150"><b><var>internal ID</var></b></td><td >- hexadecimal value prefixed by '*' sign. Each menu item has assigned unique number - internal ID;</td>
</tr><tr>
<td width="30"></td><td width="150"><b><var>time</var></b></td><td >- date and time value;</td>
</tr><tr>
<td width="30"></td><td width="150"><b><var>array</var></b></td><td >- sequence of values organized in an array;</td>
</tr><tr>
<td width="30"></td><td width="150"><b><var>nothing</var></b></td><td >- default variable type if no value is assigned;</td>
</tr>
</table>
 
</p>
</p>
<h4>Constant Escape Sequences</h4>
<h4>Constant Escape Sequences</h4>
<p>
<p>
Following escape sequences can be used to define certain special character within string:
Following escape sequences can be used to define certain special character within string:
\' - single quote
<table>
\" - double quote
<tr>
\\ - backslash
<td width="30"></td><td width="150"><b>\'</b></td><td > - single quote</td>
\n - newline
</tr><tr>
\r - carriage return
<td width="30"></td><td width="150"><b>\"</b></td><td > - double quote</td>
\t - horizontal tab
</tr><tr>
\$
<td width="30"></td><td width="150"><b>\\</b></td><td > - backslash</td>
</tr><tr>
<td width="30"></td><td width="150"><b>\n</b></td><td > - newline</td>
</tr><tr>
<td width="30"></td><td width="150"><b>\r</b></td><td > - carriage return</td>
</tr><tr>
<td width="30"></td><td width="150"><b>\t</b></td><td > - horizontal tab</td>
</tr><tr>
<td width="30"></td><td width="150"><b>\$</b></td><td ></td>
</tr>
</table>
(MRZ: varbuut veel kaads)
(MRZ: varbuut veel kaads)
</p>
 
<h5>Example</h5>
<h5>Example</h5>
<pre><nowiki>
<pre><nowiki>
:put "This\nis\na\ntest";
:put "This\nis\na\ntest";
</nowiki>
</nowiki></pre>
</pre>


which will show on display
which will show on display<br />
This
This<br />
    is
is<br />
      a
a<br />
      test
test<br />
( MRZ: bugs vai fiicha ??? ka whitespaci katras jaunas rindas priekshaa??? ).
( MRZ: bugs vai fiicha ??? ka whitespaci katras jaunas rindas priekshaa??? ).
</p>
<h3>Operators</h3>
lala
<h4>Arithmetic Operators</h4>
<p>
Usual arithmetic operators are supported in RouterOS scripting language<br />
<table border=0 bgcolor=#4f4f4f>
<tr border=0 bgcolor=#cccccc>
  <th bgcolor=#ffffff width="30" align="left"></th>
  <th width="100" align="left">Opearator</th>
  <th width="200" align="left">Description</th>
  <th align="left">Example</th>
</tr>
<tr border=0 bgcolor=#f4f4f4>
  <td bgcolor=#ffffff width="30"></td>
  <td width="100"><b><var>"+"</var></b></td>
  <td width="200">binary addition</td>
  <td><code>{ :local a 3; :local b 4; put (a+b); }</code></td>
</tr>
<tr border=0 bgcolor=#f4f4f4>
  <td bgcolor=#ffffff width="30"></td>
  <td width="100"><b><var>"-"</var></b></td>
  <td width="200">binary subtraction</td>
  <td><code>{ :local a 3; :local b 4; put (a-b); }</code></td>
</tr>
<tr border=0 bgcolor=#f4f4f4>
  <td bgcolor=#ffffff width="30"></td>
  <td width="100"><b><var>"*"</var></b></td>
  <td width="200">binary multiplication</td>
  <td><code>{ :local a 3; :local b 4; put (a*b); }</code></td>
</tr>
<tr border=0 bgcolor=#f4f4f4>
  <td bgcolor=#ffffff width="30"></td>
  <td width="100"><b><var>"/"</var></b></td>
  <td width="200">binary division</td>
  <td><code>{ :local a 10; :local b 2; put (a*b); }</code></td>
</tr>
<tr border=0 bgcolor=#f4f4f4>
  <td bgcolor=#ffffff width="30"></td>
  <td width="100"><b><var>"-"</var></b></td>
  <td width="200">unary negation</td>
  <td><code>{ :local a 1; put (-a); }</code></td>
</tr>
<tr border=0 bgcolor=#f4f4f4>
  <td bgcolor=#ffffff width="30"></td>
  <td width="100"><b><var>"+"</var></b></td>
  <td width="200">unary plus</td>
  <td></td>
</tr>
</table>
</p>
<h4>Relational Operators</h4>
<p>
<table border=0 bgcolor=#4f4f4f>
<tr border=0 bgcolor=#cccccc>
  <th bgcolor=#ffffff width="30" align="left"></th>
  <th width="100" align="left">Opearator</th>
  <th width="200" align="left">Description</th>
  <th align="left">Example</th>
</tr>
<tr border=0 bgcolor=#f4f4f4>
  <td bgcolor=#ffffff width="30"></td>
  <td width="100"><b><var>"<"</var></b></td>
  <td width="200">less</td>
  <td><code>{ :local a 3; :local b 4; put (a<b); }</code></td>
</tr>
<tr border=0 bgcolor=#f4f4f4>
  <td bgcolor=#ffffff width="30"></td>
  <td width="100"><b><var>">"</var></b></td>
  <td width="200">greater</td>
  <td><code>{ :local a 3; :local b 4; put (a>b); }</code></td>
</tr>
<tr border=0 bgcolor=#f4f4f4>
  <td bgcolor=#ffffff width="30"></td>
  <td width="100"><b><var>"="</var></b></td>
  <td width="200">equal</td>
  <td><code>{ :local a 1; :local b 1; put (a=b); }</code></td>
</tr>
<tr border=0 bgcolor=#f4f4f4>
  <td bgcolor=#ffffff width="30"></td>
  <td width="100"><b><var>"<="</var></b></td>
  <td width="200">less or equal</td>
  <td></td>
</tr>
<tr border=0 bgcolor=#f4f4f4>
  <td bgcolor=#ffffff width="30"></td>
  <td width="100"><b><var>">="</var></b></td>
  <td width="200">greater or equal</td>
  <td></td>
</tr>
<tr border=0 bgcolor=#f4f4f4>
  <td bgcolor=#ffffff width="30"></td>
  <td width="100"><b><var>"!="</var></b></td>
  <td width="200">not equal</td>
  <td></td>
</tr>
</table>


</p>


</div>
</div>

Revision as of 08:28, 26 September 2008

Scripting language manual

lala

Line structure

RouterOS script is divided into number of logical lines.

Logical line

The end of logical line is represented by the token “;” or NEWLINE. Logical line can be constructed from more than one physical line by following line joining rules.

Physical Line

A physical line is a sequence of characters terminated by an end-of-line (EOL) sequence. Any of the standard platform line termination sequences can be used:

unix– ASCII LF
windows– ASCII CR LF;
mac– ASCII CR;

Standard C conventions for new line characters can be used ( the \n character).

Comments

A comment starts with a hash character (#) and ends at the end of the physical line. A comment signifies the end of the logical line unless line joining rules are invoked ( nav taisniiba, skat piemeerus). Comments are ignored by syntax. If (#) character appear inside string it is not considered a comment.

Example
# this is a comment

:global a; # bad comment (MRZ: shitadu vajadzetu atlaut)

:global myStr "lala # this is not a comment"

Line joining

Two or more physical lines may be joined into logical lines using backslash character (\). A line ending in a backslash cannot carry a comment. A backslash does not continue a comment. A backslash does not continue a token except for string literals. (MRZ: nezinu vai shii ir taisniiba, buutu logiski ja taa buutu). A backslash is illegal elsewhere on a line outside a string literal.

Example
:if ($a = true \
      and $b=false) do={ :put “$a $b”; }

:if ($a = true \      # bad comment
      and $b=false) do={ :put “$a $b”; }

# comment \
    continued – invalid  (syntax error)

Whitespace between tokens

Whitespace can be used to separate tokens. Whitespace is necessary between two tokens only if their concatenation could be interpreted as a different token.

Example
{
   :local a true; :local b false;
   :put (a&&b);      # whitespace is not required
   :put (a and b);   # whitespace is required
}


Keywords

( Vai RouterOSaa vispaar ir keywordi??? )
The following words are keywords and cannot be used as variable and function names:

and       or        not       break
continue  do        for       foreach
in        from      to        while
global    local     if        else

(break un continue vajadzeetu ieviest)
(varbuut veel kaads?)

Delimiters

The following tokens serve as delimiters in the grammar:

()  []  {}  :   ;   $   / 

Data types

RouterOS scripting language has following data types:

number- 64bit signed integer, possible hexadecimal input;
boolean- values can bee true or false;
string- character sequence (??? kaads ir memas limits ???);
IP- IP address;
internal ID- hexadecimal value prefixed by '*' sign. Each menu item has assigned unique number - internal ID;
time- date and time value;
array- sequence of values organized in an array;
nothing- default variable type if no value is assigned;

Constant Escape Sequences

Following escape sequences can be used to define certain special character within string:

\' - single quote
\" - double quote
\\ - backslash
\n - newline
\r - carriage return
\t - horizontal tab
\$

(MRZ: varbuut veel kaads)

Example
:put "This\nis\na\ntest";

which will show on display
This
is
a
test
( MRZ: bugs vai fiicha ??? ka whitespaci katras jaunas rindas priekshaa??? ).

Operators

lala

Arithmetic Operators

Usual arithmetic operators are supported in RouterOS scripting language

Opearator Description Example
"+" binary addition { :local a 3; :local b 4; put (a+b); }
"-" binary subtraction { :local a 3; :local b 4; put (a-b); }
"*" binary multiplication { :local a 3; :local b 4; put (a*b); }
"/" binary division { :local a 10; :local b 2; put (a*b); }
"-" unary negation { :local a 1; put (-a); }
"+" unary plus

Relational Operators

Opearator Description Example
"<" less { :local a 3; :local b 4; put (a<b); }
">" greater { :local a 3; :local b 4; put (a>b); }
"=" equal { :local a 1; :local b 1; put (a=b); }
"<=" less or equal
">=" greater or equal
"!=" not equal