Chapter 7. Commands

Table of Contents
How to Issue Commands
Command Echo
Rexx and Batch Files
Issuing a Command to Call a .CMD File
Using Variables to Build Commands
Using Quotation Marks
ADDRESS Instruction
Using Return Codes from Commands
Subcommand Processing
Trapping Command Errors

From a Rexx program you can pass commands to Windows and Unix/Linux shells or to applications designed to work with Rexx. When used to run operating system commands, Rexx becomes a powerful substitute for the Windows Batch Facility or the Unix shell. You can use variables, control structures, mathematics, and parsing to create procedures that would be impossible to implement with the Windows Batch Facility.

Applications that are designed to work with Rexx are often referred to as scriptable applications. To work with Rexx, a scriptable application registers an environment with Rexx. An environment serves as a kind of workspace shared between Rexx and the application. Environments accept application subcommands issued from Rexx programs.

For example, many editors provide a command prompt or dialog box from which you can issue subcommands to set margins or add lines. If the editor is scriptable from Rexx, you can issue editor subcommands from a Rexx program. These Rexx programs are referred to as macros.

When an application runs a Rexx macro, Rexx directs commands to the application's environment. If you issue a subcommand that the application does not recognize, it might pass the command to Windows, depending on the application.

To let you specify which environment processes a command, Rexx includes an ADDRESS instruction. Starting your Rexx programs from the Windows command line makes Windows the default environment for Rexx commands.

How to Issue Commands

Rexx makes it easy to issue commands. The basic rule is that whatever Rexx cannot process it passes to the default environment. You can:

Rexx processes your program one clause at a time. It examines each clause to determine if it is:

If the clause is none of the above, Rexx evaluates the entire clause as an expression and passes the resulting string to Windows.

If the string is a valid operating system command, the OS processes it as though you had typed the string at the command prompt and pressed the Enter key.

The following example shows a Rexx clause that uses the DIR command to display a list of files in the current directory.

/* display current directory */
say "DIR command using Rexx"
dir

The clause dir is not a Rexx instruction or a label, so Rexx evaluates it and passes the resulting string to Windows. Windows recognizes the string DIR as one of its commands and processes it.

Letting Rexx evaluate the command as an expression might cause problems, however. Try adding a path to the DIR command in the above program (such as, dir c:\config.sys). The Windows command in this case is an incorrect Rexx expression. The program ends with an error.

A safer way to issue commands is by enclosing the command in quotes, which makes the command a literal string. Rexx does not evaluate the contents of strings. Because the string is not a Rexx instruction or label, Rexx passes the string to Windows. Here is an example using the PATH command:

/* display current path      */
say "PATH command using Rexx"
"path"

The following example, DP.CMD, shows a program using the DIR and PATH commands. The PAUSE command is added to wait for the user to press a key before issuing the next instruction or command. Borders are added too.

/* DP.CMD -- Issue DIR and PATH commands to Windows */

say "="~copies(40)    /* display line of equal   */
                      /* signs (=) for a border  */

"dir"                 /* display listing of      */
                      /* the current directory   */

"pause"               /* pauses processing and   */
                      /* tells user to "Press    */
                      /* any key to continue."   */

say "="~copies(40)    /* display line of =       */
"path"                /* display the current     */
                      /* PATH setting            */

When you specify the following:

[C:\]rexx dp

a possible output would be:

========================================

The volume label in drive C is WIN.
Directory of C:\EXAMPLES

.            <DIR>     10-16-94  12:43p
..           <DIR>     10-16-94  12:43p
EX4_1    CMD     nnnn  10-16-94   1:08p
DEMO     TXT      117  10-16-94   1:10p
4 File(s)   12163072 bytes free
Press any key when ready . . .

========================================
PATH=C:\WINDOWS
[C:\]

Note: Whenever you execute a host command addressed to the Windows or Unix/Linux command shell, that is, an expression that is passed to the system by address CMD as described above, a new process is created in which the system command handler is executed. When the host command returns, any changes to the process environment, such as changing the directory, are not passed back to the process running the Rexx program. To avoid this potential problem, use a different function such as directory() instead of the command CD