Mathematica 教程 函数 »|教程 »

# 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.
 Structured communication use MathLink to exchange expressions with MathLink-compatible external programs Unstructured communication use file reading and writing operations to exchange ordinary text

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 "MathLink 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.
 <>"!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

Some ways to communicate with 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.
This executes the external command echo\$TERM, then reads the result as Mathematica input.
 Out[2]=
 With a text-based interface, putting ! at the beginning of a line causes the remainder of the line to be executed as an external command. squares is an external program which prints numbers and their squares. `In[1]:= !squares 41 12 43 94 16`
This runs the external command squares 4, then reads numbers from the output it produces.
 Out[3]=
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.
 OpenWrite[] open a new file with a unique name in the default area for temporary files on your computer system

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.
 Run["command",arg1,...] run an external command from within Mathematica

Running external commands without input or output.

This executes the external Unix command date. The returned value is an "exit code" from the operating system.
 Out[4]=
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 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.
 RunThrough["command",expr] run command, using expr as input, and reading the output back into Mathematica

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.
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.
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.
 Out[5]=
 SystemOpen["target"] opens the specified file, URL or other target with the associated program on your computer system

Opening files with external programs.

 This opens the URL using your system's preferred web browser.
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.