Home / lang / shell 
SHELL
Syntax
[ Process = ] SHELL Command [ WAIT ] [ FOR { { READ | INPUT } | { WRITE | OUTPUT } } ] [ AS Name ]
SHELL Command TO Variable

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

The command is a string containing a command passed to the system shell (/bin/sh).

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 content of a directory

SHELL "ls -la /tmp" WAIT

' Same thing, but in background

DIM Content AS String

EXEC "ls -la /tmp" FOR READ

...

PUBLIC SUB Process_Read()

  DIM sLine AS String

  READ #LAST, sLine, -256

  Content = Content & 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.

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.

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 object (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.

See also

Process Management  Process  Lof  Quote.Shell