Thanks -----Original Message----- From: Public [mailto:public-bounces@talk.mikrotik.com.au] On Behalf Of Andrew Cox Sent: Tuesday, 7 February 2017 1:11 PM To: MikroTik Australia Public List <public@talk.mikrotik.com.au> Subject: Re: [MT-AU Public] scripting Personally I always prefer to prefix commands with a : to ensure they aren't treated as variables but Mike is correct in saying it does work without. However Ben's recommendation is on the money here for the following reason: "A variable declared within a block is accessible only within that block and blocks enclosed by it, and only after the point of declaration." - wiki source <http://wiki.mikrotik.com/wiki/Manual:Scripting#Scopes> *For anyone who's familiar with this - I'd still recommend giving it a quick once over as you may be surprised by the results.* Firstly, the basics: 1. each :local scope variable created at the top of a script will be visible within any child scope inside the script it is defined in, but does not persist after the script has ended and is not visible to any other scripts. 2. A :global variable is saved and can be updated or read by other scripts so long as it is declared in the script before being required. This can be super useful if you're wanting to save certain info for the next time a script runs, take the following example. Script name: "example" :local ltest :set ltest ($ltest + 1) :put "Local test is: $ltest" :global gtest :set gtest ($gtest + 1) :put "Global test is: $gtest" When I run this script in terminal multiple times you see the following results. [admin@MikroTik] > /system script run example Local test is: 1 Global test is: 1 [admin@MikroTik] > /system script run example Local test is: 1 Global test is: 2 [admin@MikroTik] > /system script run example Local test is: 1 Global test is: 3 [admin@MikroTik] > Another example of scoping is shown in the following script: Script name "example2" :local "re-declare-variables" "yes" :local v1 "LOCAL" :global v1 "GLOBAL" :do { :global v2 "SCOPED GLOBAL" } :if ($"re-declare-variables" = "yes") do={ :global v1 :global v2 :put "The next 2 lines will show both global variables" :put $v1 :put $v2 } else={ :put "The next line will show the first variable defined in the root scope only" :put $v1 :put $v2 } Running the script with *re-declare-variables set to yes* will show the 2 global variables, one defined at the root scope of the script, the other defined within a do block. [admin@MikroTik] > /system script run example2 The next 2 lines will show only global variables GLOBAL SCOPED GLOBAL While running the script with *re-declare-variables set to no* will show the local variable but not the global variable defined within the do block, or the global variable defined right after the local at the root scope. [admin@MikroTik] > /system script run example2 The next line will show the first variable defined in the root scope only LOCAL Not all is as it seems though - additional points to note are: 3. The first variable type (local or global) by a given name to be defined within a scope takes precedence, but 4. An alternative variable type with the same name declared inside a child scope will take over. Here's my third and last example, which shows point 2. more clearly. Script name: example3 :local testing1 "Local Test 1" :global testing2 "Global Test 2" :do { :put "This is inside the first do bracket" :put $testing1 :put $testing2 } :do { :global testing1 :local testing2 "Local Test 2" :put "This is inside the second do bracket" :put $testing1 :put $testing2 } Before running this I defined one global variable in terminal: [admin@MikroTik] > :global testing1 "This was set outside of the script" [admin@MikroTik] > /system script run example3 This is inside the first do bracket Local Test 1 Global Test 2 This is inside the second do bracket This was set outside of the script Local Test 2 [admin@MikroTik] > Hopefully this small amount of confusion now will save large amounts of confusion down the track :-) - Andrew On 6 Feb. 2017 7:04 pm, "Alex Samad - Yieldbroker" < Alex.Samad@yieldbroker.com> wrote:
Hi
Okay for completeness
Tried removing the : from global NOP Tried placing a :put in the IF statement - it fired so not the if statements Add the :globals at the top and it started to work.
Which is strange - cause it doesn't match up with the doco ... global should be global for the script ! Any way I have my answer
Thanks Alex
-----Original Message----- From: Public [mailto:public-bounces@talk.mikrotik.com.au] On Behalf Of Mike Everest Sent: Tuesday, 7 February 2017 11:53 AM To: 'MikroTik Australia Public List' <public@talk.mikrotik.com.au> Subject: Re: [MT-AU Public] scripting
Hi Alex,
You don't need ':' for 'global' statement :)
Try pasting into the shell first to check operation.
Cheers, Mike.
-----Original Message----- From: Public [mailto:public-bounces@talk.mikrotik.com.au] On Behalf Of Alex Samad - Yieldbroker Sent: Tuesday, 7 February 2017 11:31 AM To: MikroTik Australia Public List <public@talk.mikrotik.com.au> Subject: [MT-AU Public] scripting
Hi
I have this I a test.rsc file which I scp over and then I do
/import test.rsc
But it doesn't print anything for the variable
# set local hostname :global hostname [/system identity get name]
:if ( $hostname = "artr1" ) \ do={ :global LocalIP 192.168.0.1 :global ManagementIP 1.3.8.72 :global PublicIP 2.7.3.67 :global PrivateIP 1.3.200.10 :global RouterID 1.3.255.10 }
:if ( $hostname = "artr2" ) \ do={ :global LocalIP 192.168.0.2 :global ManagementIP 1.3.8.73 :global PublicIP 2.7.3.68 :global PrivateIP 1.3.200.11 :global RouterID 1.3.255.11 }
:if ( $hostname = "bextrtr1" ) \ do={ :global LocalIP 192.168.0.1 :global ManagementIP 1.3.8.70 :global PublicIP 2.7.3.69 :global PrivateIP 1.3.200.12 :global RouterID 1.3.255.12 }
:if ( $hostname = "bextrtr2" ) \ do={ :global LocalIP 192.168.0.2 :global ManagementIP 1.3.8.71 :global PublicIP 2.7.3.70 :global PrivateIP 1.3.200.13 :global RouterID 1.3.255.13 }
# vrrp dgw :global PublicVRRPIP 2.7.3.71
# # Validate variables # :put "Hostname: $hostname"
:put "Local IP: $LocalIP" :put "Management IP: $ManagementIP" :put "Public IP: $PublicIP" :put "Private IP: $PrivateIP" :put "Routerid: $RouterID"
_______________________________________________ Public mailing list Public@talk.mikrotik.com.au http://talk.mikrotik.com.au/mailman/listinfo/public_talk.mikrotik.com.au
_______________________________________________ Public mailing list Public@talk.mikrotik.com.au http://talk.mikrotik.com.au/mailman/listinfo/public_talk.mikrotik.com.au -- Message protected by MailGuard: e-mail anti-virus, anti-spam and content filtering.http://www.mailguard.com.au/mg Click here to report this message as spam: https://console.mailguard.com.au/ras/1QdrTdsMhO/5Jeen5FubnSBTggFWs6ceh/0.4
_______________________________________________ Public mailing list Public@talk.mikrotik.com.au http://talk.mikrotik.com.au/mailman/listinfo/public_talk.mikrotik.com.au
_______________________________________________ Public mailing list Public@talk.mikrotik.com.au http://talk.mikrotik.com.au/mailman/listinfo/public_talk.mikrotik.com.au