Community discussions

MikroTik App
 
dfdf
newbie
Topic Author
Posts: 36
Joined: Wed Dec 08, 2021 3:51 pm

Can somebody explain how this script works?

Sun Jan 08, 2023 1:48 pm

I was testing some script to allow variable function name. And suddenly found very confusing result for this short script. Tested on ROS 6.49 and 7.6. Can someone explain how it works?
:local cookie "someName";
:local cookieCode do={
 :put "TestIt";
 /system routerboard print;
};

[[:parse (":global ".$cookie)]];
[[:parse (":global ".$cookie."; :set \$".$cookie." (\"\");")]];

/system script environment remove [find where name=$cookie];
Output:
TestIt
       routerboard: yes
        board-name: hAP ac
             model: RB962UiGS-5HacT2HnT
     serial-number: XXXXXXXXXXX
     firmware-type: qca9550L
  factory-firmware: 6.43.10
  current-firmware: 7.6
  upgrade-firmware: 7.6
I suppose this is a bug, because another variant is more interesting:
:local cookie "someName";

:local cookieCode do={
 :put "TestIt";
 /system routerboard print;
};

:local cookieCode2 do={
 :put "TestIt2";
  /system resource print;
};


[[:parse (":global ".$cookie)]];
[[:parse (":global ".$cookie."; :set \$".$cookie." (\"\");")]];

/system script environment remove [find where name=$cookie];
Output:

TestIt

       routerboard: yes
        board-name: hAP ac
             model: RB962UiGS-5HacT2HnT
     serial-number: XXXXXXXXX
     firmware-type: qca9550L
  factory-firmware: 6.43.10
  current-firmware: 7.6
  upgrade-firmware: 7.6
  
TestIt2

                   uptime: 2w6d4h21m51s
                  version: 7.6 (stable)
               build-time: Oct/17/2022 10:55:40
         factory-software: 6.43.10
              free-memory: 63.7MiB
             total-memory: 128.0MiB
                      cpu: MIPS 74Kc V5.0
                cpu-count: 1
            cpu-frequency: 720MHz
                 cpu-load: 18%
           free-hdd-space: 512.0KiB
          total-hdd-space: 16.0MiB
  write-sect-since-reboot: 120848
         write-sect-total: 1554295
               bad-blocks: 0%
        architecture-name: mipsbe
               board-name: hAP ac
                 platform: MikroTik
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3279
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Can somebody explain how this script works?

Sun Jan 08, 2023 2:43 pm

What is the goal of your testing? What do you need to have done?
Get router board info?
I need a script that ......

PS you can remove ; at end of each line. Only needed when you have multiple commands at same line.
 
dfdf
newbie
Topic Author
Posts: 36
Joined: Wed Dec 08, 2021 3:51 pm

Re: Can somebody explain how this script works?

Sun Jan 08, 2023 2:59 pm

I know about semicolons. that's doesn't matter, i prefer C-like style writing.
Routerboard info is above in my initial post, but this behavior is observed on different devices.

I was finding a way to have a global function with generated name (stored in variable).
Smth. alike (not working):
:local funcName "myFunc";
:global $funcName do={ :put "Hello"; };

:execute { $funcName };

Last edited by BartoszP on Sun Jan 08, 2023 3:37 pm, edited 1 time in total.
Reason: removed excessive quotting of preceding post; be wise, quote smart, save network traffic
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3279
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Can somebody explain how this script works?

Sun Jan 08, 2023 9:13 pm

Nor 6 or 7 RouterOS accept $ in the global variable.
You did not write what you trying to get out of this:

Example on working code.
:global sum do={ :return ($1 + $2)}
:put (14 - [$sum 3 4])  
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Can somebody explain how this script works?

Mon Jan 09, 2023 2:14 am

How define dynamic variables:
viewtopic.php?f=9&t=178435&p=879152#p879152


You do not specify where you try to run this "script", I assume the Terminal at this point.

Script analysis. part one:
:local cookie "someName";
:local cookieCode do={
 :put "TestIt";
 /system routerboard print;
};

[[:parse (":global ".$cookie)]];
[[:parse (":global ".$cookie."; :set \$".$cookie." (\"\");")]];

/system script environment remove [find where name=$cookie];

1) remove all frills
2) replace long chain of statements like "1 + 1 + 1 + 1 + 1" with a chain that does the same thing, but shorter like "1 × 5" (just an example)
3) fix $ error after :set and added missing { } for terminal.

{
:local cookie "someName"

:local cookieCode do={:put "TestIt"; /system routerboard print}
# create a function called cookieCode and the function do what is present between the { }

[[:parse ":global $cookie"]]
# is like >:global someName<
# what this do: on parse session define global variable someName with "nothing" value
 
[[:parse ":global $cookie; :set $cookie \"\""]]
# is like >:global someName; set someName ""<
# what this do: on parse session define global variable someName with "nothing" value,
# and later set someName global variable = to "" (empty string)

/system script environment remove [find where name=$cookie]
# what this do: remove global variable "someName" from environment
}

But we don't give a damn about all this, in reality what we notice is that the function is called even if it is never written to use it:

For extremely synthesize the compiler bug:

terminal code

[9b184@MATRIX] > :local test do={:put "WHY\3F\3F\3F"};[];[]
WHY???
[9b184@MATRIX] >


Part two is just the same as part one, just multiplied:

Example code

{
:local test1 do={:put "test1"}
:local test2 do={:put "test2"}
:local test3 do={:put "test3"}
:local test4 do={:put "test4"}
:local test5 do={:put "test5"}
[]
[]
}
on second call [] all function defined before are executed...


Temporary (I hope) fix: assign the returned value to one useless variable:
{
:local cookie "someName"

:local cookieCode do={:put "TestIt"; /system routerboard print}

:local useless [[:parse ":global $cookie"]]
 
:local useless [[:parse ":global $cookie; :set $cookie \"\""]]

/system script environment remove [find where name=$cookie]
}

Tested on 6.48.6 and 7.6
 
User avatar
pkt
just joined
Posts: 14
Joined: Tue Jan 24, 2023 10:12 pm
Location: /u/mw/ss/e/eu/es

Re: Can somebody explain how this script works?

Sun Feb 05, 2023 11:03 am

I have also run into this nasty bug of [...] executing all functions in the script.

In my case, the function first called was not the first declared, but the first in alphabetical order: deleteSMS.

I guess this was because all functions were "methods" of an array, and as array members are stored alphabetically by key, so was the execution order.

Fortunately, as the functions seems to be called without arguments, it failed with an error and execution of the others stopped also.

But this is really dangerous.

Your workaround of assigning [] to a variable worked great. Thanks!

Who is online

Users browsing this forum: Rhydu and 14 guests