Community discussions

MikroTik App
 
marrold
Member
Member
Topic Author
Posts: 427
Joined: Wed Sep 04, 2013 10:45 am

Unique Global Variables

Thu Jul 31, 2014 11:00 pm

Hi all,

I've been scripting now for coming up to a year and I am now managing several Mikrotik devices. To simplify management, I am re-writing scripts and improving them, with a view that only a handful of variables need updating at the top of the script for each location, and the rest can be left as is.

There are a few scenarios where I have to persistently store information in a Global Variable, like an interface IP address. The problem comes when I have several copies of the same script running simultaneously (E.G for several interfaces). The Global Variables then clash if they are not individually named manually.

Is there anyway I can have unique global variables per script?

Any ideas are appreciated.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Unique Global Variables

Thu Jul 31, 2014 11:09 pm

AFAIK, no... Other than, obviously, that you prefix the name with the value of another variable (e.g. the interface name) upon declaration.

To make this easier on yourself, you should pack your scripts into a function variable (you know... the "do" argument on ":global"...), and then use that function. The function itself will "install" each instance, i.e. declare the per-script global variables, their initial values, and perhaps add itself to scheduler if needed to keep them updated.


BTW, if you end up making some "general purpose" scripts (i.e. ones that let a user define their own settings, rather than use your hardcoded ones), abstracted away as functions, I'm sure many people (myself included) would like to check them out, so if you could share, that would be awesome.
 
marrold
Member
Member
Topic Author
Posts: 427
Joined: Wed Sep 04, 2013 10:45 am

Re: Unique Global Variables

Thu Jul 31, 2014 11:36 pm

AFAIK, no... Other than, obviously, that you prefix the name with the value of another variable (e.g. the interface name) upon declaration.
Thats the method I am using at the moment.
To make this easier on yourself, you should pack your scripts into a function variable (you know... the "do" argument on ":global"...), and then use that function. The function itself will "install" each instance, i.e. declare the per-script global variables, their initial values, and perhaps add itself to scheduler if needed to keep them updated.
I dont quite understand sorry. Do you mean a script to 'install' the script?

Thanks for your input
 
psamsig
Member Candidate
Member Candidate
Posts: 161
Joined: Sun Dec 06, 2009 1:36 pm
Location: Denmark

Re: Unique Global Variables

Fri Aug 01, 2014 12:10 am

If the variables is only used within a script, they should be declared local and not global.
 
marrold
Member
Member
Topic Author
Posts: 427
Joined: Wed Sep 04, 2013 10:45 am

Re: Unique Global Variables

Fri Aug 01, 2014 12:18 am

If the variables is only used within a script, they should be declared local and not global.
The variables need to be stored persistently, I.E if the script is run again, the variables are still there. Local variables only apply in that instance the script is ran.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Unique Global Variables

Fri Aug 01, 2014 12:22 am

To make this easier on yourself, you should pack your scripts into a function variable (you know... the "do" argument on ":global"...), and then use that function. The function itself will "install" each instance, i.e. declare the per-script global variables, their initial values, and perhaps add itself to scheduler if needed to keep them updated.
I dont quite understand sorry. Do you mean a script to 'install' the script?
Yes.

The idea is that you won't have to "manually" enter new global variables for each copy of the script, but you'll instead run the script with parameters. The parameters will determine the variables to be added, and add a copy of the script that would use those variables.

So for example,
$installMonitor interface=local
Would read the "interface" argument, and create a global variable with the name "monitor_local_ip" for the IP address of the interface "local", and so on. If you want the same script for another interface, you run your install script with the name of the new interface, so
$installMonitor interface=wan1
to now create the "monitor_wan1_ip" variable, etc.
 
User avatar
dissident76
just joined
Posts: 10
Joined: Fri Apr 10, 2015 7:33 am

Re: Unique Global Variables

Fri Apr 10, 2015 1:34 pm

To make this easier on yourself, you should pack your scripts into a function variable (you know... the "do" argument on ":global"...), and then use that function. The function itself will "install" each instance, i.e. declare the per-script global variables, their initial values, and perhaps add itself to scheduler if needed to keep them updated.
I dont quite understand sorry. Do you mean a script to 'install' the script?
Yes.

The idea is that you won't have to "manually" enter new global variables for each copy of the script, but you'll instead run the script with parameters. The parameters will determine the variables to be added, and add a copy of the script that would use those variables.

So for example,
$installMonitor interface=local
Would read the "interface" argument, and create a global variable with the name "monitor_local_ip" for the IP address of the interface "local", and so on. If you want the same script for another interface, you run your install script with the name of the new interface, so
$installMonitor interface=wan1
to now create the "monitor_wan1_ip" variable, etc.
Hi robot,

That is exactly the idea I was looking for, but what's the syntax to use the value of a parameter (or variable) as variable name?
I have tried this and it does not work:
:global name wan1;
:global $name
If this worked, I would have declared a variable named "wan1", which is the value of $name (or any parameter, like $1, etc)

Thanks a lot
 
noib
Member Candidate
Member Candidate
Posts: 291
Joined: Fri Jan 25, 2013 6:04 pm
Location: France
Contact:

Re: Unique Global Variables

Fri Apr 10, 2015 2:17 pm

Dirty way to have system-wide, persistent variable even after reboot: use something like a comment on an interface.
/interface ethernet set ether1 comment=$myData
:local myData [/interface ethernet get ether1 comment ];
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Unique Global Variables

Fri Apr 10, 2015 3:19 pm

Drat... I thought that the syntax would be
:global name="$interface" "value";
(with the "name=" and quotes being a way to disambiguate between that and a new global variable called "interface")
But that doesn't work... The only way seems to be to call :execute with appropriate arguments, which BTW means the installer can be easily broken when a value is supplied that would spill into the rest of the script (probably causing a had to detect error).

So f.e.
:execute script=":global \"interface_$interface\" \"val\""
would create a global variable with a name equal to "interface_", followed by the interface name, and with a value equal to "val". If $interface had a value of "local", the variable can then be referenced with $"interface_local".
 
User avatar
dissident76
just joined
Posts: 10
Joined: Fri Apr 10, 2015 7:33 am

Re: Unique Global Variables

Fri Apr 10, 2015 6:59 pm

Drat... I thought that the syntax would be
:global name="$interface" "value";
(with the "name=" and quotes being a way to disambiguate between that and a new global variable called "interface")
But that doesn't work... The only way seems to be to call :execute with appropriate arguments, which BTW means the installer can be easily broken when a value is supplied that would spill into the rest of the script (probably causing a had to detect error).

So f.e.
:execute script=":global \"interface_$interface\" \"val\""
would create a global variable with a name equal to "interface_", followed by the interface name, and with a value equal to "val". If $interface had a value of "local", the variable can then be referenced with $"interface_local".
Yeah, that worked, thanks a lot :)