IBM i e-Book
A Developer’s Guide to Mastering IBM i Concepts
IBM i Index
Jobs & Logs
Job Switches
The group of eight logical values that can be used to pass information to a submitted job or to exchange information between programs running in the same job are known as Job Switches in IBMi. Logical values indicate only 0’s (false) and 1’s (true) to be specified in the 8-digit character string, but not other character values.
These switches can be set or tested in a CL program and used to control the flow of the program.
How we can pass information to a submitted job via Job Switches?
The Job Switches can be used as an appropriate way to pass information to a submitted job. When a job enters the system, the initial job switches value can be set via the following ways:
- Using the Job Switches Parameter (SWS) of a Submit Job Command
- Job Switch attribute of the JOBD object used to start the job.
Specifying the initial value of the Job Switches of a submitted job via SWS Parameter in a SBMJOB CMD as below:
SBMJOB CMD (CL-Command-To-Run)
SWS (01010010) /* This 8-Byte Character Value ‘01010010’ is the example */
/* for the Job Switches value to pass to the submitted job */
Specifying the initial value of the Job Switches by setting up the JOBD parameter to JOBD object as below:
/* By assuming each job switch indicates whether a specific task among */
/* all the eight tasks need to be done by the submitted job */
CRTJOBD JOBD (DONOTHING)
SWS (00000000)
CRTJOBD JOBD (EVERYTHING)
SWS (11111111)
SBMJOB CMD (CL-Command-To-Run)
JOBD (DONOTHING) /* The Submitted Job here need not do anything */
SBMJOB CMD (CL-Command-To-Run)
JOBD (EVERYTHING) /* The Submitted Job here needs to do everything */
Accessing the Job Switches
By using CL Commands and Built-in-Function, we can access the Job Switches.
With the help of below DSPJOB command, we can check the Job Switch settings of the current interactive job.
DSPJOB OPTION(*DFNA)
The CL Switch Built-in Function (%SWITCH) tests one or more of the eight switches of the current job under an 8-character mask and returns a logical value of ‘0’ or ‘1’.
Syntax of %SWITCH BIF:
%SWITCH(8-Character-Mask)
Only 0, 1 or X can be specified as the valid characters in every position of the mask.
Following are the list of indications of the above valid characters for %SWITCH BIF.
- ‘0’ – The corresponding job switch is to be tested for a 0 (off).
- ‘1’ – The corresponding job switch is to be tested for a 1 (on).
- ‘X’ – The corresponding job switch is not to be tested (X). The value in the switch does not affect the result of %SWITCH.
Simple Job Switch Example in CL
IF COND(%SWITCH(0XX1XXXX) THEN (GOTO HOME)
From the above example, we can understand that if the Job Switch# 1 contains ‘0’, and Job Switch# 4 contains ‘1’, then the program will branch to label HOME. The remaining Job Switches 2, 3, 5, 6, 7, and 8 will not be tested.
Other CL Commands used to get or set the Job Switches
With the help of RTVJOBA and CHGJOB command, we can get or set one or more Job Switches of the current job or another job, respectively.
CHGJOB SWS(XXX1XXXX)
The above CL command changes the value of the fourth Job Switch value of the current job to ‘1’.
Accessing the Job Switches via RPG PGM
OPM RPG & ILE RPG supports Job Switches via external indicators (U1 through U8). At the beginning of the RPG Program cycle, the current Job Switches settings are copied to indicators U1-U8 and at the end of the program cycle, the indicator values of U1-U8 are copied back to the Job Switches.
Also, noting that the returning from an RPG Program with the LR indicator set off will not get the Job Switches settings of the updated current job.
RPG PGM Example on how Job Switches settings are updated via external indicators:
D SET_OFF S N INZ(*OFF)
D SET_ON S N INZ(*ON)
/Free
*INU1 = SET_OFF;
*INU2 = SET_ON;
*INLR = *ON;
/End-Free
Note:
Although, the RPG compiler will stop assigning constant values other than ‘0’ or ‘1’ (*ON or *OFF) to an indicator variable, it is still possible to change the contents of external indicators with non-logical values. Since, assigning a character variable to an indicator variable is allowed in RPG.
Hence, to modify a Job Switch value in an ILE RPG PGM via a stand-alone variable, use an Indicator Type (N) character variable instead of a type A character variable to avoid the non-logical values assigning issue.