Community discussions

MikroTik App
 
User avatar
favincen
just joined
Topic Author
Posts: 21
Joined: Mon Jun 08, 2015 1:56 pm
Location: Grenoble, France

[Solved] Use of externally defined global variables inside import scripts,

Fri Aug 28, 2015 4:38 pm

Hi all,

When I import a rsc file, the code executed can't access global variables defined in the rOS main /env .
And the script can neither access variables defined in a subscript (loaded by a /import within the import script).
Here's my test code:
[admin@MikroTik] > /file print detail where name~"test"                             
 0 name="test.rsc" type="script" size=504 creation-time=jan/03/2002 00:12:20 
   contents=
     #Cmd to run before importing this rsc file: :global GlobalVar "VALUE_of_GlobalVar"
     :put "GlobalVar value is: $GlobalVar"
     
     :put "Importing test-inc.rsc..."
     /import test-inc.rsc\t
     :put "SubScriptGlobalVar value is: $SubScriptGlobalVar"
     
     :global ImportedScriptGlobalVar "VALUE_of_ImportedScriptGlobalVar"
     :put "Just defined var ImportedScriptGlobalVar with value: $ImportedScriptGlobalVar"
     #Cmd to run after importing this rsc file: :put "ImportedScriptGlobalVar value is: $ImportedScriptGlobalVar"

 1 name="test-inc.rsc" type="script" size=135 creation-time=jan/03/2002 00:12:20 
   contents=
     :global SubScriptGlobalVar "VALUE_of_SubScriptGlobalVar"
     :put "Just defined var SubScriptGlobalVar with value: $SubScriptGlobalVar"
Running the test gives:
[admin@MikroTik] > /import test.rsc 
GlobalVar value is: 
Importing test-inc.rsc...
Just defined var SubScriptGlobalVar with value: VALUE_of_SubScriptGlobalVar

Script file loaded and executed successfully
SubScriptGlobalVar value is: 
Just defined var ImportedScriptGlobalVar with value: VALUE_of_ImportedScriptGlobalVar

Script file loaded and executed successfully
 
See that test.rsc can't get the value of $GlobalVar (which is defined global at a upper level) nor the one of $SubScriptGlobalVar (which is defined global at a lower level).

Test done on rOS 6.29 and 6.30.4, on a RB951G-2HnD.

BTW, as shown below, it appears that $SubScriptGlobalVar, which is defined in the subscript test-inc.rsc and not readable within test.rsc, is well propagated to the upper level at the prompt.
[admin@MikroTik] > :put "ImportedScriptGlobalVar value is: $ImportedScriptGlobalVar"                                    
ImportedScriptGlobalVar value is: VALUE_of_ImportedScriptGlobalVar

[admin@MikroTik] > :put "SubScriptGlobalVar value is: $SubScriptGlobalVar"
SubScriptGlobalVar value is: VALUE_of_SubScriptGlobalVar
So, $SubScriptGlobalVar, defined global in test-inc.rsc, imported from test.rsc, imported from CLI, is readable from test-inc.rsc and from the CLI, but not by script that propagated from one to the other !
What a strange behavior !!! :oops:

Is this a bug, a known (undocumented ?) limitation, or am I missing something :roll: ???

In http://wiki.mikrotik.com/wiki/Manual:Sc ... #Variables , I read: "global - accessible from all scripts created by current user, defined by global keyword"

Thanks for your attention and feedbacks.
Fabrice
Last edited by favincen on Tue Sep 08, 2015 1:00 pm, edited 1 time in total.
 
User avatar
favincen
just joined
Topic Author
Posts: 21
Joined: Mon Jun 08, 2015 1:56 pm
Location: Grenoble, France

Re: Use of externally defined global variables inside import scripts,

Mon Aug 31, 2015 2:29 pm

Hi again,

I encountered a similar issue when using a function within a import script !

Here's a test code:
:global GlobalVar "VALUE_of_GlobalVar"
:put "GlobalVar value is: $GlobalVar"
:global myFunc do={:put "INSIDE a function, GlobalVar value is: $GlobalVar"}
$myFunc
:set GlobalVar "NEW_VALUE_of_GlobalVar"  
:put "NEW GlobalVar value is: $GlobalVar" 
$myFunc
Running it directly on the CLI, it works as expected:
[admin@MikroTik] > :global GlobalVar "VALUE_of_GlobalVar"
[admin@MikroTik] > :put "GlobalVar value is: $GlobalVar"
GlobalVar value is: VALUE_of_GlobalVar
[admin@MikroTik] > :global myFunc do={:put "INSIDE a function, GlobalVar value is: $GlobalVar"}
[admin@MikroTik] > $myFunc
INSIDE a function, GlobalVar value is: VALUE_of_GlobalVar
[admin@MikroTik] > :set GlobalVar "NEW_VALUE_of_GlobalVar"  
[admin@MikroTik] > :put "NEW GlobalVar value is: $GlobalVar" 
NEW GlobalVar value is: NEW_VALUE_of_GlobalVar
[admin@MikroTik] > $myFunc
INSIDE a function, GlobalVar value is: NEW_VALUE_of_GlobalVar
But putting the very same code in an import script, and running it, I get:
[admin@MikroTik] > /import test-env_in_func.rsc                                                
GlobalVar value is: VALUE_of_GlobalVar
INSIDE a function, GlobalVar value is: 
NEW GlobalVar value is: NEW_VALUE_of_GlobalVar
INSIDE a function, GlobalVar value is: 

Script file loaded and executed successfully
I'm puzzled to observe that myFunc is not able to get the variable value anymore ! :(

I'd love to hear I do something wrong, with explanation on how to do it right...

My context: I'm currently writing a script to initialize rOS boxes the way I need, and these strange behaviors get in the way !
I need to rewrite my script to have everything in one file (while I was planning to split config and code in separate files) and cannot use functions to make an easily readable and refactored code... :cry:
 
User avatar
favincen
just joined
Topic Author
Posts: 21
Joined: Mon Jun 08, 2015 1:56 pm
Location: Grenoble, France

Re: Use of externally defined global variables inside import scripts,

Mon Aug 31, 2015 4:55 pm

I'm puzzled to observe that myFunc is not able to get the variable value anymore ! :(

I'd love to hear I do something wrong, with explanation on how to do it right...
I answer to myself with a workaround: I need to pass variables as arguments of my functions. Not that handy, but it works.

Still, why is it working as expected (or as I would expect) when ran from the CLI and not when the same code is executed from a .rsc script ???
 
User avatar
favincen
just joined
Topic Author
Posts: 21
Joined: Mon Jun 08, 2015 1:56 pm
Location: Grenoble, France

Re: Use of externally defined global variables inside import scripts,

Wed Sep 02, 2015 1:22 pm

Anybody to confirm they get the same behavior (or not) ?
Anybody to explain if I'm doing something wrong, or another way to do it that works ?
 
User avatar
favincen
just joined
Topic Author
Posts: 21
Joined: Mon Jun 08, 2015 1:56 pm
Location: Grenoble, France

[Solved] Use of externally defined global variables inside import scripts,

Fri Sep 04, 2015 4:22 pm

Hi all (despite I feel quite alone on this topic... :-o )

I found the good answer to my issue ! :mrgreen: :lol:
To make it short: to read the value of a global variable from within a .rsc script or a function, just declare it again in that local context, using ":global MyExistingGlobalVariable".
I discovered that in the manual, looking at the Nested function example.
 
wcsnet
Frequent Visitor
Frequent Visitor
Posts: 65
Joined: Mon Apr 29, 2013 12:43 pm
Location: South Africa

Re: [Solved] Use of externally defined global variables inside import scripts,

Wed Mar 02, 2016 10:17 am

Had just about the same issue, i have global variable for thing used across a number of scripts, for example email addresses .

So i have a file "post configuration load.rsc" to basically send just a email but this did not work

with a exec "post configuration load.rsc" or import "post configuration load.rsc"

This works
:do {[:global emailnotificationsto;:global emailnotificationscc;/tool e-mail send to=$emailnotificationsto cc=$emailnotificationscc subject="$[/system identity get name] loaded configeration" body=""]} on-error={}


This doesn't
:do {[/tool e-mail send to=$emailnotificationsto cc=$emailnotificationscc subject="$[/system identity get name] loaded configeration" body=""]} on-error={}

So seems like global vars has to be declared similar to scripts that uses global vars :-)

Who is online

Users browsing this forum: No registered users and 18 guests