exit or break a loop statement

Hello everybody, I’m making a script that ping more than 3 hosts, if first fail then ping second and so on, but if some of the hosts it’s up then the loop should terminate and dont ping the rest of the servers, in mikrotik language there is some break or exit order than can make this possible i need break or exit the loop inmediatly when some host respond to ping. thanks


***** this works, but somebody has another idea..? *****

:local continue true;
:local counter 0;
:while ($continue) do={
   :log info $continue
   :set counter ($counter + 1);
   :log info $counter;
   :if ($counter=50) do={:set continue false;}
}
:log info "end"

Currently there is no “break” or “exit” in RouterOS scripting.
You will have to use “while” loop just like in your example.

Right, Thanks, the problem was resolved with while. more job to do but the same result.

Any plans to add loop “break” or “exit” in RouterOS scripting?

+1
This would be useful… The while continue will work if you want to break at the end of the while loop but for the loop to actually stop, processing has to make it back to the “:while ($continue) do={”. This means that if you set set continue to false half way though the while loop the rest of the while loop still processes.

A way around this is to have every statement in the while loop enclosed in a “:if ( $continue = true ) do={” block.

If you have a large while loop this makes allot of extra code just to do a break; :frowning: .

Thanks Mikrotik team for your work in 6.2 to make functions much cleaner. Scripting just got a little more fun :smiley:.

+1.

I found one optional solution - use error, this will stop script.
Wiki:
error :error Generate console error and stop executing the script

Thank you!

+1
Thanks …

I am not certain how to use “while ($continue)” loop in the same manner as a foreach loop.

I’m trying to add a rule before the first drop action in the filters.. maybe there is a better way to do this …
foreach filterrule in [/ip firewall filter find where action=drop] do={
/ip firewall filter add action=accept chain=input comments=“Remote Administration” dst-port=8291 protocol=tcp src-address-list=REMOTE_Admin
break
}
Of course that doesn’t work…

Nevermind … I figured it out an alternative method using place-before and :pick/:find
/ip firewall filter add action=accept chain=input comments=“Remote Administration” dst-port=8291 protocol=tcp src-address-list=REMOTE_Admin place-before=[:pick [find action=drop] 0]
Will leave my post just incase someone else is looking for same information

With “on-error” it is now possible to break a for/foreach loop and continue with the rest of the script.
In that case you even have the possibility to execute extra instructions in case the loop is exited prematurely.

:do {
   :foreach i in 1,2,3,4,5,6 do={
      :if ($i = 4) do={
         :put "break?";
#        This is generating a run-time error:
         /break;
      }
      :if ($i = 2) do={
         :do {
            :put "break?";
            /break;
         } on-error={
            :put "error handled. do not break.";
         }
      }
      :put $i;
   }
} on-error={
   :put "break!";
}

Output:

1
break?
error handled. do not break.
2
3
break?
break!

The topic was created 15 years ago and still no “break” functionality for loops that exists in every programming language. Very pity…

Are you sure?

In what?

@optio write:

http://forum.mikrotik.com/t/executing-script-from-net-failed/180687/1

/quit