Community discussions

MikroTik App
 
jeroenp
Member Candidate
Member Candidate
Topic Author
Posts: 159
Joined: Mon Mar 17, 2014 11:30 am
Location: Amsterdam
Contact:

[SOLVED] What parts of scripts can be ran from the console?

Mon May 02, 2016 8:49 pm

As a n00b on Microtik, but not on programming, I'm trying to wrap my head around the scripting.

My first questions are centred around what parts of scripts can and cannot be ran on the console and why.

For instance when you run the script at http://wiki.mikrotik.com/wiki/Script_to ... f_the_week from the console, it barfs at the `:set` variable assignments.

Why is that?
How can I work around it?
Shouldn't this script be a function that returns the day of the week so it can be used in a more general fashion?

--jeroen
Last edited by jeroenp on Thu May 05, 2016 12:00 pm, edited 1 time in total.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: What parts of scripts can be ran from the console?

Mon May 02, 2016 10:03 pm

The console doesn't maintain its own variables. As soon as you call :local, the next line doesn't have it. That's why "set" fails - there's no variable to be changed. The previous lines that reference the variable don't fail - they resolve the variable to a value of type "nothing".

You can workaround this by wrapping the whole script with "{" and "}". Alternatively, have it in "/system script", and then run it with "/system script run".
 
jeroenp
Member Candidate
Member Candidate
Topic Author
Posts: 159
Joined: Mon Mar 17, 2014 11:30 am
Location: Amsterdam
Contact:

Re: What parts of scripts can be ran from the console?

Tue May 03, 2016 8:42 am

The console doesn't maintain its own variables. As soon as you call :local, the next line doesn't have it. That's why "set" fails - there's no variable to be changed. The previous lines that reference the variable don't fail - they resolve the variable to a value of type "nothing".

You can workaround this by wrapping the whole script with "{" and "}". Alternatively, have it in "/system script", and then run it with "/system script run".
Thanks for the explanation.

So basically it comes down to:

- :local variables are local in scope
- the console only has only global scope, but no local scope
- there is a block statement `{}` in which there is local scope

That raises more questions:

- when nesting multiple block statements, are locals defined in the outer block valid for read/write in the inner block?
- when nesting multiple block statements, are locals defined in the inner block valid for read/write in the outer block?
- is a pair of `{}` the only way to start a block statement?
- is there some kind of "Language Reference" or, even better, EBNF or BNF specification of the language?

--jeroen
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: What parts of scripts can be ran from the console?

Tue May 03, 2016 3:15 pm

- yes
- no
- AFAIK, yes, though you could also write multiple commands on the same line, separated with ";". On the console, everything on one line is part of the same local block, so
:local var "val";:put $var
will output "val", as expected.
- As a reference http://wiki.mikrotik.com/wiki/Manual:Scripting . Doesn't quite have an EBNF, but it does have a general command syntax near the top that almost all commands follow. Reading the whole page, you can infer the whole language's grammar (if you were to go as far as creating a custom parser or something...).

Though ":set" is actually an outlier. You don't have to use
:set "var" "new value"
to change $var. You can instead have
:set $var "new value"
though that doesn't quite make sense IMHO, and you can set associative array keys with
:set ($var->"key") "new value"
which also doesn't quite make sense with how every other command is used.