/ lang / exec
EXEC
Syntax

    [ Process = ] EXEC Command [ WAIT ] [ FOR { { READ | INPUT } | { WRITE | OUTPUT } } ] [ AS Name ]

    EXEC Command TO Variable

Executes a command. An internal Process object is created to manage the command.

The command must be specified as an array of strings containing at least one element. The first element of this array is the name of the command, and the others are optional parameters.

- If `WAIT` is specified, then the interpreter waits for the command ending. Otherwise, the command is executed in background.

- If `FOR` is specified, then the command input-outputs are redirected so that your program intercepts them:

  - If `WRITE` is specified, you can send data to the command standard input by using the Process object with common output instructions: PRINT, WRITE, ... Note that you need a reference to the Process object for that.

  - If `READ` is specified, then events will be generated each time the command sends data to its standard output stream: the event Read is raised when data are sent to the standard output stream, and the event Error is raised when data are sent to the standard error stream. Use the process object with Stream & Input/Output functions to read the process standard output.

If you use the `INPUT` and `OUTPUT` keywords instead of `READ` and `WRITE`, then the process is executed inside a virtual terminal. It means that the process will think running inside a true terminal.

`Name` is the event name used by the Process object. By default, it is `"Process"`.

You can get a reference to the internal Process object created by using an assignment.

If you use the second syntax, the command is executed, the interpreter waiting for its end, and the complete command output is put in the specified string.

### Examples

    ' Get the contents of a directory
    EXEC [ "ls", "-la", "/tmp" ] WAIT

    ' Get the contents of a directory into a string
    DIM sOutput AS String
    EXEC [ "ls", "-la", "/tmp" ] TO sOutput

    ' Get the contents of a directory into a string, but in background
    DIM sOutput AS String

    ' A specific event name is used
    EXEC [ "ls", "-la", "/tmp" ] FOR READ AS "Contents"

    ...

    PUBLIC SUB Contents_Read()

      DIM sLine AS String

      READ #LAST, sLine, -256

      sOutput &= sLine

    END

    PUBLIC SUB Contents_Kill()

      PRINT sOutput

    END

| If you want to know how many bytes you can read in a `Process_Read` event handler, use the Lof function.

| As arguments are sent directly to the process, you do not have to quote them, as you must do in a shell.
    ' perl -e 'print while <>;' becomes

    EXEC [ "perl", "-e", "print while <>;" ] FOR READ WRITE

### See also

Process Management Process Lof