catch 22 importing a RSC file

Good morning. We have a RSC file with this line:

/interface bridge port
  add bridge=bridge_name interface=ether9

It runs OK the first time, using the command “import my_config.rsc verbose=yes”
but if there are errors in the lines after this one, once we fix the error and import the file again,
we get the error

failure: device already added as bridge port

So, we decided to include the line

remove bridge_name

just before the “add bridge”

The problem is that in certain situations we get the error message “no such item”, and the import fails,
and we get into the initial situation, in a loop.

So I think the “remove” sentence must be “conditional” the the “existence” of this bridge,
but I dont know how to code this.

The pseudo-code can be similar to :

if exists(bridge-name) then delete(bridge-name)

Can someone provide me a sample or a pointer to a good documentation ?

Thanks.

You can start by reading about scripting from here:
https://help.mikrotik.com/docs/display/ROS/Scripting

There you can also find a way to intercept errors when your code is part of a do section by using on-error statement.
https://help.mikrotik.com/docs/display/ROS/Scripting#Scripting-Catchrun-timeerrors

Likely you have a reason for using RSC… but if you’re doing this on the same router AND want the entire config replaced, Backup/Restore from the Files menu might be easier. The “export” file is designed to be “import” to into an unconfigured router - as you found out, you can NOT just do “export”, and then “import” same file back without adding code yourself. (And it unfortunate there is not an option to have RouterOS do this task for you)

But another good example of the needed “:if do={} else={} on-error={}” syntax that would have to be added to an RSC, is actually the default configuration for the box, which you can see using for the specific router you’re dealing with using:

/system default-configuration print

Another forum posting shows default config from the Audience: http://forum.mikrotik.com/t/audience-default-config/139424/7