Community discussions

MikroTik App
 
yosu
just joined
Topic Author
Posts: 5
Joined: Fri Oct 31, 2014 8:44 pm

Global variable not retained in script

Thu Feb 25, 2021 10:39 am

hi,

I am new to mikrotik scripting, and trying to create a global variable that will persists throughout the mikrotik. I know global-variables are user-based, so I only use Admin in this case. Here is my script :
/log info message="gIsReconnecting before: $gIsReconnecting"
:if ( $gIsReconnecting = nil ) do= { 
  /log info message="gIsReconnecting is empty"
  :global gIsReconnecting "yellow" 
  :set gIsReconnecting "RED"
}
/log info message="gIsReconnecting after:  $gIsReconnecting"
.

Output (after running a few times) :
gIsReconnecting before:  
gIsReconnecting is empty
gIsReconnecting after:  
gIsReconnecting before:  
gIsReconnecting is empty
gIsReconnecting after:    

Problem is the global variable is never set. Am I doing something wrong ?
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Global variable not retained in script  [SOLVED]

Thu Feb 25, 2021 11:48 am

You need to declare global variable at the top of the script, or else it does not work.

I did just post the same answer yesterday:
viewtopic.php?f=9&t=172891

:global gIsReconnecting
/log info message="gIsReconnecting before: $gIsReconnecting"
:if ( $gIsReconnecting = nil ) do= { 
  /log info message="gIsReconnecting is empty"
  :global gIsReconnecting "yellow" 
  :set gIsReconnecting "RED"
}
/log info message="gIsReconnecting after:  $gIsReconnecting"
 
yosu
just joined
Topic Author
Posts: 5
Joined: Fri Oct 31, 2014 8:44 pm

Re: Global variable not retained in script

Thu Feb 25, 2021 12:50 pm

Thanks! I read https://wiki.mikrotik.com/wiki/Manual:Scripting and it doesn't say global variables needs to be right on top. The closest is "Every variable, except for built in RouterOS variables, must be declared before usage by local or global keywords. .."

On a note to help other users, global variables must always be declared on top the script. Here's an example of how i am using a global variable throughout the device :

Script called when mikrotik device startup (i use System->Scheduler to run a script @ startup)
#in System->Scheduler->On Event :
/system script run setGlobal
.

Then, the script called 'setGlobal' is as such :
#in System->Scripts->new script name 'setGlobal'
:global gIsReconnecting "Y"
/log info message="gIsReconnecting: $gIsReconnecting "
.
Subsequent scripts called must have all variables declared on top as such :
:global gIsReconnecting
#code
#code...
.
.

You need to declare global variable at the top of the script, or else it does not work.

I did just post the same answer yesterday:
viewtopic.php?f=9&t=172891

:global gIsReconnecting
/log info message="gIsReconnecting before: $gIsReconnecting"
:if ( $gIsReconnecting = nil ) do= { 
  /log info message="gIsReconnecting is empty"
  :global gIsReconnecting "yellow" 
  :set gIsReconnecting "RED"
}
/log info message="gIsReconnecting after:  $gIsReconnecting"

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 18 guests