Sie sind auf Seite 1von 26

Mainframe Automation

Using REXX
Agenda


Mainframe Automation

What is REXX?

REXX - The Basics

Input/Output Statements

Conditional Processing

Iterative Loops

String Parsing

Built-in Functions

Interacting with subsystems

Trapping command Output

Handling data sets

System Variables

REXX - Performance Perspective

Limitations of REXX

Next Steps - Extensions to REXX

Questions

References
Mainframe Automation

 What can you automate in Mainframes?


 Daily Monitoring activities

House Keeping activities
 Creating Test Data
 Process Automation
 Almost every manual activity

 How can you do that?


 REXX
 Clists
 ISPF Macros
 Emulator Macros
What is REXX?

 REXX is REstructured eXtended eXecutor


 A full featured general purpose programming language
 Offers versatile programming options
 Suitable for writing command procedures, application front ends, user defined macros
 Available across Platforms. Can run on mainframe servers, UNIX servers and even on PCs.
 Interpreted line-by-line during execution. Does not require compilation.
 Has structured programming instructions
 Has an English like syntax with no restriction on program format
 Has tiny instruction set, surrounded by a large built-in function library
 Most importantly, very simple yet very powerful
REXX - The Basics

Program Format
 Starts with a commented line containing the word “REXX”

The “EXIT” keyword marks the end of the EXEC.
 No restrictions on program format
 A line can have more than one clause
 A clause can span over more than one line
 Allows any indentation to improve program readability
 A semicolon delimits an instruction, but can be implied by line-end
 A comment starts with characters /* and ends with */

Variables do not need explicit declaration
 All the below operators have their usual meaning:
+ - * / % = > < >= <= <>

\= \> \< ** & (AND) | (OR)


Input/Output statements

 SAY – writes a string to the Output Stream

SAY A displays A
A = 100; SAY A displays 100
SAY 'A = ' A displays A = 100


PULL – reads a string from the Input Queue

SAY 'Do you want to delete the file? Answer Yes or No'
PULL Ans
IF Ans = 'No' THEN SAY 'File will not be deleted.'

SAY 'Enter the max and min limits:'


PULL maxval minval

SAY 'Enter the First Value:'


PULL A . >> the dot serves as a dummy place holder
Conditional Processing

 IF.. THEN.. ELSE Construct

IF answer = 'YES' THEN SAY 'OK, alright.'


ELSE SAY 'Why not?'

 SELECT.. WHEN..OTHERWISE Construct

Select
When Balance > 100 Then say 'You have enough Balance.'
When Balance < 100 Then say 'Your Balance is low!'
When Balance < 10 Then say 'Your Balance is critically low!'
When Balance = 0 Then say 'Your Balance is zero. Please recharge.'
Otherwise say 'Your Balance is negative. Please recharge.'
End
Iterative Loops


Simple DO group – It just groups a set of instructions. Does not do repetitive processing

IF answer = 'YES' THEN Do


SAY 'That's correct!'
SAY 'The answer to this question is YES.'
End
ELSE SAY 'Sorry. The correct answer is YES.'

 Repetitive DO Loops – Executes a set of instructions repetitively

Do 5 Do i = 3 to -3 by -1
Say 'Hello' Say i
End End

j = 1 j = 1
Do while (j < 10) Do until (j < 10)
Say j Say j
j = j + 1 j = j + 1
End End
String Parsing


Parsing - splits up the data in a source string and assigns pieces of it into the variables.

Parse Value:
Parse value ’Thick and Thin’ with var1 var2 var3
Say var1 displays 'Thick'
Say var2 displays 'and'
Say var3 displays 'Thin'

Parse Var:
String = 'Bread and Butter'
Parse var String str1 str2 str3
Say Str3 str2 str1 displays 'Butter and Bread'

Parse var String str1 str2


Say str1displays 'Bread'
Say str2displays 'and Butter'

Parse var String str1 . Str2


Say str1displays 'Bread'
Say str2displays 'Butter'
Compound/Stem Variables

 Stem Variables, also called Compound Variables, are a special category of variables that hold
multiple lines of data.
 Compound variables contain a period in their names and allow for indexing.
 They hold an array of numbers or strings.

If stemVar. is considered the name of a stem variable, then


stemVar.0 – would contain the number of lines of data that it holds
stemVar.1 – would contain the first line of data
stemVar.2 – would contain the second line
stemVar.3 – would contain the third line
and so on..

 The below piece of code retrieves all the lines from a stem variable.

do j = 1 to stemVar.0
say stemVar.j
j = j + 1
end
User-defined Functions


REXX allows users to create their own functions (sub-routines) within the exec

A list of frequently executed instructions are grouped under a sub-routine
 The sub-routines can be invoked using call statements whenever needed

Here is an example:

/* Rexx */

j = 1
do while j < 5
call Printj
j = j + 1
end
exit

Printj:
Say 'J = ' j
return
Built-in Functions

 LEFT - Returns the left most characters of specified length


 RIGHT - Returns the right most characters of specified length
 REVERSE - Returns a string swapped end for end
 SUBSTR - Returns a sub-string of specified length starting at a given position
 SUBWORD - Returns a word at a specified position
 LENGTH - Returns the length of the string in characters
 POS - Returns the position of a given character/word in a string
 OVERLAY - Over writes a given string on a target string
 MIN, MAX - Returns the smallest/largest number from the specified list
 DATE - Returns local date in the requested format
 TIME - Returns local time in the requested format
 STRIP - Removes the leading and trailing blanks from a string
 C2D/D2C - Converts a string to decimal and vice versa
 C2X/X2C - Converts a character string to hexadecimal value and vice versa
 SYSDSN - TSO/E external function that verifies whether the given dataset
exists and is available for use.
 And lots more..
Interacting with Subsystems

 A REXX exec can include a variety of commands that are specific to different host
environments. For example, a REXX exec can issue

 TSO Commands (Allocate file)


 ISPF Commands (View/Browse files)
 DB2 Commands (Connect to subsystem)
 z/OS Console commands (IPLINFO)


But before issuing such commands, the correct command environment has to be established.
The “ADDRESS ” instruction directs the commands to the right host environment.

ADDRESS TSO “ALLOCATE DATASET(…)”


ADDRESS ISPEXEC “LIBDEF ISPPLIB … “
ADDRESS DSNREXX “CONNECT DB2P”
ADDRESS CONSOLE “SYSCMD … ”
Interacting with Subsystems

 Connecting to DB2

QueryTxt = "SELECT CURRENT TIMESTAMP FROM SYSIBM.SYSDUMMY1"


ADDRESS DSNREXX
“CONNECT DB2P”
“EXECSQL DECLARE C1 CURSOR FOR S1”
“EXECSQL PREPARE S1 FROM :QueryTxt”
“EXECSQL OPEN C1”
“EXECSQL FETCH C1 INTO :QueryResult”
“EXECSQL CLOSE C1”
“DISCONNECT”
Say “The current timestamp is “ || QueryResult
Trapping Command Output


OUTTRAP – Traps the output of a command into a stem variable.
 This external TSO function is highly used to capture the system response for a command issued
from an exec.

The below piece of code illustrates its syntax.

X = OUTTRAP('Var.')
… REXX/TSO Command …
Y = OUTTRAP('Off')

Here is an example.

Drop var.
x = OUTTRAP('var.')
"REPRO INFILE(INFILE) OUTFILE(OUTFILE)"
Y = OUTTRAP('off')
Do i = 1 to var.0
Say var.i
i = i + 1
End
Handling data sets

 REXX provides an easy way of handling data sets with minimal set of I/O instructions
 The command EXECIO controls data set I/O. It can both read from and write to data sets.

The I/O data set must be either sequential or a single member of a PDS.

The EXECIO command does not perform data set allocation. Before it can perform I/O to or
from the data set, the data set must be allocated to a file specified in EXECIO statement.
 Data sets have to be allocated before they could be used in the exec. Similarly, they should be
closed and deallocated after using them.

A few sample instructions used while handling data sets:

Allocate a File:

Allocate an existing sequential data set:


"ALLOC FI(INFILE) DA('REXX.SEQ.INPUT') SHR REUSE"

Allocate an existing sequential data set:


"ALLCO FI(INFILE) DA('REXX.PDS.INPUT(MEM01)') SHR REUSE"

Allocate a new data set:


Fileparm = " TRACKS SPACE(01,05) RECFM(F,B) LRECL(80) RELEASE REUSE "
"ALLOC FI(OUTFILE) DA('REXX.SEQ.OUTPUT')" || Fileparm || "NEW"
Handling data sets

 Open and read from a File

Read one record from input file into a stem variable:


"EXECIO 1 DISKR INFILE (STEM RVAR.1"

Read all records from the input file:


"EXECIO * DISKR INFILE (STEM RVAR."

 Write to a File

Write a line from a stem variable into the output file


"EXECIO 1 DISKW OUTFILE (STEM WVAR.1"

Write the contents of the stem variable into outfile and close the file:
"EXECIO * DISKW OUTFILE (FINIS STEM WVAR."


Close and deallocate a file

"EXECIO 0 DISKR INFILE (FINIS"


"FREE FI(INFILE OUTFILE)"
System Variables

 System variables in REXX give us the ability to retrieve and use system & session information
in execs. REXX offers two sets of system variables

 SYSVAR
 SYSVAR returns information about MVS, TSO/E, and the current session, such as
levels of software available, your log on procedure, and your user ID.
 The information returned depends on the arg_name value specified on the function
call.
 SYSVAR can only be used in TSO/E environments
Say "ISPF STATUS : " SYSVAR(SYSISPF)
displays ISPF STATUS: NOT ACTIVE

 MVSVAR
 MVSVAR returns information about MVS, TSO/E, and the current session, such as the
symbolic name of the MVS system
MVSVAR(SYSPLEX) displays the MVS sysplex name as found in the COUPLExx
or LOADxx member of SYS1.PARMLIB
System Variables

 A sample list of arguments for SYSVAR

SYSPREF : the prefix assigned to not-fully-qualified dataset names.


SYSPROC : the logon procedure for the current session.
SYSUID : the user ID of the person logged on.
SYSENV : returns FORE or BACK.
SYSCPU : the number of CPU seconds used this session.
SYSSRV : the number of service units used this session.
SYSHSM : the status of DFHSM. Returns AVAILABLE or null.
SYSJES : the name and level of JES.
SYSLRACF : the level of RACF. If RACF is not installed, this is null.
SYSRACF : returns AVAILABLE, NOT AVAILABLE, or NOT INSTALLED.
SYSTSOE : the level of TSO installed.
SYSDTERM : If double-byte character set (DBCS) is enabled, returns YES.
SYSKTERM : if Katakana character set is enabled, returns YES.
SYSTERMID : the terminal ID, or null if batch.
System Variables

 A sample list of arguments for MVSVAR

SYSAPPCLU : the APPC/MVS logical unit (LU) name.


SYSDFP : the level of MVS/Data Facility Product (MVS/DFP).
SYSMVS : the level of the base control program (BCP) component of z/OS.
SYSOPSYS : the z/OS name, version, release, modification level, and FMID.
SYSSECLAB : the security label (SECLABEL) name of the TSO/E session.
SYSSMFID : identifies the system on which SMF is active.
SYSSMS : indicates whether DFSMS/MVS is available to your REXX exec.
SYSCLONE : MVS system symbol representing its system name.
SYMDEF : symbolic variables of your MVS system.
SYSNAME : the name of the system your REXX exec is running on, as specified in the
SYSNAME statement in SYS1.PARMLIB member IEASYSxx.
SYSPLEX : the MVS sysplex name as found in the COUPLExx or LOADxx member of
SYS1.PARMLIB.
REXX - a performance view-point


Here are a performance concerns/tips that can help you code efficient REXX execs

 Shorter variable names are faster. Use meaningful short names.


SSN performs better than Social_Security_Number

 Drop large stem arrays (compound variables) when they are no longer needed
DROP stemvar.

 Literals work better than variables. Prefer them whenever you can.
if Firstname = 'Alfred' works better than
strName = 'Alfred'
if Firstname = strName

 Avoid using functions within functions.


strName = LEFT(STRIP(strName),6)

Instead use:
strName = STRIP(strName)
strName = LEFT(strName,6)
REXX in a performance view-point

 Do not issue functions like DATE and TIME repetitively within loops. Instead issue
them once outside the loop and store them in a variable and use the variable within
the loop.
 Calling external subroutines may not be a great idea if you are within a loop. If at all a
need arises, code the routine inside your exec
 Code subroutines as close to the call as possible
 When there are more than one subroutine, code the frequently used routine first.
 Consider using a subroutine when a large number of statements are needed for
something that is often NOT executed.
 Avoid placing comments inside loops. A single long comment is efficient than multiple
smaller comments.
REXX – The other side

 Of course, REXX has a few limitations just like any other programming language.
 REXX is an interpreted language. Though it offers ease of debugging, its execution
speed is much slower and less efficient compared to compiled languages.
 Consumes more processor resource
 Slower File I/O speed
 Slower decimal arithmetic and relatively lesser mathematical precision
 Lack of native support for file access methods like VSAM

However, the REXX execs can be compiled as well. There are IBM REXX compilers available for
purchase that can greatly improve performance and reduce system load. Compiled REXX
programs run faster than interpreted programs. Because a program has to be compiled only one
time, system load is reduced and response time is improved when the program is run
frequently.
Next Steps – Extensions to REXX

 ISPF Dialogs
 ISPF Dialogs (Panels) are used to develop interactive REXX tools
 Dialogs can gather input, validate them at real-time and decide program path
 The dialogs have to be developed using ISPF panel definitions before invoking them
through REXX

 Edit Macros
 A collection of TSO/ISPF commands packaged to perform a specific activity
 Tailor-made macros can perform a host of house-keeping activities

 File Tailoring
 It is a way of generating formatted reports using predefined templates called skeletons
 It can be used to tailor JCL skeletons and submit jobs automatically
Questions?
References

 IBM TSO/E REXX Reference manual, Ninth edition.


 IBM TSO/E REXX User's Guide, First edition.
 Online REXX Forums
 http://www.tek-tips.com
 http://theamericanprogrammer.com

Das könnte Ihnen auch gefallen