Writing scripts code in Winbox is a PITA. I check my syntax by copying it to a terminal and see there are errors in it and test it directly if possible. This manual copy and paste can be much easier if in the boxes where we can input scripts, select the script code and then right-click and choose Check Selected.
What this does is to take the selected script code and remove the remark lines (starting with #) and combine the left script code to one script code-line. The assembled script code-line is then pasted to a new or already exiting Terminal and then on a new line.
Example of a simple script with an syntax error in it:
#Check this code
:put "This code is to be checked";
# Also this code
:put "And this" code also";
Past to Terminal is:
:put "This code is to be checked"; :put "And this" code also";
I assume there is a maximum length of a line in Terminal. Checking if the selected code, minus the comments and the inserted space(s), replacing the Newline or/and Return, is mandatory.
This will make the life of script-kiddies and script-elderly much easier and I hope I can get some support for this so that Mikrotik considers to implement this.
I have have been a lot of coding in RouterOS lately and have some more insight now how to syntax check can bea lot easier.
I first said that combining to one line was good, however there is a better way.
If I want to check code now I add an "" at the end of the list and then select it and past it into the Terminal.
A little script where a needed “;” is missing:
:local a 100;
:global b 400;
:put a
:log info "test;
:put “Hello world”;\
If you past it then you see the following:
> :local a 100;\
\... :global b 400;\
\... :put a\
\... :log info "test;\
expected end of command (line 4 column 1)
The red cursor is on the “:” of “:log” and that is because the previous line is missing a “;”.
If you don’t use the "" the lines are executed and you don’t get an error:
[me@MikroTik] > :local a 100;
[me@MikroTik] > :global b 400;
[me@MikroTik] > :put a
a
[me@MikroTik] > :log info "test;
[me@MikroTik] > :put "Hello world";
Hello world
It is displayed if you use { and } to contain the bit of code:
{
#Loop through names of the peers and see if they need restart
:foreach peerName in=[find] do={
:lset $pn [get $peerName name ];
:if (![get $peerName disabled]) do={:if ([/ip ipsec policy find peer=$pn]="") do= {:log error "$pn not active, restarting.."; disable $pn; :delay 1500ms; enable $pn}};
}; # end of foreach peerN....
}
I find it helpfull to revese the code by selecting it;
An other place where a syntax check could be done is when resting you mouse pointer on the on-event line in scheduler or script then you get the code shown in black letters and a yellow background.