Wolfram ResearchPRODUCTSPURCHASEFOR USERSCOMPANYOUR SITES
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.
Previous section-----Next section

1.11.10 Running External Programs

Although Mathematica 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. 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
ReadList["!command", Number] 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.

In[1]:=  x^2 + y^2 >> "!lpr"

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[2]:=  !squares 4


This runs the external command squares 4, then reads numbers from the output it produces.

In[3]:=  ReadList["!squares 4", Number, RecordLists->True]

Out[3]=


Any questions about topics on this page? Click here to get an individual response.Buy NowFree TrialMore Information



 © 2009 Wolfram Research, Inc.  Terms of Use  Privacy Policy |
Sign up for our newsletter: