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
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;
}
Thanks I never thought of that but it will solve my problem
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?
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
}
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 )
.
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
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.