Generate script from another script

Is it possible to generate a script from a script with variable substitution, something like this:


:global myVar1 "ABC"
:global myVar2 "DEF"

/system script
add dont-require-permissions=no name=myScript owner=admin policy=\
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source="#\
    \n:global scriptVar1 $myVar1\r\
    \n:global scriptVar2 $myVar2\r\
    \n\r\
    \n}"

Instead of the generated script containing $myVar1, I want it to contain “ABC”

Sure it does. In this example you should add quotes I guess…

Yes it does when working with variables. String arrays and key, value arrays are another story. Ultimately I was able to solve the problem. Here’s how:

# Previously defined/assigned string array
:global FOinterfaces

# Previously defined/assigned key, value array
:global FOpingHosts

#Convert the string array to a string variable, need to insert the quotes
:global FOSinterfaces ""
:foreach i in=$FOinterfaces do={
	:if ([:len $FOSinterfaces] = 0) do={ :set FOSinterfaces ("\"".$i."\"") } else={ :set FOSinterfaces ($FOSinterfaces.";\"".$i."\"") }
}

#Convert the key, value array to a string. Need to quote the value
:global FOSpingHosts ""
:foreach k,v in=$FOpingHosts do={
	:if ([:len $FOSpingHosts] = 0) do={ :set FOSpingHosts ($k."=\"".$v."\"") } else={ :set FOSpingHosts ($FOSpingHosts.";".$k."=\"".$v."\"") }
}

/system script
add dont-require-permissions=no name=success owner=admin policy=\
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source="
    \n:global FLinterfaces {$FOSinterfaces}\r\
    \n:global FLpingHosts {$FOSpingHosts}\r\
    ...............

Here is the generated script:

:global FLinterfaces {"vlan20";"ether2";"lte1"}
:global FLpingHosts {ether2="208.67.220.220";lte1="64.6.64.6";vlan20="1.0.0.1"}