Table of Contents

21.2 Instructions SHELL and EXEC

The following sections present and explain the syntax for the two instructions SHELL and EXEC and provide comparative considerations for these instructions.

21.2.1 Syntax SHELL

There are two variants for the SHELL instruction - a version with many different options and a simple variant to start a process:

[ ProzessVariable = ] SHELL Command [WAIT] [FOR {{READ|INPUT}|{WRITE|OUTPUT}}] [AS PEventName]

Notes:

The following calls are correct:

SHELL "firefox www.gambas-buch.de"
SHELL "dmesg | grep ttyS | grep 00: > /tmp/schnittstellenliste.liste" WAIT
SHELL "dmesg | grep ttyUSB >> /tmp/schnittstellenliste.liste" WAIT

Private $hProcess As Process

aCommand = ["ping", "www.fewo-kellermann.de", "-c", "4"]
$hProcess = EXEC aCommand For Read As "myPingProcess"

aCommand = ["wget", "www.gambas-buch.de", "-O-", "--directory-prefix=" & User.Home]
$hProcess = EXEC aCommand For Read As "myProcess"

sCommand = "mysql -f -n -vvv -u root -pYourMySQLPassword4MySQLRoot"
$hProcess = SHELL sCommand For Input Output As "myProcess"

The 2nd syntax (quick syntax) is simple:

SHELL sCommand TO String-Variable   #   sCommand ist ein String

The output of the process started in the background is stored in the (string) variable.

Attention: You have no control over the blocking executed process! If the process could not be started, then you can catch this error.

21.2.2 Syntax EXEC

There are also two variants for the EXEC instruction to start a process:

[ ProzessVariable = ] EXEC Command [WAIT] [FOR {{READ|INPUT}|{WRITE|OUTPUT}}] [AS PEventName]

The following calls are correct:

Private $hCommand As Process
$hCommand = Exec [...] For Read Write As "MyCommand"

Second variant of the EXEC instruction:

EXEC aCommand TO String-Variable   #   aCommand ist ein String-Array

If you use the second syntax, the specified Exec command is executed and the interpreter waits for it to finish. Afterwards, the complete output of the process started in the background is written into the specified string variable. Also in this case, the following applies with all consequences: You have no control over the executed process!

21.2.3 Syntax extension - environment variables

You can set environment variables selected for the process to be started to new values by specifying the keyword WITH after the shell or Exec command Cmd, which is followed by an array of environment variable value pairs with Environment:

[Process=] EXEC Cmd WITH Environment …
[Process=] EXEC Cmd WITH Env_Array [WAIT] [FOR {{READ|INPUT}|{WRITE|OUTPUT}}] [AS PEventName]
[Process=] SHELL Cmd WITH Environment …
[Process=] SHELL Cmd WITH Env_Array [WAIT] [FOR {{READ|INPUT}|{WRITE|OUTPUT}}] [AS PEventName]

This call in a console :

hans@linux:~$ LC_ALL=en_GB.utf8 gambas3

can be replicated in a Gambas program to use the SHELL instruction or the EXEC instruction to start Gambas 3 temporarily in the English version, for example:

Public Sub btnStartGambas3EN_Click()
 SHELL "gambas3" With ["LC_ALL=en_GB.utf8"] Wait
 EXEC ["gambas3"] With ["LC_ALL=de_DE.utf8"] Wait ' Alternative: EXEC-Instruktion
End

21.2.4 Comparative considerations of the SHELL and EXEC instructions

Whether you use the SHELL instruction or the EXEC instruction is determined primarily by the task at hand. First, differences between the SHELL and EXEC instructions are described and then similarities are highlighted. This will help you decide more confidently whether to use the SHELL instruction or the EXEC instruction to work successfully.

21.2.4.1 Differences SHELL - EXEC

There is a significant difference in the way the command passed (Command) is executed and in the type of command:

The advantages that are available in a shell speak for the use of the SHELL instruction:

Quoting

Starting with Gambas 3, you can use the Shell$(shell command string) function to implement a safe passing to the shell so that certain characters in the shell command string are not interpreted by the shell used - in Ubuntu this is '/bin/dash'. For EXEC, masking of selected characters is not necessary. Under the following URL you will find well-prepared information on quoting: http://wiki.ubuntuusers.de/Shell/Bash-Skripting-Guide_für_Anfänger .

21.2.4.2 Similarities SHELL - EXEC

The SHELL instruction and the EXEC instruction both start an external programme - a process is created. Essential properties, methods and events as well as selected constants of the class 'Process' have been described in chapter 21.1. In the following chapter 21.3, the use of the event handlers PEventName_Read(), PEventName_Kill() and PEventName_Error(..) are described first. In order to pass inputs to the process, corresponding procedures are presented for this purpose. These descriptions apply to the two instructions SHELL and EXEC in the same way.