{
:put "storing global variables"
:global text "my text"
:global number 2345
:put "Test of text: $text = $([:typeof $text])"
:put "Test of number: $number = $([:typeof $number])"
}
storing global variables
Test of text: my text = str
Test of number: 2345 = num
Second script, using data
{
:put "using global variables"
:global text
:global number
:put "Test of text: $text = $([:typeof $text])"
:put "Test of number: $number = $([:typeof $number])"
}
using global variables
Test of text: my text = str
Test of number: 2345 = num
Nothing changes, string is str and number is num.
Edit test your script.
admin@941-2nD] > :global X1 123
[admin@941-2nD] >
[admin@941-2nD] > {
{... :local i 1; # set local variable i=1
{... :execute ":global X$i"; # declare global variable X1, since i=1
{... :put [:typeof $X1]; # Test what type of data X1
{... }
num
[admin@941-2nD] >
[admin@941-2nD] > :global X1 "test"
[admin@941-2nD] >
[admin@941-2nD] > {
{... :local i 1; # set local variable i=1
{... :execute ":global X$i"; # declare global variable X1, since i=1
{... :put [:typeof $X1]; # Test what type of data X1
{... }
str
Declaring global variable works as it suppose to do.
But i fails when you try to declare it indirectly using a variable.
It may be an other way to do it, will test some.
But this seems to be as support case for MT. Send them an email at support@mikrotik.com
Don’t think that is the problem, I tried as admin with every policy selected, and same result. Unless the terminal runs the code with different permissions; that, I don’t know.
First, as others already described - you need to declare the variable again (global) in order for your script to “read” it.
If you have a nested function that also uses that variable, you have to re-declare it again inside the nested function as well. (Even if it’s declared outside and above it)
The problem begins with the usage of the “execute” command. Execute runs the supplied command in a sub-shell - so you get no interaction with it whatsoever.
Allow me to take it step by step as to what happens here:
:local i 1; > <— local variable “i” is declared with a value of 1, being local means it can only be read by the region it’s been put into (script, curly braces, etc). :execute “:global TrafWAN$i”; > <— $i is resolved into value 1, so the string that’s getting executed is “:global TrafWAN1”. But this command was ran in a sub-shell, once it completes - POOF! Everything’s gone! ..think of it more like calling another script, from your script, to do stuff for you. You cannot interact with it, it only executes whatever is enclosed in its quotes. :global type [:typeof $TrafWAN1]; > <— the command “typeof” attempts to fetch the type of $TrafWAN1 which at this point hasn’t been declared in the script and is therefore “unknown”.
The reason this works on the terminal and not on the script, is that on the terminal you can “read” the global variables without having to declare them prior.
Plus, on the terminal version of your code, you can even skip the “execute” line altogether because it offers virtually nothing to you. (it just executes a global declaration and exits)