Community discussions

MikroTik App
 
User avatar
Sertik
Member
Member
Topic Author
Posts: 420
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Passing a parameter from a script to another script

Mon Dec 04, 2023 11:10 am

Hello everybody! This is not GPT! My question is: I want to pass a parameter from one script to another. What's easier, you say, do it through global variables. But I want to do without variables (the interest is purely academic)

I managed to do it using this method:

script Transfer:
:local scrName "scriptReceiver"
:local Var1 "Par1=mytext"
:local Var2 "Par2=12"

:execute script="[[:parse \"[:parse [/system script get $scrName source]] $Var1 $Var2\"]]"
script Receiver:
:log warning "$Par1 $Par2"
:log warning [:typeof $Par1]
:log warning [:typeof $Par2]
:local Par3 [:tonum $Par2]
:log error $Par3
:log error [:typeof $Par3]
In the design :execute script="[[:parse \"[:parse [/system script get $scrName source]] $Var1 $Var2\"]]"
:execute is clearly redundant, it is enough [[:parse \"[:parse [/system script get $scrName source]] $Var1 $Var2\"]]

From my experience, it can be seen that

1. all parameters are passed as string parameters and after catching them, the receiver script must convert their type back, knowing it in advance.
2. And secondly, strings can only be transmitted in Latin. I was unable to pass string parameters in the national language (say Cyrillic).

Questions:

1. Is there any way to do this more gracefully? (without global variables and functions).
2. And is it possible to somehow pass parameters with their types (not only in string form) and pass the Cyrillic alphabet? I tried to transfer Cyrillic alphabet in HEX format like \D0\E1, etc... - it doesn't work.
 
User avatar
Sertik
Member
Member
Topic Author
Posts: 420
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Passing a parameter from a script to another script

Mon Dec 04, 2023 12:43 pm

Rex and Amm0 where are you friends?
 
User avatar
Sertik
Member
Member
Topic Author
Posts: 420
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Passing a parameter from a script to another script

Mon Dec 04, 2023 1:41 pm

Here, Russian friends gave me the idea that I was bothering a lot and that I could pass positional parameters like $0, $1, etc. from the script to the script ... with the same construction. We should try it too.

and catching them in script Receiver is just like :local Par1 $0
 
User avatar
Sertik
Member
Member
Topic Author
Posts: 420
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Passing a parameter from a script to another script

Mon Dec 04, 2023 1:58 pm

Да, можно передавать и именованные и позиционные, но всё равно все превращаются в тип "str"
Yes, you can pass both named and positional ones, but still everything turns into the "str" type:

scrTransfer:
:local scrName "scriptReceiver"
:local Var1 "12"
:local Var2 "Par2=13"
:local Var3 14

:execute script="[[:parse \"[:parse [/system script get $scrName source]] $Var1 $Var2 $Var3\"]]"

scr Receiver:
:log info $0
:log info [:typeof $0]

:log info $1
:log info [:typeof $1]

:log info $Par2
 
User avatar
Sertik
Member
Member
Topic Author
Posts: 420
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Passing a parameter from a script to another script

Mon Dec 04, 2023 9:16 pm

It turns out that you can pass parameters from a script to a script while preserving their type. This is done like this:
# Script transfer

:local scrName "scriptReceiver"
:local Var1 "Par1=text"
:local Var2 "Par2=12"
:local Var3 14
:local Var4 192.168.0.1
:local Var5 [/system clock get time]

:log info ("$Var1"." $[:typeof $Var1]")
:log info ("$Var2"." $[:typeof $Var2]")
:log info ("$Var3"." $[:typeof $Var3]")
:log info ("$Var4"." $[:typeof $Var4]")
:log info ("$Var5"." $[:typeof $Var5]")

[[:parse "[:parse [/system script get $scrName source]] $Var1 $Var2 $Var3 [:to$[:typeof $Var4] $Var4] [:totime $Var5]"]]
# Script Receiver

:log warning ("$Par1"." $[:typeof $Par1]")
:log warning ("$Par2"." $[:typeof $Par2]")

:local Par3 $0
:local Par4 $1
:local Par5 $2
:log error ("$Par3"." $[:typeof $Par3]")
:log error ("$Par4"." $[:typeof $Par4]")
:log error ("$Par5"." $[:typeof $Par5]")
Place both scripts in your repository, run the first one and see what happens in the log
In my example, the parameters $Var1, $Var2 and $Var3 "lose" their type when they are all converted to type "str", while the parameters $Var4 and $Var5 retain their type when passed


Pay attention to the line
[[:parse "[:parse [/system script get $scrName source]] $Var1 $Var2 $Var3 [:to$[:typeof $Var4] $Var4] [:totime $Var5]"]]
and in it the entry [:totime $Var5]. To pass $Var5 as a "time" type, you need to explicitly specify this again for :parse, although $Var5 is already of the "time" type.

and the design for :parse

[:to$[:typeof $Var4] $Var4]

substitutes the type of the Var4 variable on the fly into the collected string.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11929
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Passing a parameter from a script to another script

Tue Dec 05, 2023 5:23 pm

Rex and Amm0 where are you friends?

What's easier, you say, do it through global variables. But I want to do without variables (the interest is purely academic)

I have no interest in complicating life unnecessarily.
If there is a quicker way, I don't see why waste time with all these mental wanks...
 
User avatar
Sertik
Member
Member
Topic Author
Posts: 420
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Passing a parameter from a script to another script

Tue Dec 05, 2023 9:17 pm

I didn't expect this from you. It always seemed to me that you love mental exercises. Regarding my question, do you have any ideas or solutions?
 
User avatar
Sertik
Member
Member
Topic Author
Posts: 420
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Passing a parameter from a script to another script

Tue Dec 05, 2023 9:21 pm

So far I have come to this final working design:

[[:parse "[:parse [/system script get $scrName source]] Par1=[:to$[:typeof $Var1] $Var1] [:to$[:typeof $Var2] $Var2] [:to$[:typeof $Var3] $Var3] [:to$[:typeof $Var4] $Var4] [:to$[:typeof $VarN] $VarN]"]]

Now I can pass parameters without losing their types. But I can’t pass the Cyrillic alphabet in string parameters. Do you have any ideas about this? I have not tried to transfer arrays.
 
User avatar
Sertik
Member
Member
Topic Author
Posts: 420
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Passing a parameter from a script to another script

Wed Dec 06, 2023 9:54 am

Thank you, friends. I guessed it myself. It's just that string parameters need additional escaping like "\" hello мамочка!\"" and then they are transmitted without problems !

Who is online

Users browsing this forum: No registered users and 3 guests