What can the REstructured eXtended eXecutor (Rexx) language do for you as an AIX® developer or administrator? More than you might realize, particularly with the first official release late in 2006 of the Open Object Rexx (ooRexx) variant for AIX.
Much of what you need to know about this scripting language appears, in one aspect or another, in Listing 1:
Listing 1. Word counting in Rexx
parse arg filein
count. = 0
do while lines(filein) > 0
input = linein(filein)
do n = 1 to words(input)
w = word(input, n)
count.w = count.w + 1
if count.w = 1 then word_list = word_list w
end
end
do i = 1 to words(word_list)
w = word(word_list, i)
say w count.w
end
|
This is an uncommented Rexx program. Can you guess what it does? I'll bet you can, even without ever having tried to learn the language. This program:
- Opens a file whose name appears on the command line
- Reads each line of the file
- Breaks each line into words
- Accumulates counts of each word
- Reports the total number of appearances of each word
When run on a UNIX® machine with Rexx installed as Rexx word_count.Rexx draft.xml, the output looks like Listing 2.
Listing 2. Typical output from the word-count program.
.
.
.
included 3
in 55
file 3
.
.
.
|
Don't worry if Rexx is not installed on your desktop; I'll come back to questions of availability in a moment.
The first lesson from Listing 1, then, is that Rexx programs are easy to read: They're a lot like the shell programs you already write, except they have better arithmetic and their associative arrays are potent. An associative array (also called hash, dictionary, stem variable, and so on) is an indexed datatype whose index can be "any" string, not just the integers used by C's arrays. In the count example program, a stem variable maps a word to the number of times that word occurs.
To use Rexx effectively yourself, you need to learn the language and access its references. That's also easy: There are plenty of Rexx resources available, including both on-line and printed treatments, available at no charge. The Resources section below, including several developerWorks articles, contains more than enough references to start you on the path to programming in this nearly thirty-year-old language.
If you typed Rexx word_count.Rexx draft.xml in the command line from above on most desktops, though, you'll quickly learn that Rexx simply isn't installed. If few modern computers offer Rexx in a default installation, is it worth your trouble to learn it? Let's review the other essentials of Rexx's "cultural role," so you can fairly make that judgment for yourself.
Rexx was crucial for Amiga and OS/2® desktops, and it remains a standard scripting language for large IBM hosts -- System z™, CICS®, and so on. IBM has offered Rexx as a product for AIX under several different terms during the lifespan of the operating system. Whatever particular versions you run, there are at least a couple of prominent implementations -- Regina and ooRexx, most likely -- that can be downloaded for your host. That's the next key fact about the language: There's a Rexx available for your computer, with few exceptions. Moreover, along with the "native" implementations of Rexx, every system that supports a Java™ Virtual Machine (JVM) can run Rexx because the NetRexx variant compiles directly to Java bytecodes.
Among other things, all this portability means that, if you work in Rexx on one computer or network, you'll be able to apply that knowledge wherever else you go. This is especially important for those with responsibilities that range over mainframes and smaller systems, which share so little else in their programming or maintenance models.
Integration with the operating system
Let's see what more Listing 1 teaches. Suppose you wanted your report sorted; how does Rexx do that?
ANSI Standard Rexx doesn't include a sort routine, function, or keyword. That should shock you; sorting is a requirement of many of even the simplest applications. The Rexx planners didn't simply overlook this reality, though. Instead, part of the essence of Rexx is to integrate closely with the command-line environment of its host operating system. Rexx regards every statement that can't be parsed as an assignment or instruction as a "native" command. To adjust Listing 1 to produce a sorted result, you need only write:
Listing 3. Sorted word counting in Rexx
parse arg filein
count. = 0
do while lines(filein) > 0
input = linein(filein)
do n = 1 to words(input)
w = word(input, n)
count.w = count.w + 1
if count.w = 1 then word_list = word_list w
end
end
tmpfile = "/tmp/mytempfile"
stream(tmpfile, 'c', "open write replace")
do i = 1 to words(word_list)
w = word(word_list, i)
oneline = w count.w
call lineout tmpfile, oneline
end
command = "sort" tmpfile
address system command
do i = 1 while queued() \= 0
parse pull line
say line
end
|
This example writes out the table of word counts to the tmpfile external file, and then relies on the sort external system command to sort the resulting report. AIX and other flavors of UNIX build in the command-line sort utility.
Listing 4. Example sorted report from Listing 3
...
along 1
alongside 1
already 3
also 7
...
|
Most modern Rexx installations build in their own sorting extension, and it's easy to define a subroutine that implements quicksort or bubblesort for those which lack it. Listing 3 is instructive because so much of Rexx usage has to do with parsing, managing, and rearranging results from other facilities. In environments that handle enormous datasets, it can make sense to have Rexx team with a specialized external sort command. The next Rexx fact, therefore, is Rexx expects to "partner" with system commands.
Doesn't this show how clumsy Rexx is, though? A shell program to produce a sorted list of word counts can be as brief and simple as:
Listing 5. Sorted word counting in shell
FILE=$1
TMPFILE=/tmp/$$
for word in `cat $FILE`
do
echo $word >> $TMPFILE
done
sort $TMPFILE | uniq -c
|
Isn't this a better solution?
For the immediate problem, yes, shell is better; it makes good use of the built-in uniq -c command. Even reformatting the report or simple filtering probably adds only a single line more to the sh source.
Shell starts to falter and stumble, though, when asked to do arithmetic or manage data structures more sophisticated than space-separated records. Rexx is practical for much more demanding projects than sh. ooRexx, in particular, builds in a base set of classes, including:
- QUEUE
- TABLE
- ALARM
- SET
- MESSAGE
- And a half-dozen more
ooRexx fully supports such aspects of object orientation as polymorphism, inheritance, and important libraries, such as TCP/IP and mathematics, so that Rexx programs can solve complex requirements. Even graphical user interface programming is possible with Rexx/Tk, Rexx/DW, or one of the other GUI libraries.
Now you know the basic facts about Rexx. If you're already fluent in a couple of dynamic languages and a modern shell, Rexx might not present much of an advantage to you. If, on the other hand, your daily focus is on something else and doesn't demand that you think in terms of metaprogramming or continuations, and especially if you work across a range of IBM systems, you'll want to read through the Resources section to learn more about Rexx.
Remember:
- Rexx programs are easy to read.
- You can find plenty of Rexx resources available.
- Few modern computers offer Rexx in a default installation.
- Rexx is available for your computer.
- Rexx expects to "partner" with system commands.
- Rexx is practical for much more demanding projects than
sh.
Good luck with your Rexx career.
Learn
- Open Object Rexx Web site: This site is valuable and competently maintained. For a quick start in programming with Rexx, retrieve an appropriate version of ooRexx from the download page. Note that, while there was a time when AIX users effectively had to pay for Rexx, open source "ooRexx for AIX is available starting with the 3.1 release."
- "Rexx for everyone" (developerWorks, February 2004): This article introduces such key Rexx concepts as stacks and string-parsing.
- Rexx runs on mainframes, handhelds, and nearly everything in-between, including Java virtual machines and Apache Web servers. It even has special powers to control DB2® databases, as Howard Fosdick's 2005 article "Script for DB2 Universal Database using Rexx: Quick DB2 UDB scripting" explains. Not only do Rexx products support a number of distinct standards for accessing database management systems (DBMSs) "from the outside," some implementations of DB2 permit stored procedures (although not triggers or functions) to be expressed in Rexx.
- comp.lang.Rexx: One of my favorite resources for learning Rexx is the Usenet newsgroup.
- "Awk by example (developerWorks, January 2001): This article gently introduces associative arrays. Rexx labels the same concept a "stem variable," as illustrated in this workshop tutorial.
- Rexx Language Association: This site maintains a page on Standards, including the 1996 ANSI specification X3.274-1996.
- rexx/Tk and Rexx/DW: Rexx/TK and Rexx/DW are the leading GUI libraries.
- "NetRexx ... let[s] you create applications and applets for the Java environment faster and more easily than by programming in Java.": It's a bold claim, but one IBM can reasonably make -- NetRexx is that good. NetRexx also gives all standard functionality of Rexx.
- Rexx historic documents: These documents explain the highlights of Rexx's trajectory since its invention in 1979.
- Check out other articles and tutorials written by Cameron Laird:
- Search the AIX and UNIX® library by topic:
- System administration
- Application development
- Performance
- Porting
- Security
- Tips
- Tools and utilities
- Java technology
- Linux®
- Open source
- AIX and UNIX: The AIX and UNIX developerWorks zone provides a wealth of information relating to all aspects of AIX systems administration and expanding your UNIX skills.
- New to AIX and UNIX: Visit the New to AIX and UNIX page to learn more about AIX and UNIX.
- AIX 5L Wiki: A collaborative environment for technical information related to AIX.
- Safari bookstore: Visit this e-reference library to find specific technical resources.
- developerWorks technical events and webcasts: Stay current with developerWorks technical events and webcasts.
- Podcasts: Tune in and catch up with IBM technical experts.
Get products and technologies
- IBM trial software: Build your next development project with software for download directly from developerWorks.
Discuss
- Participate in the developerWorks blogs and get involved in the developerWorks community.
- AIX Power Users Group: Participate and get involved in the AIX community.
-
Participate in the AIX and UNIX forums:
- AIX 5L -- technical forum
- AIX for Developers Forum
- Cluster Systems Management
- IBM Support Assistant
- Performance Tools -- technical
- Virtualization -- technical
- More AIX and UNIX forums

Cameron Laird is a long-time developerWorks contributor and former columnist. He often writes about the open source projects that accelerate development of his employer's applications, focused on reliability and security. He first used AIX twenty years ago, when it was still an experimental product. He's been an enthusiastic consumer of and contributor to a variety of memory debugging tools through that time. You can contact him at claird@phaseit.net.




