Community discussions

MikroTik App
 
Chiara
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 72
Joined: Thu Jul 23, 2015 3:47 pm

Fail Variable declaration sintax from manual

Fri Dec 04, 2020 4:34 pm

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?
 
msatter
Forum Guru
Forum Guru
Posts: 2897
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: Fail Variable declaration sintax from manual

Fri Dec 04, 2020 4:39 pm

:set $myVar "my value";
 
Chiara
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 72
Joined: Thu Jul 23, 2015 3:47 pm

Re: Fail Variable declaration sintax from manual

Fri Dec 04, 2020 5:03 pm

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.
Last edited by Chiara on Fri Dec 04, 2020 5:59 pm, edited 1 time in total.
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7041
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Fail Variable declaration sintax from manual

Fri Dec 04, 2020 5:53 pm

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.
 
Chiara
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 72
Joined: Thu Jul 23, 2015 3:47 pm

Re: Fail Variable declaration sintax from manual

Fri Dec 04, 2020 6:02 pm

Thank you, this way all seems to work:
 [ :set $myVar "my value"; log info $myVar ]
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1070
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: Fail Variable declaration sintax from manual

Sat Dec 19, 2020 12:49 am

You should use curly brackets, not square brackets. So correct code:
{ :local myVar; :set myVar "my value"; :log info $myVar; }
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Fail Variable declaration sintax from manual

Sun Dec 20, 2020 5:14 pm

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.

Who is online

Users browsing this forum: No registered users and 19 guests