Referencing local variables in MC script if/else/foreach constructions
search cancel

Referencing local variables in MC script if/else/foreach constructions

book

Article ID: 388603

calendar_today

Updated On:

Products

Management Center - VA

Issue/Introduction

In general local variables can be referenced in the following ways in MC scripts:

${@get-local localVariableName}

 Or 

${locals.localVariableName}

However if the 1st syntax is used while referencing a local variable in if/else/foreach construction script execution may fail. 

For example the following script:

${@set-local flag=True} 
${@set-local icap_object = "icap.testcas01" }

!- Start of Script
health-check
${if locals.flag=False}
disable sick ${@get-local icap_object}
${else}
enable ${@get-local icap_object}
${end}
exit

Will fail to execute with the following results:

TestProxy01#(config)!- Start of Script
TestProxy01#(config)health-check
TestProxy01#(config health-check)"icap.testcas01"  
                                        ^
% Invalid input detected at '^' marker.
TestProxy01#(config health-check)enable "icap.testcas01"
  ok
TestProxy01#(config health-check)
TestProxy01#(config health-check)exit

As you can see, local variable reference is getting substituted with a value from disable sick ${@get-local icap_object} even though if condition above resolves to False.

Environment

MC script with a local variables and if/else/foreach constructions

Resolution

${locals.localVariableName} variable referring syntax must be used in logical expressions in MC scripts. 

Script example above needs to be adjusted as follows:

${@set-local flag=True} 
${@set-local icap_object = "icap.testcas01" }

!- Start of Script
health-check
${if locals.flag=False}
disable sick ${locals.icap_object}
${else}
enable ${locals.icap_object}
${end}
exit