Community discussions

MikroTik App
 
baumie
newbie
Topic Author
Posts: 41
Joined: Mon Mar 19, 2007 7:51 pm
Location: South Africa

parameters for scripting

Sat Apr 05, 2008 2:22 pm

Hi
I want to know if there's a way of passing parameters to a MT script. For instance I want to change the rate-limits for centain user groups can I write one script and get the limits from parameters passed to the script or must I do a script for each rate limit and group
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7056
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: parameters for scripting

Mon Apr 07, 2008 9:33 am

You can use global variables, for example,

simple script "myScript":
{
:global myVar;
:put &myVar;
}

Before calling "myScript" set "myVar" value:

{
:global myVar 999;
/system script run myScript;
}
 
baumie
newbie
Topic Author
Posts: 41
Joined: Mon Mar 19, 2007 7:51 pm
Location: South Africa

Re: parameters for scripting

Thu Apr 10, 2008 9:38 am

Thanks I never thought of that but it will solve my problem
 
User avatar
Etz
Member Candidate
Member Candidate
Posts: 178
Joined: Thu Mar 27, 2014 10:09 am
Location: Estonia

Re: parameters for scripting

Wed Feb 13, 2019 12:13 am

Sorry for necro posting, but how do you pass multiple parameters to script?

Would this work?:
{
:global myVar1 999;
:global myVar2 9999;
/system script run myScript;
}
Considering that MyScript has global scope variables MyVar1 and MyVar2 present?
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3300
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: parameters for scripting

Wed Feb 13, 2019 4:25 pm

Should work as long as you declare your global variables in the sub script like this:
:global myVar1
:global myVar2
PS, you do not need ut use ; at the end of the line, only to separate multiple commands at the same line.
So your script could be written:
{
:global myVar1 999
:global myVar2 9999
/system script run myScript
}
 
A9691
newbie
Posts: 25
Joined: Sat May 14, 2016 10:58 am

Re: parameters for scripting

Thu Sep 12, 2019 4:24 pm

You can pass parameters to script files run with /import. For example:
Content of fact.rsc:
:if ($1 > 1) do={
	:global fact
	:set $result ([$fact ( $1 - 1 )]*($1))
} else={
	:set $result 1
}
Define fact function:
:global fact do={/import "fact.rsc"; return $result}

Testing:
[admin@bkeniko] > :put [$fact 3]

Script file loaded and executed successfully

Script file loaded and executed successfully

Script file loaded and executed successfully
6
[admin@bkeniko] > 

Is there any documentation on this?
(tested on v6.40.8 )
 
User avatar
SiB
Forum Guru
Forum Guru
Posts: 1888
Joined: Sun Jan 06, 2013 11:19 pm
Location: Poland

Re: parameters for scripting

Fri Sep 13, 2019 12:40 pm

Is there any documentation on this? (tested on v6.40.8 )
.
I think it's new, I cannot see about passing variable by global function.to /import feature. This is like recursion . Works in 6.45.3
 
A9691
newbie
Posts: 25
Joined: Sat May 14, 2016 10:58 am

Re: parameters for scripting - update

Tue Oct 10, 2023 3:47 pm

When the /import command is used within a function or script, the parameters available in the function will be available in the script file.
Just don't declare them as :local or :global.

For example:
[a@mk] > :global myfunc do={ /import myfunc.rsc }
myfunc.rsc:
:put "passed parameters: $1 $2 $3"
Try it:
[a@mk] > $myfunc A B C
passed parameters: A B C

Script file loaded and executed successfully
[a@mk] > 
Define an $import function and use that instead of /import and parameter passing to a script file is solved. Named parameters are also supported.
:global import do={ :global result; :set result; /import "$1.rsc"; :local r $result; :set result; :return $r}
The global variable $result is used keep the the return value of the script file until is returned by the $import function.
Has to be cleared before and after invoking the script to avoid "unexplainable" errors.

The only disturbing things are those "Script file loaded and executed successfully" messages. They appear and you don't know which file was loaded.

Who is online

Users browsing this forum: No registered users and 8 guests