:SWITCH ... :CASE ... :OTHER ... :ENDSWITCH

Use the :SWITCH script statement to execute specific script blocks based on the value of a variable. The evaluated variable acts as the condition, while the :SWITCH block contains one or more :CASE statements that define the potential matching values. You must define separate cases for each distinct variable value that requires a different processing path.

Tip: If you only need to evaluate a single condition, use :IF... :ELSE... :ENDIF statements instead.

Important! You can use the predefined &$PLATFORM# variable with this statement. However, if you use this predefined variable within a generic Job object, you must also explicitly define the &AGENT# variable in your script. Because generic Job objects lack a defined host upon creation, the script will return an error if you fail to specify the host. For more information, see Predefined Variables.

Syntax

:SWITCH Variable

:CASE Value1

  [Statements]

[:CASE Value2

  [Statements]

...]

[:OTHER

  [Statements]

...]

:ENDSWITCH

Parameters

Parameter Description Format
:SWITCH Initiates the block of conditional statements. Keyword
Variable Specifies the name of the variable evaluated to determine the applicable case. Script literal, script variable, or script function
Statements (Optional) Contains one or more script statements to execute when the condition is met. Script statement
:CASE

Designates the start of a statement block corresponding to a specific variable value.

Note: When dealing with complex expressions, if you want to connect multiple :CASE statements using a logical OR operator, write them sequentially without any intervening :OTHER statements.

Keyword
Value1, Value2 Defines the specific conditions or values that trigger the corresponding case block. Script literal, script variable, or complex expression
:OTHER

(Optional) Provides a fallback block of statements that execute if none of the preceding defined cases are met.

Notes:

  • You must place the :OTHER statement only once, located after all :CASE statements.
  • You cannot position :OTHER on the line immediately following a :CASE line; you must insert a blank line between them.
Keyword
:ENDSWITCH Terminates the entire :SWITCH block. Keyword

Evaluating Expressions

You can use arithmetic operations directly as the target value for a :CASE line:

:SWITCH &NUM#

:CASE 3*(5-2)

:  PRINT "&NUM# = 9"

:ENDSWITCH

For more complex logic involving value ranges or operators (<, >, <>, <=, >=, =), use the structure "between Value1 and Value2" within the :CASE lines. In these scenarios, use the string "Y" for the :SWITCH variable parameter.

:SET &STATUS# = GET_STATISTIC_DETAIL(&RUNID#, STATUS)

:SWITCH "Y"

:CASE &STATUS# between 1300 and 1599

:CASE &STATUS# between 1700 and 1799

:  PRINT "The task &RUNID# is active."

:CASE &STATUS# between 1600 and 1699

:  PRINT "The task &RUNID# is in a waiting condition."

:CASE &STATUS# between 1800 and 1899

:  PRINT "The task &RUNID# has aborted."

:CASE &STATUS# >= 1900

:  PRINT "The task &RUNID# has successfully ended."

:ENDSWITCH

Examples

The following example determines the current day of the week numerically and uses that number to execute distinct Jobs exclusively on Mondays and Fridays. If neither day matches, it prints a message.

:SET &DATE# = SYS_DATE_PHYSICAL("DD.MM.YYYY")

:SET &WEEKDAY# = WEEKDAY_NR("DD.MM.YYYY:&DATE#")

 

:SWITCH &WEEKDAY#

:CASE 1

:  SET &ACT# = ACTIVATE_UC_OBJECT(JOBS.MONDAY, WAIT)

:CASE 5

:  SET &ACT# = ACTIVATE_UC_OBJECT(JOBS.FRIDAY, WAIT)

:OTHER

:  PRINT "No Processing."

:ENDSWITCH

The next script configures conditions within a generic Job object based on its operating platform. Notice the mandatory blank line placed before the :OTHER statement.

:SWITCH &$PLATFORM#

:CASE "WINDOWS"

!  insert these lines in your script to determine if an error occurred

!  @set retcode=%errorlevel%

!  @if NOT %ERRORLEVEL% == 0 goto :retcode

!

:CASE "UNIX"

 

:OTHER

:ENDSWITCH

See also: