Global variables can be set in any macro and retrieved without being explicitly passed in another macro. The global variable is set with the following syntax:
env.setVar(name, value)
The name is a quoted string and the value can be a character string or number.
The global variable is retrieved with the following syntax:
value := env.getVar(name).
The name is a quoted string and the value is the value stored by an earlier env.setVar.
A macro can contain one onError macro. The onError macro is like any other macro file template. There can only be one macro with the name onError in the macro file.
In this example, the macro sets a global variable before using it in a configuration command:
<# badInt #>
<# interface := "9/0" #>
<# env.setVar("interface",interface) #>
config t
interface fast <# interface; '\n' #>
ip add 7.6.5.4 255.255.255.0
<#endtmpl#>
<# onError #>
<# c := env.getVar("interface") #>
<# setoutput console #>
<# "begin output\n" #>
<# "The interface value: ";c; "\n" #>
<# endsetoutput #>
end
<#endtmpl#>
When the macro runs, the global variable interface is set and the interface command contains an invalid interface value. The CLI reports a syntax error and the macro onError is called. Within the onError macro, the global variable interface is retrieved.
ERX-40-4a-cc#macro b.mac badInt
Macro 'badInt' in file 'b.mac' starting execution (Id: 18)
Enter configuration commands, one per line. End with ^Z.
The interface command contains a bad interface value.
ERX-40-4a-cc(config)#interface fast 9/0
^
% interface not found
The CLI reports a syntax error and the macro onError is called. Within the onError macro, the global variable interface is retrieved.
The interface value: 9/0
The macro terminates.
Macro 'badInt' in file 'b.mac' ending execution (Id: 18)