Skip to main content

skip to main content

developerWorks  >  AIX and UNIX  >

Rexx and what it means for AIX

Easy, powerful cross-platform scripting

developerWorks
Document options

Document options requiring JavaScript are not displayed


Rate this page

Help us improve this content


Level: Intermediate

Cameron Laird (claird@phaseit.net), Vice President, Phaseit, Inc.

13 Mar 2007

Nearly thirty years of growth haven't exhausted the potential of the REstructured eXtended eXecutor (Rexx) language. The first of the widely used "scripting" languages continues to expand its capabilities and platform range, and it makes for a particularly good match with AIX®.

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.

Readable Rexx

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.

Conclusion

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.



Resources

Learn

Get products and technologies
  • IBM trial software: Build your next development project with software for download directly from developerWorks.


Discuss


About the author

Photo of Cameron Laird

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.




Rate this page


Please take a moment to complete this form to help us better serve you.



 


 


Not
useful
Extremely
useful
 


Share this....

digg Digg this story del.icio.us del.icio.us Slashdot Slashdot it!



Back to top


IBM, AIX, AIX 5L, CICS, DB2, OS/2, and System Z are registered trademarks of International Business Machines Corporation in the United States, other countries, or both. Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. in the United States, other countries, or both. Linux is a registered trademark of Linus Torvalds in the United States, other countries, or both. UNIX is a registered trademark of The Open Group in the United States and other countries. Other company, product, or service names may be trademarks or service marks of others.