Introduction to Running External Programs
does many things well, there are some things that are inevitably better done by external programs. You can use Mathematica
to control the external programs, or to analyze output they generate.
On almost all computer systems, it is possible to run external programs directly from within Mathematica
communicates with the external programs through interprocess communication mechanisms such as pipes.
In the simplest cases, the only communication you need is to send and receive plain text. You can prepare input in Mathematica
, then give it as the standard input for the external program. Or you can take the standard output of the external program, and use it as input to Mathematica
In general, Mathematica
allows you to treat streams of data exchanged with external programs just like files. In place of a file name, you give the external command to run, prefaced by an exclamation point.
|<<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|
|run an external command, and read in a list of the numbers it produces|
Some ways to communicate with external programs.
This feeds the expression x^2+y^2
as input to the external command lpr
, which, on a typical Berkeley Unix system, sends output to a printer.
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.
This runs the external command squares4
, then reads numbers from the output it produces.