Page 1 of 1

Script not running

Posted: Fri May 31, 2024 11:05 pm
by globalmedia
Dear users, hope you doing well. I am trying to learn more about scripting in RouteOS so I am writing some codes to help me understand the possibilities. But I need a way to debug the script and it's no working well.

There is a button called ˜Run Script" in the script window, but when I run it I just do not receive any message in Log.

do {
:local notificationTeam ({{"1";"2"};{"3";"4"}});
:local Host $host
:put "$notificationTeam->0"
/log info message="$notificationTeam->0"
} on-error={
:put "Erro ao rodar script netwatch"
}

I don't know why, but the :put command just doesn't work for me.
/log info works well, I can see the debug command... But I am not obtaining the correct value from arrays. How to correctly access the array information to obtain 1 and 2... and... 3 and 4 for the second array?

Hope someone can help me in my journey of script learning.

Re: System Script not running

Posted: Fri May 31, 2024 11:08 pm
by rextended
Have you even read this?
https://wiki.mikrotik.com/wiki/Manual:Scripting

Does the very simple example seem consistent with what you wrote?

example on wiki code

:global aaa {a=1;b=2}
:put ($aaa->"a")
:put ($aaa->"b")
First of all, immediately lose the habit of writing parentheses and semicolons haphazardly...
Also do not use names identical or similar of system words on the context used at that time.

Not fixing the errors, at least you must write the script on this way:

unfixed example code

do {
    :local notificationTeam { {"1";"2"} ; {"3";"4"} }
    :local hostName         $host
    :put      "$notificationTeam->0"
    /log info "$notificationTeam->0"
} on-error={
    :put "Erro ao rodar script netwatch"
}

Fixing the wrong way you have wroted the code:

corrected code

do {
    :local notificationTeam { {"1";"2"} ; {"3";"4"} }
    :local hostName         $host
    :put      ($notificationTeam->0)
    /log info ($notificationTeam->0)
} on-error={
    :put "Erro ao rodar script netwatch"
}

If you must add text on the message, the correct way for both :put and /log is:
:put      "notificationTeam is $[:tostr ($notificationTeam->0)]"
or
:put      "notificationTeam is $($notificationTeam->0->0) and $($notificationTeam->0->1)"
If you write on this way, is wrong:
:put "notificationTeam is $($notificationTeam->0)"
give predictable but unexpected results (notificationTeam is 1;notificationTeam is 2),
because the result is one array.

Re: System Script not running

Posted: Sat Jun 01, 2024 1:10 am
by Amm0
It a bit unclear whether you want to make one array with {1,2,3,4} or two-dim array like {{1;2};{3;4}}.

i.e.
:local notificationTeam {{"mateo";"mateo@example.com"};{"carlo";"carlo@example.com"}}
vs.
:local notificationTeam {{"sofia@example.com";"mateo@example.com"},{"beatrice@example.com";"carlo@example.com"}}

Note: the semi-colon ; appends to ITEM to a array (and the item could be an array). Or, the comma, join TWO LIST together into one (i.e. makes it one-dimensional). So comma and semi-colon will give radically different results.

It's sometimes hard to see what happens with arrays and the ":put" – they sometime will look the same, when printed, but be internally different... i.e.
{ 
:local semi {{"mateo";"mateo@example.com"};{"sofia";"sofia@example.com"}}
:put $semi
# mateo;mateo@example.com;sofia;sofia@example.com
:local comma ({"mateo";"mateo@example.com"},{"sofia";"sofia@example.com"})
:put $comma
# mateo;mateo@example.com;sofia;sofia@example.com

# they look the same, but not same when accessing...
:put ($semi->1->1)
# sofia@example.com
:put ($comma->1->1)
# (gets nothing)

# and only the comma version has a 3rd element...
:put ($semi->3)
# (gets nothing)
:put ($comma->3)
# sofia@example.com

# BOTH out put of :put look IDENTICAL
}

It is confusing.

So viewing the array as JSON often makes the differences more apparent when testing things out:

# if it was {name; email};...
:put [:serialize to=json {{"mateo";"mateo@example.com"};{"sofia";"sofia@example.com"}}]
# [["mateo","mateo@example.com"],["sofia","sofia@example.com"]]

# but the the comma, now a list....within a list
:put [:serialize to=json {{"mateo";"mateo@example.com"},{"sofia";"sofia@example.com"}}]
# [["mateo","mateo@example.com","sofia","sofia@example.com"]]

# so you do need parentheses if you want a one-dim array, without the extra outlist
:put [:serialize to=json ({"mateo";"mateo@example.com"},{"sofia";"sofia@example.com"})]
# ["mateo","mateo@example.com","sofia","sofia@example.com"]

# and you cannot even do this one.
:put [:serialize to=json ({"mateo";"mateo@example.com"};{"sofia";"sofia@example.com"})]
# syntax error (line 1 column 56)

# but it was just two list of emails, always semi-consoles
:put [:serialize to=json ({"igor@example.lv";"mateo@example.com"},{"nadia@example.lv";"sofia@example.com"})]
# ["igor@example.lv","mateo@example.com","nadia@example.lv","sofia@example.com"]
# or
:put [:serialize to=json (("igor@example.lv","mateo@example.com"),("nadia@example.lv","sofia@example.com"))]
# ["igor@example.lv","mateo@example.com","nadia@example.lv","sofia@example.com"]

# just not...
:put [:serialize to=json (("igor@example.lv","mateo@example.com");("nadia@example.lv","sofia@example.com"))]
# syntax error (line 1 column 45)


Re: Script not running

Posted: Sun Jun 02, 2024 4:57 pm
by globalmedia
Thank you so much for both you guys. I am very grateful for your explanations. Helped me a lot.

Re: Script not running

Posted: Sun Jun 02, 2024 5:35 pm
by globalmedia
Why the :put just doesn't work for me? The only way to see in console what is happening is using /log info for debug. Is that normal? Where put prints on?

Re: Script not running

Posted: Sun Jun 02, 2024 6:17 pm
by Amm0
Why the :put just doesn't work for me? The only way to see in console what is happening is using /log info for debug. Is that normal? Where put prints on?
That's expected in /system/script and /system/scheduler, or any of the "background" scripts. Basically there is no terminal where the :put can go to. You can monitor the logs from the console for script message using a filter like:
/log print follow where topics~"script" 
Which would show any "/log info" output from scripts until you stop it (or remove follow to just see past runs). _i.e._ when you hit "Run Script", you'd see the output in the window running above command in realtime.