On most computer systems, you can execute external programs or commands from within the Wolfram Language. Often you will want to take expressions you have generated in the Wolfram Language, and send them to an external program, or take results from external programs, and read them into the Wolfram Language.
|Structured communication||use WSTP to exchange expressions with WSTP‐compatible external programs|
|Unstructured communication||use file reading and writing operations to exchange ordinary text|
The idea of structured communication is to exchange complete Wolfram Language expressions to external programs which are specially set up to handle such objects. The basis for structured communication is the Wolfram Symbolic Transfer Protocol (WSTP) system, discussed in "WSTP and External Program Communication".
Unstructured communication consists in sending and receiving ordinary text from external programs. The basic idea is to treat an external program very much like a file, and to support the same kinds of reading and writing operations.
|<<file||read in a file|
|<<"!command"||run an external command, and read in the output it produces|
|expr>>"!command"||feed the textual form of expr to an external command|
|ReadList["!command",Number]||run an external command, and read in a list of the numbers it produces|
In general, wherever you might use an ordinary file name, the Wolfram Language allows you instead to give a pipe, written as an external command, prefaced by an exclamation point. When you use the pipe, the Wolfram Language will execute the external command, and send or receive text from it.
In:= !squares 4
One point to notice is that you can get away with dropping the double quotes around the name of a pipe on the right‐hand side of << or >> if the name does not contain any spaces or other special characters.
Pipes in the Wolfram Language provide a very general mechanism for unstructured communication with external programs. On many computer systems, Wolfram Language pipes are implemented using pipe mechanisms in the underlying operating system; in some cases, however, other interprocess communication mechanisms are used. One restriction of unstructured communication in the Wolfram Language is that a given pipe can only be used for input or for output, and not for both at the same time. In order to do genuine two‐way communication, you need to use WSTP.
Even with unstructured communication, you can nevertheless set up somewhat more complicated arrangements by using "temporary files". The basic idea is to write data to a file, then to read it as needed.
|OpenWrite||open a new file with a unique name in the default area for temporary files on your computer system|
Particularly when you work with temporary files, you may find it useful to be able to execute external commands which do not explicitly send or receive data from the Wolfram Language. You can do this using the Wolfram Language function Run.
|Run["command",arg1,…]||run an external command from within the Wolfram Language|
Note that when you use Run, you must not preface commands with exclamation points. Run simply takes the textual forms of the arguments you specify, then joins them together with spaces in between, and executes the resulting string as an external shell command.
It is important to realize that Run never "captures" any of the output from an external command. As a result, where this output goes is purely determined by your operating system. Similarly, Run does not supply input to external commands. This means that the commands can get input through any mechanism provided by your operating system. Sometimes external commands may be able to access the same input and output streams that are used by the Wolfram Language itself. In some cases, this may be what you want. But particularly if you are using the Wolfram Language with a front end, this can cause considerable trouble.
|RunThrough["command",expr]||run command, using expr as input, and reading the output back into the Wolfram Language|
As discussed above, << and >> cannot be used to both send and receive data from an external program at the same time. Nevertheless, by using temporary files, you can effectively both send and receive data from an external program while still using unstructured communication.
The function RunThrough writes the text of an expression to a temporary file, then feeds this file as input to an external program, and captures the output as input to the Wolfram Language. Note that in RunThrough, like Run, you should not preface the names of external commands with exclamation points.
|SystemOpen["target"]||opens the specified file, URL or other target with the associated program on your computer system|
SystemOpen uses settings in your operating system to determine how to open a URI or file. When opening files, it typically uses the same program that would be used if you double-clicked the file's icon.