### 3.9 The system function

Apart from the functions that BayES provides to facilitate communication with other scientiﬁc software, the system() function can be used to give access to the operating system’s command line. This function works slightly diﬀerently on Microsoft® Windows® and Linux/macOS systems:

1. on Linux and macOS systems the system() function executes the command passed to it as a string. For example, the statement:

system("ls");

will list all folders and ﬁles located in the current working directory, while the statement:

system("bash myScript.sh");

will execute the commands contained in the shell script ﬁle myScript.sh (assuming that such a ﬁle exists in the current directory). In both cases, the output of the command passed to system() will be directed to the BayES console.

2. on Microsoft® Windows® systems the system() function executes the command passed to it as a string, after prepending it with the string "cmd /Q /C ". For example, when the user runs:

system("dir");

the command submitted to the Microsoft® Windows® shell is actually "cmd /Q /C dir". This is done so that the system() function can call both Microsoft® Windows® applications, with a statement like:

as well as Microsoft® Windows® DOS commands, such as the dir command used in the example above.

cmd requests Microsoft® Windows® to start a new shell to execute the command or start the application and the two option speciﬁers have the following eﬀect:

• /C: terminates the shell after the command ﬁnishes execution
• /Q: turns echo oﬀ

A statement like:

system("myScript.bat");

will execute the DOS commands contained in the myScript.bat batch ﬁle (assuming that such a ﬁle exists in the current directory). If the command to be executed contains spaces, then it can be enclosed in double-quote marks. For example:

system(""copy myFile.txt .AnotherFolder"");

In all cases, any output sent to the Microsoft® Windows® console by the program or command will be redirected to the BayES console.

The script ﬁle "1$-$System control statements.bsf", located at "\$BayESHOME/Samples/6$-$AdvancedUsage" contains an extensive example of using the system() function. Note that this ﬁle diﬀers between Microsoft® Windows® and Linux/macOS systems and the BayES installer saves only the relevant ﬁle for the respective host system.