Code: Select all
:global $wan1 1;
:put ("wanSta" . $wan1);
if I want to set something
Code: Select all
:set ("wanStatus" . $wanIndex) 10
:global $wan1 1;
:put ("wanSta" . $wan1);
:set ("wanStatus" . $wanIndex) 10
:global $wan1 1;
:put ("wanSta" . $wan1);
:set ("wanStatus" . $wanIndex) 10
:global wan1 1
:put "wanSta$wan1"
:set wanSta1 10
:global variablename "test" # Create a global variable using the name inside "variablename" and set the value (or update the value if is already defined) [:parse ":global $variablename \"REX1\""] # after "parse" the variable exist and can be displayed :put $test # Set a value of variable with name defined inside "variablename" (or simply apply previous "parse" command) [:parse "global $variablename;:set $variablename \"REX2\""] :put $test # For read the value, of a variable with name defined inside "variablename", and put it inside another global or local declared variable: :global testx [[:parse ":global $variablename; :return \$$variablename"]] :put $testx # For read the value, of a variable with name defined inside "variablename", simply for put the value on terminal: :put [[:parse ":global $variablename; :return \$$variablename"]] # Or on alternative way: (but this throw an error if the variable not previously created) :put [/system script environment get $variablename value]
:global wan1 1
:global variablename "wanSta$wan1"
[:parse ":global $variablename \"10\""]
:put [[:parse ":global $variablename; :return \$$variablename"]]
In this example, you output the name of a variable to the value of another variable.# For read the variable inside another declared variable:
:global testx
[:parse "global $variablename;:global testx \$$variablename"]
:put $testx
What?Is it possible to do the opposite? Display the value of a variable in the name of another variable ? And so that it was declared ?
If you don't know the name of the variable anyway, you still can't manipulate it from the script without declaring it. Therefore, all the fuss with "dynamic variables" loses its meaning.Hello,
RouterOS scripting does not support declaring variables which names are taken from another variables value.
Regarding your question, variable is not declared in current scope, that is why you cannot get its value until ;global varname; is added to the script.
Best regards,
Maris B.
:global variablename "test"
:local name
:for i from=1 to 3 do={
:set name ("$variablename"."$i")
[[:parse ":global $name; set $name $i"]]
}
:for i from=1 to 3 do={
:set name ("$variablename"."$i")
[[:parse ":global $name; log error (\$$name+5)"]]
}
:global wan1 1
:global variablename "wanSta$wan1"
[:parse ":global $variablename \"10\""]
# :put [[:parse ":global $variablename; :return \$$variablename"]]
# another way:
:put [/system script environment get $variablename value]
{ :global test ":log info \"ok parse with find\"" :local variablename "test" [:parse [/system script environment get [find where name=$variablename] value]] } { :global test ":log info \"ok execute with find\"" :local variablename "test" [:execute [/system script environment get [find where name=$variablename] value]] } { :global test ":log info \"ok parse directly\"" :local variablename "test" [:parse [/system script environment get $variablename value]] } { :global test ":log info \"ok execute directly\"" :local variablename "test" [:execute [/system script environment get $variablename value]] }
{
:global test ":log info mama"
:local variablename "test"
[:execute [/system script environment get [find where name=$variablename] value]]
}
{
:global test ":put mama"
:local variablename "test"
[:execute [/system script environment get [find where name=$variablename] value]]
}
[:execute [/system script environment get [find where name=$variablename] value]]
:global test do={:log info mama}
:local variablename "test"
[[:parse "[/system script environment get [find where name=$variablename] value]"]]
This is one function, not one variable where inside is present one string that represent one script for be compared later with parse or execute.Code: Select all:global test do={:log info mama} […]
And so what?This line is the last one in the script. There's no one to wait for.