Community discussions

MikroTik App
 
User avatar
SiB
Forum Guru
Forum Guru
Topic Author
Posts: 1888
Joined: Sun Jan 06, 2013 11:19 pm
Location: Poland

7.1rc3, set $variable or set variable in system/script

Sun Sep 19, 2021 10:54 pm

In CLI/Terminal this works perfect. Global "WAN1DownCounter" is increase itself in every run that code in CLI.
if ([len [system/script/environment/find name=WAN1DownCounter ]] = 0 ) do={ global WAN1DownCounter 1 } else={ set WAN1DownCounter value=([system/script/environment/get WAN1DownCounter value]+1) }
:
But if I create a script1 with that code and run it at 7.1rc3 then it's not work properly.
system/script/run script1
syntax error (line 1 column 115)
When I change problematic set WAN1DownCounter to set $WAN1DownCounter then it's start working... but still not proper. He create global but not inc it value. No more syntax error.
 
User avatar
SiB
Forum Guru
Forum Guru
Topic Author
Posts: 1888
Joined: Sun Jan 06, 2013 11:19 pm
Location: Poland

Re: 7.1rc3, set $variable or set variable in system/script

Mon Sep 20, 2021 2:39 pm

@rextended Please help :)
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1070
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: 7.1rc3, set $variable or set variable in system/script

Mon Sep 20, 2021 5:55 pm

Try this:
:global WAN1DownCounter;
:if ([ :typeof $WAN1DownCounter ] = "nothing") do={
  :set WAN1DownCounter 1;
} else={
  :set WAN1DownCounter ($WAN1DownCounter + 1);
}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: 7.1rc3, set $variable or set variable in system/script

Mon Sep 20, 2021 6:05 pm

@rextended Please help :)
Ohhh... call me on topic where already I have wroted :)

NOw I read and reply to your post
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: 7.1rc3, set $variable or set variable in system/script

Mon Sep 20, 2021 6:19 pm

Ok, little "tutorial"

1) please proper indent, and use the : and / everityme!
:if ([:len [/system/script/environment/find where name=WAN1DownCounter]] = 0) do={
    :global WAN1DownCounter 1
} else={
    :set WAN1DownCounter ([/system/script/environment/get WAN1DownCounter value] + 1)
}

2) the right way to use a variable is to call it directly, not searching everytime on environment
why not work: before run the ":if" the ":set WAN1DownCounter" can not find "WAN1DownCounter" because is undeclared.
Soluction: delcare it (:global WAN1DownCounter do not assign any value, and do not alter it if is already set)
:global WAN1DownCounter
:if ([:len $WAN1DownCounter] = 0) do={
    :global WAN1DownCounter 1
} else={
    :set WAN1DownCounter ($WAN1DownCounter + 1)
}

3) but now we have another problem, the first time is run do error because undefined variable + 1 fail
the soluction is check first if WAN1DownCounter is a number, if not set it to 1, if yes add 1 to actual value:
:global WAN1DownCounter
:if ([:typeof $WAN1DownCounter] = "num") do={
    :set WAN1DownCounter ($WAN1DownCounter + 1)
} else={
    :set WAN1DownCounter 1
}

Done.
Last edited by rextended on Mon Sep 20, 2021 6:27 pm, edited 2 times in total.
 
User avatar
SiB
Forum Guru
Forum Guru
Topic Author
Posts: 1888
Joined: Sun Jan 06, 2013 11:19 pm
Location: Poland

Re: 7.1rc3, set $variable or set variable in system/script

Mon Sep 20, 2021 6:24 pm

Thanks for answers !
Sorry but OP cannot put both SOLVED
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: 7.1rc3, set $variable or set variable in system/script

Mon Sep 20, 2021 6:28 pm

No problem, but in my post I also put in the explanations :-P

About @eworm soluction:
is pretty the same:
I check if is it a number, because MUST be a number, if is it I add one, else I set to 1 the variable
instead @eworm check if is "nothing", and if is it set to 1 the variable, else add one
Last edited by rextended on Mon Sep 20, 2021 6:33 pm, edited 1 time in total.
 
User avatar
SiB
Forum Guru
Forum Guru
Topic Author
Posts: 1888
Joined: Sun Jan 06, 2013 11:19 pm
Location: Poland

Re: 7.1rc3, set $variable or set variable in system/script

Mon Sep 20, 2021 6:31 pm

Thank you for a "learning style" and other your feedback in my other ones scripts!

Who is online

Users browsing this forum: wirelesslywired and 8 guests