Local variable blank when trying to map OS version

I'm trying to have a single script for OS 6 and OS 7. As some commands like setting NTP client are different I'm looking to use logic based on OS version but am having issues capturing the Major OS version to a local variable. see the simple script below where i can log directly the lookup of "/system resource get version" command but if I map the output first to a local variable and then try to print to log the variable is empty.

Appreciate any direction our suggestions as I've been banging my head on this with no luck.

simple version detection (v6/v7)

:local verStr [:tostr [/system resource get version]]
:local majorStr [:pick $verStr 0 1]
:local majorNum [:tonum $majorStr]
:log info ("[test] ver=" . [/system resource get version] . " major=" . [:pick [/system resource get version] 0 1])
:log info ("[os] ver=" . $verStr . " majorStr=" . $majorStr . " majorNum=" . $majorNum)

log output:
jan/01 21:25:40 script,info [test] ver=7.6 (stable) major=7
jan/01 21:25:40 script,info [os] ver= majorStr= majorNum=

I found a work around but would still like to know a way of mapping a lookup and mapping to a local variable to use else where in a script. work around was to skip the local variable and put the command in the if / else / do command. example:
:if ([:pick [/system resource get version] 0 1] = 7) do={.........
this pulls the major version and if it = 7 does something if not skips to the else statement.

Hi whaller. To find the version I use the below

:local sysVersion [/system resource get version]
:put "### $name is running RouterOS version $sysVersion"
:local verBit [:pick "$sysVersion" 0]

And for the selection, you could do similar to below

:if ($verBit = "6") do={.....}
:if ($verBit = "7") do={.....}

Strange. I've just copied your script and tried to run it on both v6 and v7, output in not empty.

[test] ver=7.19 (stable) major=7
[os] ver=7.19 (stable) majorStr=7 majorNum=7

[test] ver=6.49.13 (stable) major=6
[os] ver=6.49.13 (stable) majorStr=6 majorNum=6

May be the problem exists in some specific ROS versions? Just guessing...

Did you run your code by pasting the lines directly into the terminal? If yes then the behavior is normal. Because when you run the code line-by-line in the terminal, the :local variable definition is only valid in the scope of that line, and the variable is gone when you execute the next line.

When running in the Terminal, you need to surround those line with { ... } if you want the local variables to stay in the same scope of all the lines. Or create a script under /system script and call it.

1 Like

Some advice on this matter...

This DO NOT WORK as expected...

See some hint on previous post.

Hi rextended. You're absolutely right.

Corrected it below

:local sysVersion [/system resource get version]
:local verBit [:pick "$sysVersion" 0]
:put "Router is running RouterOS version $sysVersion"
:put "verBit: $verBit"
:log warning "Router is running RouterOS version $sysVersion"
:log warning "verBit: $verBit"

Output:
Router is running RouterOS version 7.19.4 (stable)
verBit: 7

Router is running RouterOS version 6.49.10 (long-term)
verBit: 6

:if (($verBit = "6") or ($verBit = "7")) do={
  :if ($verBit = "7") do={.....}} else={.....}

or as you suggested, because you're right, any other previous version wouldn't matter

:if ($verBit = "7") do={.....} else={.....}

I wanted to be more diplomatic, but now I'll say it clearly:

Your method is wrong.

The two links I posted earlier contain one of the many correct methods, and also the reason why yours is wrong...


Ignoring other differences between, for example, 7.16.2 and 7.19.4, etc. and considering just two version:

:if ([/system resource get version]~"^7") do={
    # call script for v7
    /system script run v7script
} else={
    # call script for v6
    /system script run v6script
}

or more complicated, but working:

:execute "/system clock set time-zone-name=Greenwich"
:execute "/system ntp client set enabled=yes"
:execute "/system ntp client set primary-ntp=1.1.1.1 secondary-ntp=2.2.2.2"
:execute "/system ntp client add address=1.1.1.1"
:execute "/system ntp client add address=2.2.2.2"

Your method do not work, because one version do not support commands presents only on another...

{
/system clock set time-zone-name=Greenwich
/system ntp client set enabled=yes

:if (true) do={
    /system ntp client set primary-ntp=1.1.1.1 secondary-ntp=2.2.2.2
} else={
    /system ntp client
    add address=1.1.1.1
    add address=2.2.2.2
}

}

do this error on v7
expected end of command (line 6 column 28)

and do this error on v6
bad command name add (line 9 column 5)

Thanks folks for the feedback. rextended input especially. they echo my experience.
In short:

  1. logic to determine OS
  2. IF/ELSE to call script (internal or external)
  3. OS specific script run
1 Like