I have a variable
Code: Select all
:global a 5;
I used
Code: Select all
:global a ($a + 2);
How can I do?
:global a 5;
:global a ($a + 2);
[admin@MikroTik] > :local a 5; :set a ($a + 2); :put $a;
7
Explain better, is not uderstandableHi
for this script i want updated new value to :local b
i dont need see on terminal i want save it to script as new value please help me.
:global b
{
:local a 5
:set a ($a + 2)
:set b ($a + $b)
:put $a
:put $b
}
i guest command :put just show result on terminal and cant update script yes?I not understand what you want do...
Code: Select all:global b { :local a 5 :set a ($a + 2) :set b ($a + $b) :put $a :put $b }
Yeah, the :put shows it. But variables defined a :local disappear when they leave the scope. If you :global instead that might help until you understand scoping rules.I guess command :put just show result on terminal and cant update script yes?
:global x 0
:set x ($x + 1)
:put $x
# shows: 1
:set x ($x + 1)
:put $x
# shows: 2
That's why you want your variable to be :global. Be aware it doesn't survive a reboot, variable are stored in memory.saved on script.and every time script run value be updated
many many thanks for your time.That's why you want your variable to be :global. Be aware it doesn't survive a reboot, variable are stored in memory.saved on script.and every time script run value be updated
So in example the $x will have that value for use elsewhere – no need to :put (print to console).
It's when use use "local", the variable may go "out of scope" (e.g. disappear), which may be confusion here. Parenthesis { } create a scope/block where it's set of local variables live – so as long as use modify AND use local variable INSIDE the { } grouping (e.g. in the same scope)... increment/update works totally fine with local too, just the variable will be lost when it hits the end of the code block e.g. "}"
Now since global variable exist across terminals/sessions (but not reboots), you do want to be careful with naming so global variables are unique. My $x is a poor example for using globals BTW – since if OTHER script was using $x, I'd be changing it for your example.
:global mywgname "<myWG>"
{
:local wginterface [/interface/wireguard/find comment=$mywgname]
:local wgcurport [/interface/wireguard/get $wginterface listen-port]
/interface/wireguard/set $wginterface listen-port=($wgcurport + 2)
}
:global mywgname "<myWG>"
{
:local wginterface [/interface/wireguard/find comment=$mywgname]
:local wgcurport [/interface/wireguard/get $wginterface listen-port]
/log info "about to update WG for $mywgname with id $wginterface using port $wgcurport"
/interface/wireguard/set $wginterface listen-port=($wgcurport + 2)
:delay 1s
:set wgcurport [/interface/wireguard/get $wginterface listen-port]
/log info "updated WG for $mywgname to use NEW port: $wgcurport"
}
listen-port=(1000 +[:pick [/system/clock/get time] 3 5])