In a RouterOS script is there an equivalent to a GoTo statement in Basic? What I’m trying to accomplish is if a certain variable is some value, I want to skip a section of the script (about 30 lines). I can at least sort of do it with a :if, but it can be a bit convoluted. This will be repeated many times within the script.
Presumably since RouterOS does not have line numbers like BASIC (thank God), it would require a label and the GoTo would go to the label. I did not see either of these in the WiKi.
Note, I am doing this in RouterOS 6.49.15 on an RB4011iGS+
Not quite so easy. Each time the script would have to call a different function (several dozen) because each section is different.
Possible? Yes. Easy? No.
If you ever wrote serous code for some application you will know that code will contain several hundreds or thousands of functions. If you have some code complexity it should be splitted into functions for code reusability and readability, mostly always unless is some short POC code or simple script. GoTo is obsolete way of taking parts of code for reusability or skipping parts of commands for execution, inherited from assembly jump (jmp) instruction, much less readable and most modern languages doesn’t have support for it because it is just not needed on higher level of coding.
You made me to ask and see what is its actual answer for it :
The goto command, once a staple of early programming languages, has fallen out of favor in most modern languages for several key reasons:
Readability and Maintainability: Code that relies heavily on goto can become tangled and difficult to follow, especially in larger projects. When the flow of control can jump unpredictably between different parts of the code, it becomes much harder to understand the program’s logic at a glance. This makes the code more prone to errors and harder to maintain over time.
Structured Programming: In the 1970s, the concept of structured programming became popular, thanks largely to pioneers like Edsger Dijkstra. Structured programming encourages the use of control structures like loops (for, while) and conditionals (if, else) to create clear, linear control flow. These structures help to avoid the “spaghetti code” that goto can cause, which is chaotic and difficult to trace.
Error Handling: Before goto was deprecated in favor of more structured approaches, it was often used for things like error handling (e.g., jumping to a cleanup section in case of an error). Modern languages have better mechanisms for error handling, such as exceptions (try, catch, finally), which are more predictable and easier to manage.
Optimization: Compilers and interpreters can more easily optimize structured code, which relies on defined control flow (loops, conditionals), than code that makes arbitrary jumps using goto. This allows for more efficient machine code generation and optimization.
Control Flow Alternatives: Modern programming languages provide other ways to manage program flow more safely and clearly. For example, functions, method calls, and exception handling provide much more structure and clarity than goto.
Historical Lessons: Over time, the industry learned that goto—while powerful—often led to bad programming practices. Early languages like C, BASIC, and assembly used goto frequently, but it quickly became clear that its unchecked use contributed to the creation of unreliable and hard-to-manage codebases.
In short, goto is avoided because it’s a low-level control structure that can lead to messy, unmanageable code. Modern languages prefer higher-level, more structured alternatives to handle flow control, which makes the code clearer, more predictable, and easier to maintain.
FWIW, if you already have some procedural script with a lot of variables, which @k6ccc likely has… you cannot so easily just put everything in new local functions, since you cannot access other local variables/functions. (And, yes @optio, there are tricks like your [$mylocalfunction parentvar=$parentval] - but if we’re starting with no functions… it may not be so easy to adapt.)
I think the best you can do is make tiny local function to do some of the more complex work of the :if statement you have. The key part is local function CANNOT access other variables, only what’s provided.
Perhaps you can post the relevant script example. But without GOTO, it might be good chance to learn about functions… Or god forbid, recursive functions, which do work.
Anyway if you post script, you have all the usual suspects just waiting to comment on other people’s code.
Well, I did a little clean up and made it far easier to just use a :if to skip the desired parts.
However I ran into a situation I could not find in the Wiki. I hit the maximum size if a script. I did not know there was a maximum size, but the script editor in Winbox would not allow adding any more characters. So my question is what is the maximum size of a script, and is there a way to see the actual size?
I was able to find the size by copying the entire script from Winbox and pasting it into Notepad, then saving it, and have windows tell me the file size. Awkward, but it worked. After my cleanup the script is currently just under 28K characters, so I am guessing the limit is 32K.
FWIW, limit is 64KB in V7. But even in 7.17, /system/script/edit will only view up to first 32KB files, while both winbox and webfig will let you use the “full” 64KB allowed.
Thanks. Note that I’m on 6.49.15 and Winbox 3.41. I was no where near 64KB, but could have hit 32K. If I have time, I will run a test. Might then try with Winbox 4 to see if that is any different. Push comes to shove, I have ROS 7.something fairly recent on another router and may see what that allows.
As a little followup to the script size limit I was running into, I ran into this issue again. Confirmed Winbox 3.41 with a RB4011iGS+ / 6.49.15 would let me add characters after 32K. Downloaded WinBox 4.0 beta 17 and tried it. No problem editing and saving a script that exceeded 32K. So good news for me…
While I cannot fathom anyone editing a large (or really any) script using the WinBox dialog, the dialog will let you TYPE above 64Kb into the edit box… it just doesn’t save that part.
Interesting. On my RB4011iGS+ with 6.49.15 and WinBox 3.41 the editor would completely stop accepting new characters typed or pasted once it hit 32K. Sounds like 64K is the limit in WinBox 4. The script I am working on is no where near that large - and I don’t expect it to get there…