Pipes & Errorlevel

[Autolink] Menu

If you're using pipes in REXX programs, remember that the special REXX variable RC always contains the return code of the last program in the command containing the pipes.
Example:

 
/*                                                                    */
  address cmd 'dir | sort | find "<DIR>" '

                    /* The next statement will always print the       */
                    /* return code of the find command as the         */
                    /* value of RC                                    */
  say 'RC=' || rc

To read the return codes of the programs in the middle of the pipe as well, you can use a REXX wrapper.
Example:

 
/* ------------------------------------------------------------------ */
/* PushRC - simple wrapper program to save the return code of an      */
/*          OS/2 command or program in the current REXX queue         */
/*                                                                    */
/* Usage: pushRC program parameter                                    */
/*                                                                    */
/* History                                                            */
/*   01.09.1996 /bs v1.00                                             */
/*     - initial release                                              */
/*                                                                    */
/*                                                                    */
/*                                                                    */
/*                                                                    */

  parse arg progName progParameter

  "cmd /c" progName progParameter

                    /* progname (or something similar) is necessary   */
                    /* because OS/2 is a multi-tasking                */
                    /* operating system                               */
  push progName "RC="rc

Usage example:

 
/*                                                                    */
  address cmd "pushrc dir | pushrc sort | pushrc find ""<DIR>"" "

  do while queued() <> 0
    say lineIn( "QUEUE:" )
  end /* do while queued() <> 0 */

(see also Using PIPEs)


[Back: Using Pipes]
[Next: Hints for some OS/2 commands]