EXEC
[ 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.
Standard syntax
The
Command passed to EXEC must be specified as either a list of comma delimited string constants or as an array. The first element in the list/array is the name of the command, and the other elements are the commands parameters (if any).
If
WAIT is specified, then the interpreter waits for the command to complete, otherwise the command is run in background and your program will continue executing.
If
FOR is specified, then the command input-outputs are redirected so that your program intercepts them:
- If WRITE is specified, data can be sent to the command standard input by using the Process object with common output instructions: PRINT, WRITE, ... Note that a reference to the Process object is required.
- If READ is specified, then events will be generated each time the command sends data to its standard output التيار (stream): the Read event is raised when data are sent to the standard output stream, and the Error event 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. The process will think running inside a true terminal.
 |
If you plan to control an application by sending commands to standard input then testing should be performed outside of the IDE (i.e. make an executable and launch it from the command line) as the console within the development environment is not a true virtual terminal and will cause unexpected results.
|
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.
Quick syntax
If you use the second form of the syntax,
EXEC Command TO Variable
the interpreter waits for the command to complete, and then places the complete command output in the specified string.
During execution of the command you have no control over the process being executed.
 |
Only the standard output of the process is retrieved. The error output is not redirected.
|
مثال
' 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
|