BayES BayES

3.9 The system function

Apart from the functions that BayES provides to facilitate communication with other scientific software, the system() function can be used to give access to the operating system’s command line. This function works slightly differently 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 files located in the current working directory, while the statement:

    system("bash myScript.sh");

    will execute the commands contained in the shell script file myScript.sh (assuming that such a file 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:

    system("notepad");

    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 specifiers have the following effect:

    • /C: terminates the shell after the command finishes execution
    • /Q: turns echo off

    A statement like:

    system("myScript.bat");

    will execute the DOS commands contained in the myScript.bat batch file (assuming that such a file 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 file "1System control statements.bsf", located at "$BayESHOME/Samples/6AdvancedUsage" contains an extensive example of using the system() function. Note that this file differs between Microsoft® Windows® and Linux/macOS systems and the BayES installer saves only the relevant file for the respective host system.

Share this content:
Facebook Twitter LinkedIn Email
© 2016–20 Grigorios Emvalomatis