One of the biggest advantages in Rexx is the ability to create re-usable scripts. Often in organizations nowadays, having re-usable scripts is a big value add in saving time to do common repetitive tasks.
For example, technology teams in an IT organization can have the need to have scripts which do common everyday tasks. These tasks can include interacting with the operating systems. These scripts can then be programmed to handle bad return codes or errors.
Rexx offers a lot of system commands that can be used to perform such repetitive tasks. Let’s look at some of the system commands available in Rexx.
This is the normal directory listing command which is used in Windows.
dir
None
This method returns the current directory listing on the system.
/* Main program */ dir
The output depends on the directory in the system.
The following program is just an example.
Volume in drive D is LENOVO Volume Serial Number is BAC9-9E3F Directory of D:\ 04/06/2016 12:52 AM 268,205 100008676689.pdf 10/20/2015 08:51 PM <DIR> data 06/01/2016 10:23 AM 31 Example.txt 10/28/2014 06:55 PM <DIR> Intel 06/02/2016 11:15 AM 23 main.rexx 12/22/2014 08:49 AM <DIR> PerfLogs 12/13/2015 11:45 PM <DIR> Program Files 12/24/2015 10:26 AM <DIR> Program Files (x86) 07/17/2015 01:21 AM <DIR> Users 12/23/2015 10:01 AM <DIR> Windows 3 File(s) 268,259 bytes 7 Dir(s) 202,567,680 bytes free
Another example of the dir command is shown in the following program. Only this time we are making use of the special rc variable. This variable is special in Rexx and gives you the status of the execution of system commands. If the value returned is 0, then that means the command is executed successfully. Else the error number will be given in the rc variable name.
/* Main program */ dir if rc = 0 then say 'The command executed successfully' else say 'The command failed, The error code is =' rc
When we run the above program we will get the following result.
The command failed, The error code is = 127
Rexx also has the facility of using redirection commands. The following redirection commands are available in Rexx.
< − This command is used to take in the input which comes from a file.
> − This command is used to output the content to a file. If the file does exist, it will be over-written.
>> − This is also used to output the content to a file. But the output is added to the end of the file to preserve the existing content of the file.
Let’s look at an example of how we can use redirection commands. In the following example, we are using the sort command to sort a file called sortin.txt. The data from the file is sent to the sort command. The output of the sort command is then sent to the sortout.txt file.
/* Main program */ 'sort <sortin.txt> sortout.txt'
Assume that the file sortin.txt has the following data.
b c a
The file sortout.txt will then have the following data.
a b c
This method is used to find out what is the default environment used for the Input, Error and Output streams.
ADDRESS(options)
Options for what is the address of a particular system.
This method returns the name of the environment for the Input, Error and Output streams.
/* Main program */ say ADDRESS('I') say ADDRESS('O') say ADDRESS('E')
When we run the above program we will get the following result.
INPUT NORMAL REPLACE NORMAL REPLACE NORMAL