Community discussions

 
bokad
Member Candidate
Member Candidate
Topic Author
Posts: 152
Joined: Tue Oct 18, 2005 7:34 pm

How to pass variable between scripts

Tue Jan 27, 2009 6:34 pm

Is there any way to pass variables between scripts?
It seems that I can not access variables that are declared global in one script from another script.

Ex.
In script 1:
:global 64kdownload 64000

On the command line:
:environment print
shows: ""64kdownload"=64000

In script 3:
/queue type set "user-download" pcq-rate=$64kdownload

Script three will not run properly. Any ideas?
 
User avatar
Letni
Member
Member
Posts: 375
Joined: Tue Dec 05, 2006 5:16 am
Location: South Carolina

Re: How to pass variable between scripts

Wed Jan 28, 2009 3:17 am

 
SurferTim
Forum Guru
Forum Guru
Posts: 4637
Joined: Mon Jan 07, 2008 10:31 pm
Location: Miramar Beach, Florida

Re: How to pass variable between scripts

Wed Jan 28, 2009 10:39 am

In the scripts that use global variables, but not change them, use this:
:global 64kdownload
/queue type set "user-download" pcq-rate=$64kdownload
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6283
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: How to pass variable between scripts

Wed Jan 28, 2009 3:21 pm

to use global variable you have to declare it - if it is global and already have value - you will be able then access that value.

also note, that script should be in {} signs, or each line will count as separate script.
 
SurferTim
Forum Guru
Forum Guru
Posts: 4637
Joined: Mon Jan 07, 2008 10:31 pm
Location: Miramar Beach, Florida

Re: How to pass variable between scripts

Wed Jan 28, 2009 4:13 pm

Pardon me, but I am not sure I understand. What do you mean by "separate script"? I know I use <?php ?> for php, but I saw nothing of the brackets in the examples. I use none in mine unless to group stuff. If I am doing something that may cause me challenges, I would like to know. Maybe a reference or wiki?

BTW I do not use the commands unless assigning them or getting a return value. So it would always be
:local returnval [/command]
I do not use
/command
anywhere.
 
WojtusW5
Frequent Visitor
Frequent Visitor
Posts: 64
Joined: Mon Oct 02, 2017 1:25 pm

Re: How to pass variable between scripts

Thu Nov 29, 2018 4:38 pm

Hello, I have similar problem.
I have 2 script
name="test1" source=:global test "12345"; 
name="test2" source=:put $test; 
And I can't display global variable from script

[admin@test] /system script> run test1
[admin@test] /system script> environment print
# NAME VALUE
0 test 12345
[admin@test] /system script> run test2

[admin@test] /system script>
I invite you to visit my blog
https://mikrotikon.pl/
 
User avatar
eworm
Member
Member
Posts: 391
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: How to pass variable between scripts

Fri Nov 30, 2018 1:08 am

Hello, I have similar problem.
I have 2 script
name="test1" source=:global test "12345"; 
name="test2" source=:put $test; 
And I can't display global variable from script

[admin@test] /system script> run test1
[admin@test] /system script> environment print
# NAME VALUE
0 test 12345
[admin@test] /system script> run test2

[admin@test] /system script>
You have to declare it:
name="test1" source=:global test "12345"; 
name="test2" source=:global test; :put $test;
Manage RouterOS scripts and extend your devices' functionality: RouterOS Scripts
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 1302
Joined: Sat Dec 24, 2016 11:17 am
Location: jo.overland at gmail.com

Re: How to pass variable between scripts

Fri Nov 30, 2018 8:12 am

To use variable from other script just re-declare the variable without initializing it.

script1
:global previousIP "1.2.3.4"
:put $previousIP
will display 1.2.3.4

script2
:global previousIP
:put $previousIP
will display 1.2.3.4
 
How to use Splunk to monitor your MikroTik Router

MikroTik->Splunk
 
 
User avatar
sebastia
Forum Guru
Forum Guru
Posts: 1776
Joined: Tue Oct 12, 2010 3:23 am
Location: Antwerp, BE

Re: How to pass variable between scripts

Fri Nov 30, 2018 9:23 pm

I pass values by using named variables, there is no need for globals here:
ex:
:local rtt [$getRTT target=$target intf=$intfP count=$pcount timeout=$ptimeout]

:global getRTT do={
#:log info "getRTT: target: $target, intf: $intf, count: $count, timeout: $timeout"
...
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 1302
Joined: Sat Dec 24, 2016 11:17 am
Location: jo.overland at gmail.com

Re: How to pass variable between scripts

Fri Nov 30, 2018 10:03 pm

If you read the title
How to pass variable between scripts
You see that global is needed or you can write data to a file.
 
How to use Splunk to monitor your MikroTik Router

MikroTik->Splunk
 
 
User avatar
sebastia
Forum Guru
Forum Guru
Posts: 1776
Joined: Tue Oct 12, 2010 3:23 am
Location: Antwerp, BE

Re: How to pass variable between scripts

Sat Dec 01, 2018 9:33 pm

Actually I did see it, but ... if the scripts are related say script1 call on script2, named variables can be used.
If one can't call on the other then that won't fly naturally.

In other way, another option for a developer

Who is online

Users browsing this forum: No registered users and 14 guests