I Mikrotik Router OS scripting.
Is there any reason to use the Colon : in front of commands
Eks This works
:do {:put "hello"}
And so do this:
do {put "hello"}
Is there any reason to use one or another.
I Mikrotik Router OS scripting.
Is there any reason to use the Colon : in front of commands
Eks This works
:do {:put "hello"}
And so do this:
do {put "hello"}
Is there any reason to use one or another.
From the WiKi:
Every global command should start with “:” token, otherwise it will be treated as variable.
How is that working in the new API (ROS-7) and maybe we are in a transfer to that and the Colon is not handled that strict anymore. I also noted that having to usde define :global to have access to the global variables is not needed in just scripts. Nested functions I have not yet tested.
In API I do not know. but on console for v7.0 b5 both with and without colon do works.
Some strange that RouterOS then allow you to skip the Colon, when manual clearly stats that you need it?
Yes, I find it useful as it separates the command from its output, for example:
:foreach i in=[/ip arp print as-value] do={ :put $i }
.id=*1;address=192.168.20.1;comment=;interface=bridge;mac-address=20:1A:06:5F:C0:3D
.id=*2;address=192.168.127.254;comment=;interface=bridge;mac-address=24:A4:3C:06:6C:2D
.id=*3;address=192.168.90.1;comment=;interface=bridge;mac-address=00:40:F4:A6:CE:E2
>
I found some interesting behavior that could explain the use of the “:”.
{
:delay 4s
/interface
:if (([pppoe-client monitor pppoe-ikev2 as-value once]->"mtu") < 1500) do={
disable sfp-sfpplus1
:delay 50ms
enable sfp-sfpplus1
:log warning "PPPoE MTU lower than 1500, so the SFP port is restarted"}
}
On the third line I use /interface to switch to a different ‘path’ and then I don’t have to use a “:” for commands in that ‘path’. As soon as I cal a global command then I have to use a “:”.
I have to test also with previous scripts to see if something has been altered in 6.47RC2.
Then I tried the the command out of the OT.
/interface> do {put "hello"}
bad command name put (line 1 column 5
>do {put "hello"}
hello
Case closed? It working as stated in the Wiki.
I have seen this behaviour as well. If you are doing lots commands in a sub folder you can skip the path if you specify the path first.
So in global commands use : to differentiate what to do.
Case closed ![]()
No, it is not closed! ![]()
/interface> /do {put "hello"} ;
hello
If it’s unambiguous, then technically don’t need the colon. Problem is that’s not always easy to know. Especially with stuff like :set for variable assignment… if at the “top-level”, either set or :set work… but in a “command path”, then “set” could be set a parameter – so then you need colon in “:set” to set variable. Similar with “:find” vs “find”.
That being said, I’m a believer in always using the : for any of the top-level things… to mean it’s clearer to read and may avoid problems down road since Mikrotik’s examples mostly use the colon.
Not a colon anywhere!
/interface list member> /local x 5 ; /put $x ; /set x 7 ; /put $x ;
5
7
Your cheating! / should work same - it isthe plain “set” that be problematic in above ![]()
If you’re still using an “extra char” to disambiguate, the / vs : colon seems in the personal preference category. The colon : make the operations stand out vs cmds - but that just me.
Clear!
The only thing that bothers me is that the syntax is not clear. ![]()
“/ vs :”
I’m a rule-following guy, the [limited] docs use : — But RouterOS has the “/console/inspect request=syntax” to look these things up. It identifies both / and : same the same:
## using slash /
[amm0@MT] > /console/inspect request=syntax input="/put "
Columns: TYPE, SYMBOL, SYMBOL-TYPE, NESTED, NONORM, TEXT
TYPE SYMBOL SYMBOL-TYPE N NON TEXT
syntax collection 0 yes
syntax <message> explanation 1 no string or any expression
# using : colon
[amm0@MT] > /console/inspect request=syntax input=":put " path=ip,address
Columns: TYPE, SYMBOL, SYMBOL-TYPE, NESTED, NONORM, TEXT
TYPE SYMBOL SYMBOL-TYPE N NON TEXT
syntax collection 0 yes
syntax <message> explanation 1 no string or any expression
# using / slash with a "path" of (/ip/address == ip,address ... in the /console/inspect)
[amm0@MT] > /console/inspect request=syntax input="/put " path=ip,address
Columns: TYPE, SYMBOL, SYMBOL-TYPE, NESTED, NONORM, TEXT
TYPE SYMBOL SYMBOL-TYPE N NON TEXT
syntax collection 0 yes
syntax <message> explanation 1 no string or any expression
# BUT no / or : — a plain "put" is not valid if there is a path
/console/inspect request=syntax input="put " path=ip
Columns: TYPE, SYMBOL, SYMBOL-TYPE, NESTED, NONORM
TYPE SYMBOL SYMBOL-TYPE NESTED NONORM
syntax collection 0 yes
syntax .. explanation 1 no
syntax address explanation 1 no
syntax arp explanation 1 no
syntax cloud explanation 1 no
syntax dhcp-client explanation 1 no
[...]
The problem is not the syntax, but how to make the code as clear as possible, even for others.
For example, the code in one of the previous posts:
{
:delay 4s
/interface
:if (([pppoe-client monitor pppoe-ikev2 as-value once]->“mtu”) < 1500) do={
disable sfp-sfpplus1
:delay 50ms
enable sfp-sfpplus1
:log warning “PPPoE MTU lower than 1500, so the SFP port is restarted”}
}
(fixed also “mtu”, probably what is really wanted some years ago is “actual-mtu”) ignoring what it’s for, I would have written it like this:
{
:delay 4s
:local pname “pppoe-ikev2”
:local iname “sfp-sfpplus1”
/interface
:local chkmtu ([pppoe-client monitor $pname once as-value]->“actual-mtu”)
:if ($chkmtu < 1500) do={
disable $iname ; :delay 50ms ; enable $iname
:log warning “$pname Actual MTU $chkmtu < 1500, so $iname is reloaded”
}
}
And is better “command” delay (:delay) than “subsection” delay (/delay), ignoring that it does the same thing, it’s much clearer wit the colon.
Another ridiculous thing is also to put ; in the end for no reason, or using do { } makes no sense at all, or doing complicated things unnecessarily, like:
:put ("the value of " . $varname . " is " . $varvalue . “.”)
instead of write directly
:put “the value of $varname is $varvalue.”
(notice: () are needed only if the stting containing one special value like \ $ _ \66 \r \n etc.)