Community discussions

MikroTik App
 
globalmedia
newbie
Topic Author
Posts: 30
Joined: Mon Mar 20, 2023 11:09 pm

Script not running

Fri May 31, 2024 11:05 pm

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.
Last edited by chechito on Sat Jun 01, 2024 12:40 am, edited 1 time in total.
Reason: edit misleading title
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12652
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: System Script not running

Fri May 31, 2024 11:08 pm

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.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4467
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: System Script not running

Sat Jun 01, 2024 1:10 am

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)

 
globalmedia
newbie
Topic Author
Posts: 30
Joined: Mon Mar 20, 2023 11:09 pm

Re: Script not running

Sun Jun 02, 2024 4:57 pm

Thank you so much for both you guys. I am very grateful for your explanations. Helped me a lot.
 
globalmedia
newbie
Topic Author
Posts: 30
Joined: Mon Mar 20, 2023 11:09 pm

Re: Script not running

Sun Jun 02, 2024 5:35 pm

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?
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4467
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: Script not running

Sun Jun 02, 2024 6:17 pm

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.