[Toc][Index]

Simulate the ksh function $( )





 
/* Test code for the exec function                                     */

                    /* push something to the current queue             */
   push "Testing ..."

   curResult = exec( "dir" , "STDOUT", "##" || "0D0A"x , "B"  )
   say "The Result of exec( "dir" ...) is "
   say curResult

                    /* check the current REXX queue                    */
   pull test
   say test

exit

/* ------------------------------------------------------------------ */
/* function: simulate $( ) from the ksh shell                         */
/*           The $() function calls a command and saves the result    */
/*           in a variable.                                           */
/*                                                                    */
/* call:     exec( cmd, [ stream ], lineSep, strip )                  */
/*                                                                    */
/* where:    cmd = command to execute                                 */
/*           stream = either STDOUT, STDERR or BOTH                   */
/*                   (def.: STDOUT)                                   */
/*           lineSep = line separator                                 */
/*                     (def. "0D0A"x)                                 */
/*           strip = strip blanks (B, L, T)                           */
/*                                                                    */
/*                                                                    */
/* notes:    This routine needs rxqueue                               */
/*           It does NOT change the contents of the current queue!    */
/*                                                                    */
/* returns:  the output of the command as one string                  */
/*                                                                    */
exec: PROCEDURE
                    /* init the return code                           */
  thisRC = ""

                    /* install local error handler                    */
  signal on syntax name execEnd
  signal on error name execEnd
  signal on failure name execEnd
  signal on halt name execEnd

                    /* init the default values                        */
  defStream = "STDOUT"
  defLineSep = "0D0A"x

                    /* save the current queue and create a new one    */
  curREXXqueue = rxqueue( "Get" )
  newREXXqueue = rxqueue( "Create" )
  call rxqueue "Set", newREXXqueue

                    /* process the parameter                          */
  parse arg cmd, stream, lineSep, strip

  if wordpos( stream, "STDOUT STDERR BOTH 1 2" ) = 0 then
    stream = defStream

  if arg( 3, 'o' ) = 1 then
    lineSep = defLineSep


  select

    when stream = "STDOUT" | stream = 1 then
      curCmd = cmd " 2>>nul "

    when stream = "STDERR" | stream = 2 then
      curCmd = cmd " 2>>&1 1>>nul "

    otherwise
      curCmd = cmd " 2>>&1 "

  end /* select */

                    /* execute the command                            */
  "@cmd /c " curCmd "| rxqueue" newREXXqueue

                    /* copy the output into the result variable       */
  do while queued() <> 0
    curLine = lineIn( 'QUEUE:' )
    if strip = 'B' | strip = 'T' | strip = 'B' then
      curLine = strip( curLine, strip )
    thisRC = thisRC || curLine || lineSep

/*
    if queued() <> 0 then
        thisRC = thisRC || lineSep
*/

  end /* do while */

execEnd:

                    /* delete the new queue and set the old one again */
  call rxqueue "Delete", newREXXqueue
  call rxqueue "Set", curREXXqueue

return thisRC



Created using Inf-PHP v.2 (c) 2003 Yuri Prokushev
Created using Inf-HTML v.0.9b (c) 1995
Peter Childs