Community discussions

MikroTik App
 
msatter
Forum Guru
Forum Guru
Topic Author
Posts: 2897
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Script code syntax check [Check Selected]

Sat Mar 21, 2020 9:39 am

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.
 
Sob
Forum Guru
Forum Guru
Posts: 9119
Joined: Mon Apr 20, 2009 9:11 pm

Re: Script code syntax check [Check Selected]

Sat Mar 21, 2020 6:31 pm

Kingdom for standard "syntax error at line X, column Y"! :)
 
msatter
Forum Guru
Forum Guru
Topic Author
Posts: 2897
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: Script code syntax check [Check Selected]

Sat Mar 21, 2020 9:01 pm

An Empire. However just putting it just flat on the road is already a BIG step.
 
msatter
Forum Guru
Forum Guru
Topic Author
Posts: 2897
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: Script code syntax check [Check Selected]

Tue Mar 31, 2020 9:19 pm

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
But is a great help to know this now.
 
msatter
Forum Guru
Forum Guru
Topic Author
Posts: 2897
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: Script code syntax check [Check Selected]

Tue Apr 07, 2020 8:40 pm

Went back in time and found this:
/system script print without-paging where name="scriptname"


You will get a full page and where the colour-full letters end there is the syntax incorrect.

If this can be also reachable as described above one can easily check a part of a code.

Second option:
You can also use
/system script edit number=xx source
for scripts and for schedulers it is:
/system scheduler edit number=xx on-event
In that you can do more but it looks just horrible because of the blank on blank and more blank lines.
Last edited by msatter on Fri Nov 06, 2020 10:58 am, edited 4 times in total.
 
msatter
Forum Guru
Forum Guru
Topic Author
Posts: 2897
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: Script code syntax check [Check Selected]

Fri Apr 10, 2020 12:04 pm

Kingdom for standard "syntax error at line X, column Y"! :)
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;
lset.JPG
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.
You do not have the required permissions to view the files attached to this post.
 
Manic
just joined
Posts: 1
Joined: Fri Mar 04, 2011 5:21 pm

Re: Script code syntax check [Check Selected]

Thu May 13, 2021 5:18 pm

/system script print without-paging where name="scriptname"

@msatter, sir, you are a God! Thanks!

Who is online

Users browsing this forum: Google [Bot] and 25 guests