How to call script from script ? Documentation state this is possible and allowed but I just can’t find syntax.
any example ?
Thank you in advance,
Dejan
How to call script from script ? Documentation state this is possible and allowed but I just can’t find syntax.
any example ?
Thank you in advance,
Dejan
same way as you do from CLI
from cli:
/system script run 0
or
this is already in script:
{/system script run 0}
or you can write multi-line script right there on CLI
tnx, additional question :
my idea was to call same script for different interfaces using :
:global theInterface
:set theInterface “wan1”
/system script run setInterface
:set theInterface “wan2”
/system script run setInterface
:set theInterface “wan3”
/system script run setInterface
where script setInterface using variable theInterface but this not working. In some article have read already that script can’t receive parameter. Finally, question is :
is there way to simulate parameter or some variable set in order to avoid lot’s of redundant code ?
thank you in advance,
Dejan
Functions in RouterOS CMD scripts:
Forum discussion:
http://forum.mikrotik.com/t/functions-in-cmd-scripts/34902/1
Wiki article:
http://wiki.mikrotik.com/wiki/Use_Functions_in_CMD_Script
I use the execute command. But be aware that the scripts run concurrently, so if you need it to work like a subroutine, you need something like this:
:global done
:set done false
:execute mysub
:while (!$done) do={:nothing}
The mysub code should look like this:
:global done
# first command
# second command
# and when complete
:set done true
If you see how “done” is used, you can see how to pass parameters to the subroutine.
EDIT: My bad! I forgot the “$” on done.
I miss to declare parameter in second script, tough that this declaration would override previously set value.
This is working now, thank you all
As long as you do not assign a value to “done” in the first line of the subroutine, it becomes just a declaration, not a definition. If you used “:global done false”, then that would overwrite the original value.
just to clarifi one detail:
In your example you have piece of code :
:while (!$done) do={:nothing}
what for is this ? Will new script open another thread and both will keep running in time ?
In this case, and because of use of global variables I will need additional piece of code, just like in your example. In case this is needed : will that code use CPU ? Is there some kind of sleep ?
Both scripts run at the same time. The main script does not pause until the subroutine finishes. It keeps right on running. The way I figure it is the OS runs one line of code from the main routine, then one line from the subroutine, then another from the main, then another from the subroutine, and on and on…
In your example you have piece of code :
:while (!$done) do={:nothing}
This code does exactly what it says. “While not done, do nothing”. This is the line of code the parent routine runs while the child routine is processing. It works almost exactly like the old Unix “fork” command. The parent must wait for the parallel child process to finish before all the data will be ready. The parent routine will break out of that loop when the child process does the “:set done true” instruction.
This type of code is not very CPU friendly. It does “waste” the CPU time spent on the parent process in the “do nothing” loop. I use that time before the loop in most cases to perform other tasks. Then when it is time to retrieve the new data processed by the subroutine, then I start the “do nothing” loop.
You only need that loop if your main script will need data generated by the subroutine later in the code. If no common data will be processed, you can omit that loop.
I don’t need data generated by subroutine but I am using global variable in each one and changing this global variable in order to set data for next execution. That for, I need to stop and wait, and, because this is supposed to be executed each one minute, maybe this is not good idea to use soubrutine call, I would just made coupe scripts instead of two.
Thank you anyway
Dejan
Normally, it is fastest to run all the code in one script. But if you decide to use the execute function as a “call”, and you use any global variables between “calls”, or in any subsequent “calls” (read or write), you need the “do nothing” loop at each “call”. After a little experience with this, it isn’t difficult at all to decide when to use the loop!
I just want to say maybe :delay command instead of :nothing will use less CPU. It works for me…