In this section, we will explore how to interface REXX with external programs. This capability allows REXX scripts to interact with other software, execute system commands, and leverage external utilities, thereby extending the functionality of REXX programs.

Key Concepts

  1. Calling External Programs: Learn how to execute external programs and system commands from within a REXX script.
  2. Capturing Output: Understand how to capture the output of external programs for further processing in REXX.
  3. Error Handling: Manage errors that may occur when interfacing with external programs.
  4. Practical Examples: Implement practical examples to solidify understanding.

Calling External Programs

REXX provides the ADDRESS instruction to execute commands in different environments. The most common environment is the system command line.

Syntax

ADDRESS environment command
  • environment: Specifies the command environment (e.g., SYSTEM for system commands).
  • command: The command to be executed.

Example: Running a System Command

/* REXX script to list files in the current directory */
ADDRESS SYSTEM 'dir'

Explanation

  • ADDRESS SYSTEM 'dir': Executes the dir command in the system environment, listing the files in the current directory.

Capturing Output

To capture the output of an external command, you can use the LINEIN function to read the output line by line.

Example: Capturing Command Output

/* REXX script to capture and display the output of a system command */
ADDRESS SYSTEM 'dir > output.txt'
output = ''
DO WHILE LINES('output.txt') > 0
    output = LINEIN('output.txt')
    SAY output
END
CALL LINEOUT 'output.txt', 'CLOSE'

Explanation

  • ADDRESS SYSTEM 'dir > output.txt': Redirects the output of the dir command to a file named output.txt.
  • DO WHILE LINES('output.txt') > 0: Loops through each line of the output file.
  • output = LINEIN('output.txt'): Reads a line from the output file.
  • SAY output: Displays the line.
  • CALL LINEOUT 'output.txt', 'CLOSE': Closes the output file.

Error Handling

When interfacing with external programs, it is crucial to handle potential errors gracefully.

Example: Error Handling

/* REXX script with error handling for external command execution */
ADDRESS SYSTEM 'nonexistent_command > output.txt'
IF RC \= 0 THEN
    SAY 'Error: Command execution failed with return code' RC
ELSE
    SAY 'Command executed successfully'

Explanation

  • ADDRESS SYSTEM 'nonexistent_command > output.txt': Attempts to execute a nonexistent command.
  • IF RC \= 0 THEN: Checks if the return code (RC) is not zero, indicating an error.
  • SAY 'Error: Command execution failed with return code' RC: Displays an error message with the return code.
  • ELSE SAY 'Command executed successfully': Displays a success message if the command executed without errors.

Practical Examples

Example 1: Running a Python Script from REXX

/* REXX script to run a Python script and capture its output */
ADDRESS SYSTEM 'python myscript.py > output.txt'
output = ''
DO WHILE LINES('output.txt') > 0
    output = LINEIN('output.txt')
    SAY output
END
CALL LINEOUT 'output.txt', 'CLOSE'

Example 2: Pinging a Server

/* REXX script to ping a server and display the result */
ADDRESS SYSTEM 'ping www.example.com > ping_result.txt'
output = ''
DO WHILE LINES('ping_result.txt') > 0
    output = LINEIN('ping_result.txt')
    SAY output
END
CALL LINEOUT 'ping_result.txt', 'CLOSE'

Exercises

Exercise 1: Execute a System Command

Write a REXX script to execute the date command and display the current date and time.

Solution:

/* REXX script to display the current date and time */
ADDRESS SYSTEM 'date > date_output.txt'
output = ''
DO WHILE LINES('date_output.txt') > 0
    output = LINEIN('date_output.txt')
    SAY output
END
CALL LINEOUT 'date_output.txt', 'CLOSE'

Exercise 2: Error Handling

Modify the above script to handle errors if the date command fails.

Solution:

/* REXX script to display the current date and time with error handling */
ADDRESS SYSTEM 'date > date_output.txt'
IF RC \= 0 THEN
    SAY 'Error: Command execution failed with return code' RC
ELSE DO
    output = ''
    DO WHILE LINES('date_output.txt') > 0
        output = LINEIN('date_output.txt')
        SAY output
    END
    CALL LINEOUT 'date_output.txt', 'CLOSE'
END

Conclusion

In this section, we learned how to interface REXX with external programs, execute system commands, capture their output, and handle errors. These skills are essential for extending the functionality of REXX scripts and integrating them with other software and system utilities. In the next section, we will delve into REXX macros, which provide powerful ways to automate and extend REXX capabilities.

© Copyright 2024. All rights reserved