Page 1 of 1

How do you clear a global variable?

Posted: Tue Dec 09, 2008 11:39 pm
by MyThoughts
I'm stuck updating some old v2.9.x scripts to work with v3.17 but I can't find a way to clear a defined global variable.

Can anyone post the command(s) necessary to clear an already defined global variable?

I've reviewed the only available documentation (http://wiki.mikrotik.com/wiki/Scripting) but it doesn't even mention clearing global variables.

Cheers

Re: How do you clear a global variable?

Posted: Wed Dec 10, 2008 8:35 am
by mrz
By clearing you man delete it? It is not possible, only thing you can do is to set it to nil.

Re: How do you clear a global variable?

Posted: Wed Dec 10, 2008 10:07 am
by SurferTim
Greetings!
The variables are in
/system script environment
Do a print and then use
remove X
where X is the line number of the variable you want to delete.

Re: How do you clear a global variable?

Posted: Thu Dec 11, 2008 9:11 pm
by MyThoughts
Thanks for the replies the '/system script enviroment' method works great. I was really getting tired of unneeded global variables.

Cheers

Re: How do you clear a global variable?

Posted: Sat Oct 26, 2013 5:47 pm
by Ehman
Thanks for the replies the '/system script enviroment' method works great. I was really getting tired of unneeded global variables.

Cheers
Still impossible ?

I use the NO-IP mikrotik scrip, and every now and then, I run this command "/system script environment remove [find user="admin"]" to remove everything, so that I can do a forced IP update

but of course now after a upgrade , the old scripts doesn't work!!! :(

Re: How do you clear a global variable?

Posted: Sat Oct 26, 2013 8:24 pm
by SurferTim
I use the NO-IP mikrotik scrip, and every now and then, I run this command "/system script environment remove [find user="admin"]" to remove everything, so that I can do a forced IP update

but of course now after a upgrade , the old scripts doesn't work!!!

There is no "user" field in "/system script environment". That is probably why it is not running.

Re: How do you clear a global variable?

Posted: Sun Oct 27, 2013 3:13 am
by Ehman
I use the NO-IP mikrotik scrip, and every now and then, I run this command "/system script environment remove [find user="admin"]" to remove everything, so that I can do a forced IP update

but of course now after a upgrade , the old scripts doesn't work!!!

There is no "user" field in "/system script environment". That is probably why it is not running.


exactly... older versions of ros always said user=admin ... new version...nope

I've changed all the :global to :local ..no more issue's

Re: How do you clear a global variable?

Posted: Mon Oct 28, 2013 9:37 am
by mrz
Starting with v6.2 you can unset global variables:
[admin@pe0] /system> :global aa 1
[admin@pe0] /system> :environment print 
aa=1

[admin@pe0] /system> :set aa 
[admin@pe0] /system> :environment print 

[admin@pe0] /system> 

Re: How do you clear a global variable?

Posted: Wed May 18, 2016 11:06 am
by EtienneO
Script to clear all variables

:if ([:len [/system script environment find]] != 0) do={
:for E from=0 to=([:len [/system script environment find]] - 1) do={
/system script environment remove $E
}}

Re: How do you clear a global variable?

Posted: Wed May 18, 2016 12:10 pm
by Deantwo
Script to clear all variables

:if ([:len [/system script environment find]] != 0) do={
:for E from=0 to=([:len [/system script environment find]] - 1) do={
/system script environment remove $E
}}
Could just use:
/system script environment remove [find]
The command doesn't complain if nothing is found.
And you can easily make it only delete a variable with a specific name if need be.
/system script environment remove [find name="ThatVar"]
When using WinBox you can also go to "System -> Scripts -> Environment tab" where you have a full list of all global variables and can delete any you you want. You can even change the value of a global variable in this menu.

Remember that you need FULL ADMIN permissions to do either of these.

Re: How do you clear a global variable?

Posted: Wed May 18, 2016 4:33 pm
by EtienneO
Script to clear all variables

:if ([:len [/system script environment find]] != 0) do={
:for E from=0 to=([:len [/system script environment find]] - 1) do={
/system script environment remove $E
}}
Could just use:
/system script environment remove [find]
The command doesn't complain if nothing is found.
And you can easily make it only delete a variable with a specific name if need be.
/system script environment remove [find name="ThatVar"]
When using WinBox you can also go to "System -> Scripts -> Environment tab" where you have a full list of all global variables and can delete any you you want. You can even change the value of a global variable in this menu.

Remember that you need FULL ADMIN permissions to do either of these.
Thanks, I was not aware that you can simply add a [find] at the end of the command to remove.

Re: How do you clear a global variable?

Posted: Wed May 18, 2016 4:56 pm
by Deantwo
Thanks, I was not aware that you can simply add a [find] at the end of the command to remove.
The "[find]" simply selects all objects that matches the search criteria and added them to an array.
If there is more than one object the "remove" command is simply repeated for each object in the array. Same way as if you had done a ":foreach" loop.