Page 1 of 1

Variables in script calls.

Posted: Fri Jun 08, 2018 2:00 pm
by RenzoG
Hi everybody. I'm trying to call script B from different scripts, in the same device of course. But variables declared in B are empty when used in calling scripts.
Variables that I need are declared as follows (I tried to change "local" in "global", with same result):

:local myDate value=[/system clock get date];
:local myTime value=[/system clock get time];
:local myDay [:pick $myDate 4 6];
:local myMonth [:pick $myDate 0 3];
:local myYear [:pick $myDate 7 11];
:local myHour [:pick $myTime 0 2];
:local myMinute [:pick $myTime 3 5];
:local itaTime ($myDay."/".$myMonth."/".$myYear."-".$myHour.":".$myMinute);

Well, when I call this script from other ones, everything works fine (script are correctly run, checking out "run count"), but simply variables declared in script B are empty at the end of calling scripts...
Anyone cam help me, please? Thank you.

Re: Variables in script calls.

Posted: Fri Jun 08, 2018 2:22 pm
by dasiu
Hi!

1. If the values are local, it's normal they won't work - as the script B has different "local" variables than the calling script.
2. With global values it should work. But make sure, that in the beginning of BOTH script B and calling script you declare :global xxx. And maybe use first :global xxx without the value, and then :set $xxx [value function]
3. If it's only one value you need (the last one, itaTime) - you can think of making it a function, not a "script B":
:global itaTime do={
  :local myDate value=[/system clock get date];
  :local myTime value=[/system clock get time];
  :local myDay [:pick $myDate 4 6];
  :local myMonth [:pick $myDate 0 3];
  :local myYear [:pick $myDate 7 11];
  :local myHour [:pick $myTime 0 2];
  :local myMinute [:pick $myTime 3 5];
  :return ($myDay."/".$myMonth."/".$myYear."-".$myHour.":".$myMinute);
}
Then just do:
:put [$itaTime]

Re: Variables in script calls.

Posted: Fri Jun 08, 2018 3:09 pm
by RenzoG
Many thanks Dasiu. I'm reading Wikipedia RouterOS Funcion. It's very interesting and I have to thank you a lot for your precious infos.
Many thanks again.
I'm going to try.

Re: Variables in script calls.

Posted: Fri Jun 08, 2018 5:54 pm
by RenzoG
Well, I must be a nerd! I can't make it work. Please take a look at what I have done:
Script Ita_Time (Called Script):

:global itaTime do={
:local myDate value=[/system clock get date];
:local myTime value=[/system clock get time];
:local myDay [:pick $myDate 4 6];
:local myMonth [:pick $myDate 0 3];
:local myYear [:pick $myDate 7 11];
:local myHour [:pick $myTime 0 2];
:local myMinute [:pick $myTime 3 5];
:return ($myDay."/".$myMonth."/".$myYear."-".$myHour.":".$myMinute);
}
:put [$itaTime];

Calling Scripts:
.........
.........
.........
/system script run [/system script find name="Ita_Time"];

.............
.............
# variable $itaTime is just EMPTY;


Where am I going wrong?
Thanks.

Re: Variables in script calls.  [SOLVED]

Posted: Sat Jun 09, 2018 1:20 am
by dasiu
Nooo :).
1. Create this itaTime script with the definition I sent, the few line :global itaTime do={ ... }
2. Make a scheduler to run this script every startup
3. In your other script in the beginning include the line:
:global itaTime
4. Inside the script just use the [$itaTime] "variable" (which is a function). For example:
:global itaTime
:log info "The time now is $[$itaTime]"
- that's it!
(please, remember, first to declare it in the beginning of each script using it, with just one ":global itaTime" line, without anything else, and then using it inside [] - as it's like running the command)

You can also do (in your new script):
:global itaTime
:local currentItaTime [$itaTime]
:log info "The time now is $currentItaTime"

Re: Variables in script calls.

Posted: Sat Jun 09, 2018 2:10 pm
by RenzoG
Grrrrrreat! It works. I needed to return multiple values, not only itaTime. So, in called script, I created:

:global myDay do={
:return [:pick [/system clock get date] 4 6];
}

:global myMonth do={
:return [:pick [/system clock get date] 0 3];
}

:global myYear do={
:return [:pick [/system clock get date] 7 11];
}

:global myHour do={
:return [:pick [/system clock get time] 0 2];
}

:global myMinute do={
:return [:pick [/system clock get time] 3 5];
}

:global itaTime do={
:global myDay;
:global myMonth;
:global myYear;
:global myHour;
:global myMinute;
:return ([$myDay]."/".[$myMonth]."/".[$myYear]."-".[$myHour].":".[$myMinute]);
}

Then, in calliing scripts, after declaring all of them (myDay, myMonth, myYear etc.), I have them all available.

Thank you very much, Dasiu!!!!!!