Community discussions

MikroTik App
 
janFfm
newbie
Topic Author
Posts: 26
Joined: Thu Feb 01, 2018 11:39 am

Using loop functions in rsc-files

Thu Feb 01, 2018 11:47 am

Hey guys,
quick question here.
I'm using rsc-files to configure my devices. Now when i try to use loop statements in the file, it won't load.
Is it generally not possible to use loops or am i doing something wrong?

Thanks a million in advance
J.
 
User avatar
ochaconm
Trainer
Trainer
Posts: 28
Joined: Fri Feb 22, 2013 9:39 pm

Re: Using loop functions in rsc-files

Mon Feb 05, 2018 1:41 am

I would suggest you create a script into your rsc file and call it after creation.
Then, your script can include any loop supported by the Mikrotik scripting language.
E.g
/your commands
/system script
add name=your_script_name ....\
...\
...
/system script run your_script_name
For more information about Mikrotik Scripting see:
https://wiki.mikrotik.com/wiki/Manual:Scripting

I hope this could help you.
 
User avatar
BlackVS
Member Candidate
Member Candidate
Posts: 171
Joined: Mon Feb 04, 2013 7:00 pm
Contact:

Re: Using loop functions in rsc-files

Mon Feb 05, 2018 8:29 am

Hey guys,
quick question here.
I'm using rsc-files to configure my devices. Now when i try to use loop statements in the file, it won't load.
Is it generally not possible to use loops or am i doing something wrong?

Thanks a million in advance
J.
put your script here
 
janFfm
newbie
Topic Author
Posts: 26
Joined: Thu Feb 01, 2018 11:39 am

Re: Using loop functions in rsc-files

Mon Feb 05, 2018 4:51 pm

Thank you guys,
@ochaconm
ok, this sort of leads me to another question:
- would variable values created in an external script be available to the calling script (using ":global" they should, right?) and
- would they be available fast enough?

@BlackVS
it goes something like:
---------------------------------
:global AR [:toarray 1,2,3,4]
:global a "111"
:global b "222"
.
.
:global x "999"
:foreach i in=$AR do={:set $x ($x , ( $i . "00"))}
----------------------------------
All variables up till this "foreach"-statement are available, but not the new value of $x and none of the configuration, coded by the following lines will get applied.
... Terminal will output: "script executed successfully" though.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Using loop functions in rsc-files

Mon Feb 05, 2018 5:49 pm

From some quick tests, :global works as expected... The tricky part is to use it without a value in script in order to import a variable without setting it.

If you call :global WITH a value, you are effectively setting its value to the one specified... The difference with ":set" being that the variable is declared if it doesn't exist.

if you call :global WITHOUT a value, you are either creating the variable with a value of "nothing" (if it doesn't exist), or importing it with its current value if it does exist.

You should initialize your global variables in a separate script (perhaps at startup), and in all other scripts, just call :global without a value to import the variable.
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)

Who is online

Users browsing this forum: No registered users and 21 guests