2.11.2 External Programs
On most computer systems, you can execute external programs or commands from within Mathematica. Often you will want to take expressions you have generated in Mathematica, and send them to an external program, or take results from external programs, and read them into Mathematica.
Mathematica supports two basic forms of communication with external programs: structured and unstructured.
Two kinds of communication with external programs in Mathematica.
The idea of structured communication is to exchange complete Mathematica expressions to external programs which are specially set up to handle such objects. The basis for structured communication is the MathLink system, discussed in Section 2.12.
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.
Reading and writing to external programs.
In general, wherever you might use an ordinary file name, Mathematica allows you instead to give a pipe, written as an external command, prefaced by an exclamation point. When you use the pipe, Mathematica will execute the external command, and send or receive text from it.
This sends the result from FactorInteger to the external program lpr. On many Unix systems, this program generates a printout.
In:= FactorInteger[2^31 - 1] >> !lpr
This executes the external command echo$TERM, then reads the result as Mathematica input.
In:= <<"!echo $TERM"
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 Mathematica provide a very general mechanism for unstructured communication with external programs. On many computer systems, Mathematica 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 Mathematica 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 MathLink.
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.
Opening a temporary file.
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 Mathematica. You can do this using the Mathematica function Run.
This executes the external Unix command date. The returned value is an "exit code" from the operating system.
Running external commands without input or output.
Thu Mar 13 18:23:13 CST 1997
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 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 Mathematica itself. In some cases, this may be what you want. But particularly if you are using Mathematica with a front end, this can cause considerable trouble.
Shell escapes in Mathematica.
If you use Mathematica with a text-based interface, there is usually a special mechanism for executing external commands. With such an interface, Mathematica takes any line of input that starts with an exclamation point, and executes the text on the remainder of the line as an external command.
This line is taken as a "shell escape", and executes the Unix command date.
The way Mathematica uses !command is typical of the way "shell escapes" work in programs running under the Unix operating system. In most versions of Mathematica, you will be able to start an interactive shell from Mathematica simply by typing a single exclamation point on its own on a line.
Thu Mar 13 18:23:13 CST 1997
Running Mathematica expressions through external programs.
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.
This feeds the expression 789 to the external program cat, which in this case simply echoes the text of the expression. The output from cat is then read back into Mathematica.
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 Mathematica. Note that in RunThrough, like Run, you should not preface the names of external commands with exclamation points.
In:= RunThrough["cat", 789]