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

Use the :SWITCH script statement to run different script lines based on the value of a variable. The variable represents a condition, and the :SWITCH block contains one or more :CASE statements. The case determines which statements to run when the variable has a particular value. Define cases for each value of the variable for which to process different statements.

Tip: Use :IF... :ELSE... :ENDIF statements to evaluate expressions when you have a single condition.

Important! You can use the predefined variable &$PLATFORM# with this script statement. If you use this predefined variable in a generic Job object, you must also define the &AGENT# variable in your script. The generic Job object does not have a defined host when you create it, so the script returns an error if you do not specify the host. For more information, see Predefined Variables.

Syntax

:SWITCH Variable
:CASE Value1
[Statements]
[:CASE Value2
[Statements]
...
]

[:OTHER
[Statements]
...
]

:ENDSWITCH

Parameters

  • :SWITCH
    Defines a block of statements that run in different cases

  • Variable
    Name of the variable to verify to determine which case applies
    Format: script literal, script variable or script function

  • Statements
    (Optional) One or more statements to process when the case applies
    Format: script statement

  • :CASE
    Starts the block of statements to run when the variable has the value defined for the case
    Note: If you are using complex expressions and want to connect multiple :CASE statements with an OR operator, write the :CASE statements in sequence, without using :OTHER statements between them.

  • Value1, Value2
    Conditions or values of the variable that determine which case applies
    Format: script literal, script variable or complex expression

  • :OTHER
    (Optional) Starts a block of statements to run when none of the defined cases applies

    Notes

    • Use the :OTHER statement once after all :CASE statements.
    • :OTHER cannot be used on the line directly following a :CASE line. You must insert a blank line in between.
  • :ENDSWITCH
    Ends the :SWITCH block

Notes:

  • The value for a case can be an arithmetic expression.

    Example

    :SWITCH &NUM#
    :CASE 3*(5-2)
    : PRINT "&NUM# = 9"
    :ENDSWITCH

  • You can use the combination "between Value1 and Value2" and/or operators (<, >, <>, <=, >=, =) to define complex expressions and ranges of values for :CASE lines. Specify the string "Y" instead of the variable name in this case.

    Example

    :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 retrieves the current day of the week as a number. The script uses day of week number to conditionally start a specific Job on Mondays and Fridays only.

: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 following script in a generic Job object defines conditions based on the platform that the Job is running on. See also that a blank line is

: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: