Page 1 of 1

Fail Variable declaration sintax from manual

Posted: Fri Dec 04, 2020 4:34 pm
by Chiara
Hi,

If i copy and paste this code on a terminal of CCR1009 with routeros 6.47.7 it fails, but on the manual it says it it the correct sintax https://wiki.mikrotik.com/wiki/Manual:S ... #Variables:
:local myVar;
:set myVar "my value";
:put $myVar;
log info $myVar
this is the output from the terminal:
admin@Router] > :local myVar;
[admin@Router] > :set myVar "my value";
syntax error (line 1 column 6)
[admin@Router] > :put $myVar;
What is wrong here, why can't I set my value to myvar?

Re: Fail Variable declaration sintax from manual

Posted: Fri Dec 04, 2020 4:39 pm
by msatter
:set $myVar "my value";

Re: Fail Variable declaration sintax from manual

Posted: Fri Dec 04, 2020 5:03 pm
by Chiara
Thank you, you've saved "half" of my day, hope mikrotik will fix it also in the wiki.
Now the var can be set, but next row :put alway is empty.

Re: Fail Variable declaration sintax from manual

Posted: Fri Dec 04, 2020 5:53 pm
by mrz
local variable lives only inside scope, and each command line is in its own scope.
Either put whole script in curly braces or use global variable

and :set $var value does not solve the actual problem either.

Re: Fail Variable declaration sintax from manual

Posted: Fri Dec 04, 2020 6:02 pm
by Chiara
Thank you, this way all seems to work:
 [ :set $myVar "my value"; log info $myVar ]

Re: Fail Variable declaration sintax from manual

Posted: Sat Dec 19, 2020 12:49 am
by eworm
You should use curly brackets, not square brackets. So correct code:
{ :local myVar; :set myVar "my value"; :log info $myVar; }

Re: Fail Variable declaration sintax from manual

Posted: Sun Dec 20, 2020 5:14 pm
by Jotne
Or
{ 
  :local myVar
  :set $myVar "Some data"
  :put $myVar
  :log info $myVar
}
Both :set myVar "my value" and :set $myVar "my value" does work fine.