REXX REXX Workshop - Part 1

Introduction


Scripting Example

RENAME a file, issue FILELIST command

                                                                                                                                                 
/* This is a comment; REXX programs start with comments. */                                                                                           
                                                                                                                                                      
Address 'CMS'                   /* Send all non-rexx     */                                                                                           
                                /* instructions to CMS   */                                                                                           
                                /* for processing.       */                                                                                           
                                                                                                                                                      
'RENAME MY FILE A YOUR FILE A'  /* Rename the file       */                                                                                           
'EXEC FILELIST'                 /* Invoke FILELIST EXEC  */                                                                                           
                                                                                                                                                      
Exit                            /* End program           */                                                                                           

Procedureal Example

Receive two numbers from the keyboard and write out the sum.

                                                                                                                                                 
/* Get two numbers from the keyboard, write out the sum. */                                                                                           
                                                                                                                                                      
Say "Enter a number"            /* Prompt the user.      */                                                                                           
Pull n1                         /* Receive input.        */                                                                                           
Say "Enter another number"      /* Prompt again.         */                                                                                           
Pull n2                         /* Receive second input. */                                                                                           
                                                                                                                                                      
sum = n1 + n2                   /* Calculate the sum.    */                                                                                           
                                                                                                                                                      
Say n1 "+" n2 "=" sum           /* Write out the answer. */                                                                                           
                                                                                                                                                      
Exit                            /* End program.          */                                                                                           

Quick History

With the introduction of NetRexx and Object Rexx, the original language is now referred to as Classic Rexx.

REXX is available on virtually every computing platform from mainframes to microcomputers, but it is most commonly associated with IBM mainframe operating systems.


Language Elements


Comments

The comments in the example below are highlighted in red.

                                                                                                                                                 
         /* My First REXX Exec */                                                                                        
         Say "What is your name?" /* Prompt for name. */                                                                 
         Pull name                                                                                                                                    
         Say "Hello," name"!"     /* Greet the user.  */                                                                 
         Exit                                                                                                                                         

Exercise: Using Xedit, create a file with the name MYFIRST EXEC A and enter in the preceding REXX program. File the exec. Run the exec by entering its name on the CMS command line.

Strings

The strings in the example below are highlighted in red.

                                                                                                                                                 
         /* My First REXX Exec */                                                                                                                     
         Say 'What is your name?' /* Prompt for name. */                                                                 
         Pull name                                                                                                                                    
         Say "Hello," name||"!"   /* Greet the user.  */                                                                 
         Exit                                                                                                                                         

Variables

The variables in the example below are highlighted in red.

                                                                                                                                                 
         /* Add two numbers and report the sum. */                                                                                                    
         Say "Enter a number"                                                                                                                         
         Pull n1                                                                                                         
         Say "Enter another number"                                                                                                                   
         Pull n2                                                                                                         
         sum = n1 + n2                                         
         Say n1 "+" n2 "=" sum                                 
         Exit                                                                                                                                         

Operators


Expressions

Valid expressions:
                                                                                                                                                 
         4 + 3 - 1             results in   6                                                                                                         
                                                                                                                                                      
         4 * 3 - 1             results in   11                                                                                                        
                                                                                                                                                      
         4 * (3 - 1)           results in   8                                                                                                         
                                                                                                                                                      
         "red" "dog"           results in   "red dog"                                                                                                 
                                                                                                                                                      
         "red" || "dog"        results in   "reddog"                                                                                                  
                                                                                                                                                      
         "red" 4               results in   "red 4"                                                                                                   
                                                                                                                                                      
         "red" 4+5             results in   "red 9"                                                                                                   
                                                                                                                                                      
         4 * 2 || 5            results in   "85"                                                                                                      
                                                                                                                                                      
         4 * 2 5               results in   "8 5"                                                                                                     
                                                                                                                                                      
         4 * (2 || 5)          results in   100                                                                                                       
                                                                                                                                                      
         4 * "1.2"             results in   4.8                                                                                                       

Exercise: Write a short REXX exec to display the results of some of the expressions listed above. Run the exec. Alter the expression and run it again. Try some different expressions. For example,
                                                                                                                                                 
         /* Write out the value of expressions. */                                                                                                    
         Say 4 + 5 / 2                                                                                                                                
         Exit                                                                                                                                         

REXX Instructions

The REXX keywords in the example below are highlighted in red.

                                                                                                                                                 
         /* Examples of REXX Instructions */                                                                                                          
         Say "Enter a number"                                                                                            
         Pull n                                                                                                          
         If n = 0 Then Exit                                                                 
         Do n                                                                                                            
            Say "Hello there!"                                                                                           
            End                                                                                                          
         Exit                                                                                                            

Assignment Statements

Assignment statements are used to assign values to variables.
                                                                                                                                                 
         n    = 5               stores "5" in n                                                                                                       
         var  = 6 + 1           stores "7" in var                                                                                                     
         var2 = var + 2         stores "9" in var2                                                                                                    
         name = "Bill"          stores "Bill" in name                                                                                                 
         x    = "Bill"||5 + 7   stores "Bill12" in x                                                                                                  
Expressions to the right of the equal sign are evaluated and the result is stored in the variable to the left of the equal sign.


Labels


Commands


The Variable RC

If a command completes successfully it signals the REXX exec which invoked it by placing (usually) a zero ("0") into the special REXX variable "RC". If a command encounters errors, it usually places some other, non-zero numeric value into the RC variable. Execs can test the RC variable to determine whether or not the command succeeded.
                                                                                                                                                 
         .                                                                                                                                            
         .                                                                                                                                            
         .                                                                                                                                            
         'EXEC FILELIST'                                                                                                                              
         If rc = 0                                                                                                                                    
            Then Say 'All is well.'                                                                                                                   
            Else Say 'Filelist may have erred.'                                                                                                       
         .                                                                                                                                            
         .                                                                                                                                            
         .                                                                                                                                            
  
More information about the IF REXX instruction will be supplied later.


Miscellaneous Syntax Notes


Online VM/CMS Help

For online help for REXX, CP commands, CMS commands, or Xedit subcommands, enter one of the following commands on the CMS or Xedit command line:
                                                                                                                                                 
         HELP REXX MENU                                                                                                                               
         HELP CP MENU                                                                                                                                 
         HELP CMS MENU                                                                                                                                
         HELP XEDIT MENU                                                                                                                              
Exercise: invoke HELP for REXX and read a few entries.

The following BookManager books may be consulted for more complete discussions, if REXX is running under VM/CMS.