no such item breaks execution

I need to do a simple test to check if a connection is alive.
The connection has a connection mark and I just need to test if it exists or not.
If it exists the script will send an email or log it.

I have this:

if ([/ip firewall connection get value-name=connection-mark [find connection-mark="KRN"]]="KRN") do={ :log warning "KRN is OK;} else={ :log warning "KRN is gone"; }

If the connection exists all is fine. If it doesn’t exist the script will be interrupted by “no such item” and it will not anything after that.
I need a way to catch this error or some sort of test so I can tell the script to do something when there is no connection.
Either that or a better way to test the existance of the connection.

Thanks

use on-error={} for the “it’s down” condition instead of else={}

in your command, “get” is expecting at least 1 item in its list, and when it gets nothing from the find command, the arguments aren’t being supplied correctly so it is actually failing to run. on-error{} gives the script a branch to follow in this case.

Or you could change your logic to something like this:

/ip firewall connection
if ( [ :len [ find where conection-mark=“KRN” ] ] > 0 ) do={ :log warning “KRN is OK” } else={ :log warning “KRN is gone” }

Thanks a lot.
I went for the second choice which seems cleaner and I have used it in other programming languages. I just didn’t know there was a length function in the scripting language.
Thanks again, I’ve been struggling a bit with this although now it seems extremely easy.

In your case, I definitely agree.

Do put on-error{} in your mental toolbox, though, because sometimes that’s how it’s done. I used that for my dyndns update script, for instance, because if the tool fails, I want the script to keep running and log this information.

very helpful !
.
after finding the post, … I needed ‘my time’ to skip the ‘if’-thing … but now it’s done … jfc … :open_mouth:
.
.

:do {/interface/wireless/registration-table/get [/interface/wireless/registration-table/find where interface=wlan1]} on-error={
/interface/bridge/set w_bridge disabled=yes
:delay 1s
/interface/bridge/set w_bridge disabled=no 
};

.
.
the snippet below, looked very good for me … for hours :sunglasses: … but “no such item” is a ruthless one :confused:
.

:if ([/interface/wireless/registration-table/get [/interface/wireless/registration-table/find where interface=wlan1] ap] =true) do={ :put on } else={ 
/interface/bridge/set w_bridge disabled=yes
:delay 1s
/interface/bridge/set w_bridge disabled=no 
};

If the scripts were written correctly, they would not give errors.

If the purpose of the script is to
if printing only the count of the interface registered on wlan1, return 0, disable the bridge w_bridge and after 1 second enable it again (for what purpose???),
that is certainly not the way.

purpose of the script is to reset the w_bridge interface when interface wlan1* loses the connection to it’s SSID and release the dhcp-client on w_bridge …
to give finally way for the lower-priority default-route … on another dhcp-client-interface …
.
… there may be other opportunities ?! … suggestions ?
.
first script works fine …
.
second one … not so … because: “no such item” is a ruthless one : ) … ( in this post just to illustrate, that running in the wrong direction … mostly is a long run )
.
.
*) bridge-port of w_bridge

Why not simply release the dhcp instead of disabling bridge for do that?
Something that I do not have considered?

good point … guess since the problem was the default-route, I engaged on the related interface … habit
… releasing the dhcp-client is way cleaner … and without the delay-thing … and single line
good point … changed ! … thanks

:slight_smile: