I want to get the output of a “print” command inside a script into a variable. The [/command/print] construct gets the output in the variable, but it still also prints it.
E.g.:
This script walks the active session list of BGP and reads the number of prefixes received on each of them (due to lack of RouterOS v7 providing this in the session info).
The script works OK (as of RouterOS 7.8beta2), but before it outputs the alphabetically sorted list of BGP peers and their prefixes, it first prints the prefix numbers in the random order the sessions are in the list. That is because the
:set $prefix [/routing/route/print count-only where belongs-to="bgp-IP-$remote"]
not only puts the value in $prefix but also outputs it to the terminal.
How can I suppress that?
I know that that works, but it is extremely inefficient.
In 7.8beta2 (as I wrote) the “count-only where…” works OK and is a lot faster than fetching all the routes in an array and counting the members.
I am not looking for “how can I get the result in a different way” but for “how can I catch the output in a variable without it also appearing on the terminal”.
That is a general issue that could also become relevant in other contexts.
Thank you for the hint! Now I seem to remember as-value but the docs of scripting are so inaccessible…
Indeed, it appears the combination of “as-value” and “count-only” does not work…
It looks like the programmer of the new routing was no well integrated with RouterOS conventions, the parameter structure is also very different from tradition.
My SUP ticket was answered with “There will be major console improvements in the near future, we will include this feature too.”
So there is hope that we will soon see some improvement.