SHELL
[ Process = ] SHELL Command [ WAIT ] [ FOR { { READ | INPUT } | { WRITE | OUTPUT } } ] [ AS Name ]
SHELL Command TO Variable
Executes a command in a system shell. An internal
Process objeto is created to manage the command.
Standard syntax
The command is a string containing a command passed to the system shell (
/bin/sh).
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 objeto with common output instructions: PRINT, WRITE, ... Note that you need a reference to the Process objeto for that.
- If READ is specified, then events will be generated each time the command sends data to its standard output streams: 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 objeto with Funções de Stream e Entrada/Saída 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.
 |
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 objeto. By default, it is
"Process".
You can get a reference to the internal
Process objeto created by using an assignment.
Quick syntax
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.
You have no control on the executed process.
 |
Only the standard output of the process is retrieved. The error output is not redirected.
If you need to mix both output, use the shell redirection syntax:
Shell "command 2>&1" To Result
|
Argument quoting
As arguments are sent to a shell, you have to quote them, as if you type a command directly in it.
SHELL "perl -e 'print while <>;'" FOR READ WRITE
Or you can use the
Quote.Shell method
to create a quoted string that won't be modified by the shell.
Examples
' Get the contents of a directory
SHELL "ls -la /tmp" WAIT
' Get the contents of a directory to a string
DIM Result AS String
SHELL "ls -la /tmp" TO Result
' Get the contents of a directory in background
DIM Result AS String
SHELL "ls -la /tmp" FOR READ
...
PUBLIC SUB Process_Read()
DIM sLine AS String
READ #LAST, sLine, -256
Result = Result & sLine
PRINT sLine;
END
 |
If you want to know how many bytes you can read in a Process_Read event handler, use the Lof function.
|
 |
Unlike the VB Shell command, which returns a process ID and relies on the programmer to make API calls to control the process, the Gambas Shell function optionally returns a Process objeto (if used as an assignment to a variable declared AS Process) which can be used to directly kill or otherwise control the spawned process. Additionally, the process may be run synchronously or asynchronously, in contrast to the VB equivalent.
|