Community discussions

MikroTik App
 
RenzoG
just joined
Topic Author
Posts: 15
Joined: Mon Oct 17, 2016 9:37 pm

Variables in script calls.

Fri Jun 08, 2018 2:00 pm

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.
 
User avatar
dasiu
Trainer
Trainer
Posts: 231
Joined: Fri Jan 30, 2009 11:41 am
Location: Reading, UK
Contact:

Re: Variables in script calls.

Fri Jun 08, 2018 2:22 pm

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]
 
RenzoG
just joined
Topic Author
Posts: 15
Joined: Mon Oct 17, 2016 9:37 pm

Re: Variables in script calls.

Fri Jun 08, 2018 3:09 pm

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.
 
RenzoG
just joined
Topic Author
Posts: 15
Joined: Mon Oct 17, 2016 9:37 pm

Re: Variables in script calls.

Fri Jun 08, 2018 5:54 pm

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.
 
User avatar
dasiu
Trainer
Trainer
Posts: 231
Joined: Fri Jan 30, 2009 11:41 am
Location: Reading, UK
Contact:

Re: Variables in script calls.  [SOLVED]

Sat Jun 09, 2018 1:20 am

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"
 
RenzoG
just joined
Topic Author
Posts: 15
Joined: Mon Oct 17, 2016 9:37 pm

Re: Variables in script calls.

Sat Jun 09, 2018 2:10 pm

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!!!!!!

Who is online

Users browsing this forum: No registered users and 20 guests