Community discussions

MikroTik App
 
jeroenp
Member Candidate
Member Candidate
Topic Author
Posts: 159
Joined: Mon Mar 17, 2014 11:30 am
Location: Amsterdam
Contact:

[SOLVED] What's the meaning of "no such item" and why does it terminate a script?

Thu May 12, 2016 11:01 pm

Searching in http://wiki.mikrotik.com/wiki/Manual:Scripting I could not find the meaning of "no such item" that I sometimes get in scripts and which aborts the script.

Two questions:

1. What's the meaning of that message?
2. Why does it abort a script?

In the mean time I'm going to track down where in the script it terminates.

There are some Google Search results, but to me they're not conclusive: https://www.google.com/search?q=mikrotik+"no+such+item"

--jeroen
Last edited by jeroenp on Mon May 16, 2016 12:48 am, edited 1 time in total.
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7056
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Wat's the meaning of "no such item" and why does it terminate a script?

Fri May 13, 2016 10:38 am

It is when your script is trying to access item that does not exist any more. Dynamic entry that was removed etc.
 
jeroenp
Member Candidate
Member Candidate
Topic Author
Posts: 159
Joined: Mon Mar 17, 2014 11:30 am
Location: Amsterdam
Contact:

Re: Wat's the meaning of "no such item" and why does it terminate a script?

Fri May 13, 2016 5:54 pm

It is when your script is trying to access item that does not exist any more. Dynamic entry that was removed etc.
What do you mean with `etc.`? Can you be more specific? Are there examples that show this going wrong at will? (i.e. reproducible cases).

--jeroen
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Wat's the meaning of "no such item" and why does it terminate a script?

Fri May 13, 2016 7:39 pm

To be more precise, menu specific "get" and "set" commands (be it "/interface get", "/interface set", "/queue simple get", "/queue simple set", "/ip arp get", "/ip arp set", etc.) throw this error when you give a non existent ID or name as value for the "number(s)" argument.

f.e.
/queue simple get "test"
is a valid command call (so it's no syntax error), and is semantically equivalent to
/queue simple get number="test"
If a queue with name "test" existed, then the command will return the queue *(as an associative array with each property being array key, and the corresponding value being the member's value).

But if there is no queue with the name "test" at the time the command is actually called, the error is thrown.

If an item (be it a queue, an interface, an ARP item... anything, at any menu) is dynamically generated, and is suddenly removed, you could end up in situations where in one second, a "print" at the menu would tell you the item exists, and a second later, it won't.

An example of dynamically generated items are for example queues, which can be generated from various places, such as the "/ip hotspot" menu - you set up queues for each logged in user, and the queue gets created when the user logs in, and gets removed when the user logs out. Any scripts that do something while the user is logged in may end up in a situation where they attempt to do something, but in the middle of it, the user logs out, causing any future get/set calls related to them to fail.

* Wrong. Sorry. The above would error that you need to specify a property to retrieve. You must specify that as a second argument, at which point the command returns the value of the specified property... The point about the error being thrown when the item doesn't exist is still valid, but my description of "get" was utterly wrong.
Last edited by boen_robot on Mon May 16, 2016 3:13 pm, edited 2 times in total.
 
jeroenp
Member Candidate
Member Candidate
Topic Author
Posts: 159
Joined: Mon Mar 17, 2014 11:30 am
Location: Amsterdam
Contact:

Re: Wat's the meaning of "no such item" and why does it terminate a script?

Mon May 16, 2016 12:47 am

To be more precise, menu specific "get" and "set" commands (be it "/interface get", "/interface set", "/queue simple get", "/queue simple set", "/ip arp get", "/ip arp set", etc.) throw this error when you give a non existent ID or name as value for the "number(s)" argument.
Thanks. Now I know what's wrong. I've an associative array with IDs by name. Sometime that ID isn't there, so the `get` won't get it and - instead of the nil I expected - barfs.

--jeroen
 
User avatar
Deantwo
Member
Member
Posts: 331
Joined: Tue Sep 30, 2014 4:07 pm

Re: [SOLVED] What's the meaning of "no such item" and why does it terminate a script?

Wed May 18, 2016 12:30 pm

A good rule of thumb is to check if an object exist before trying to get/set it.
Quick example:
/interface {
    :local varif [find name="WAN" disabled=no]
    :if ([:len $varif] != 0) do={
        :put "Interface \"WAN\" was enabled, disabling."
        set $varif disabled=yes
    } else={
        :put "Interface \"WAN\" was not found or is already disabled."
    }
}
It is however possible to do this in one simple line if you don't care rather it was executed or not.
/interface set [find name="WAN" disabled=no] disabled=yes
This command will only run if the "[find]" actually returns an object. So it is safe to use in a script, even if there is no interface named "WAN" enabled.
 
jeroenp
Member Candidate
Member Candidate
Topic Author
Posts: 159
Joined: Mon Mar 17, 2014 11:30 am
Location: Amsterdam
Contact:

Re: [SOLVED] What's the meaning of "no such item" and why does it terminate a script?

Wed May 18, 2016 1:35 pm

...
/interface {
    :local varif [find name="WAN" disabled=no]
    :if ([:len $varif] != 0) do={
        :put "Interface \"WAN\" was enabled, disabling."
        set $varif disabled=yes
    } else={
        :put "Interface \"WAN\" was not found or is already disabled."
    }
}
Does `:len` work for `:typeof` returning "nil"?

(I used "nil" which in my case seems to work, but maybe a `:len` check is more general and therefore preferred).

--jeroen
 
User avatar
Deantwo
Member
Member
Posts: 331
Joined: Tue Sep 30, 2014 4:07 pm

Re: [SOLVED] What's the meaning of "no such item" and why does it terminate a script?

Wed May 18, 2016 2:13 pm

...
/interface {
    :local varif [find name="WAN" disabled=no]
    :if ([:len $varif] != 0) do={
        :put "Interface \"WAN\" was enabled, disabling."
        set $varif disabled=yes
    } else={
        :put "Interface \"WAN\" was not found or is already disabled."
    }
}
Does `:len` work for `:typeof` returning "nil"?

(I used "nil" which in my case seems to work, but maybe a `:len` check is more general and therefore preferred).

--jeroen
I am not totally sure what is actually preferred.

I always use ":len" to get the count of the objects, this may make a difference if the "[find]" returns an array of objects rather than a single object. At least it is a lot easier to make it work specifically with single objects by doing "[:len $varif] = 1".

Edit: My idea of it returnimg a single object array may be a little off. Since "[/interface find name=WAN]" will return "*0", which has a length of 2.

Who is online

Users browsing this forum: erkexzcx and 67 guests