Page 1 of 1

macros bug

Posted: Fri Mar 22, 2019 1:34 am
by cmdorexe
as example if+else not work
global test yes
/log info [:typeof $test]
/log info [:typeof yes]
/log info ( yes = yes )
/log info ( $test = yes )
global print do={
  if ( $test = yes) do={/log info yes} else={/log info no}
}
$print
global test no
$print
I'm not understand why? Anybody have some sojestion with it.

Re: macros bug

Posted: Fri Mar 22, 2019 5:03 am
by Van9018
This page: https://wiki.mikrotik.com/wiki/Manual:S ... _statement
says the syntax of the if statement should be prefixed with a colon

{
:local myBool true;
:if ($myBool = false) do={ :put "value is false" } else={ :put "value is true" }
}

Re: macros bug

Posted: Fri Mar 29, 2019 5:10 pm
by cmdorexe
It's not working in scripts with log. if i did it in terminal it's work perfect.

Re: macros bug

Posted: Fri Mar 29, 2019 11:02 pm
by Chupaka
It's not working in scripts with log
Any details? Because my "details" work fine...
Screen Shot 2019-03-29 at 23.39.48.png

Re: macros bug

Posted: Wed Apr 03, 2019 2:50 pm
by BlackVS
Works in my case (the same result as in Chupaka) - CCR1009, the last firmware. Just copied your code in script called test_if and called it from terminal.

Re: macros bug  [SOLVED]

Posted: Thu Apr 18, 2019 11:42 am
by Chupakabra303
It's not working in scripts with log. if i did it in terminal it's work perfect.
Declare a global variable before use in function!
global print do={
:global test
if ( $test = yes) do={/log info yes} else={/log info no}
}

Re: macros bug

Posted: Thu Apr 18, 2019 11:56 am
by Jotne
if ( $test = yes) do={/log info yes} else={/log info no}
Will not work, you need to double quote yes like this:
if ( $test = "yes") do={/log info "yes"} else={/log info "no"}

To test things out:
:global test "yes"
[
:global test
if ( $test = "yes") do={:put "true"} else={:put "false"}
]
prints true to screen

and
:global test "no"
[
:global test
if ( $test = "yes") do={:put "true"} else={:put "false"}
]
prints false to screen

Re: macros bug

Posted: Thu Jun 27, 2019 3:13 pm
by cmdorexe
Will not work, you need to double quote yes like this:
if ( $test = "yes") do={/log info "yes"} else={/log info "no"}
it's string not bool! I'm write about bool's in scripts!

Re: macros bug

Posted: Thu Jun 27, 2019 7:46 pm
by Jotne
Then I do not understand what you try to do.

Re: macros bug

Posted: Sat Jun 29, 2019 7:58 am
by Chupaka
So what's exactly not working?

Re: macros bug

Posted: Wed Jul 17, 2019 8:04 pm
by cmdorexe
bool1.png
bool2.png

Re: macros bug

Posted: Wed Jul 17, 2019 9:13 pm
by Jotne
So what's exactly not working?
@cmdorexe
You do just show some code and does not respond to the question. What does not work?


If you try to run a script from cli, you need to wrap it in {}

Example
:local test "more"
:put $test
Needs to be
{
:local test "more"
:put $test
}
This do work also
[
:local test "more"
:put $test
]

Re: macros bug

Posted: Thu Jul 18, 2019 7:33 pm
by Chupaka
Oh, I see. That will do the trick:
:global test yes
:global print do={
  :global test
  if ( $test = yes) do={/log info yes} else={/log info no}
}
$print
:global test no
$print
By ":global test" you're saying you need to use 'test' global var in current visibility scope (function body).

Re: macros bug

Posted: Thu Jul 18, 2019 7:35 pm
by Chupaka
Or you can pass the var into function explicitly:
:global test yes
:global print do={
  if ( $test = yes) do={/log info yes} else={/log info no}
}
$print test=$test
:global test no
$print test=$test

Re: macros bug

Posted: Fri Jul 19, 2019 8:45 am
by cmdorexe
Or you can pass the var into function explicitly:
wow n1ce trick. I never known.