7
$\begingroup$

I am learning how to run a Mathematica m file from command line. I am on windows. My question is: How to make the output display on the screen (the DOS windows) as the script is running? Currently, I have to add Print to show anything. What I'd like, is that all the OUT cells that normally display in notebook, to print to stdout (I do not care much about the actual OUT[n] itself, but just the content of the output).

Here is a simple example of foo.m

---- file foo.m ----------
Integrate[Sin[x],x]
----- end of file -----------

Now I run this as follows

G:\>"C:\Program Files\Wolfram Research\Mathematica\10.1\math.exe" -script foo.m

G:\>

You can see, nothing is displayed. I tried changing foo.m as follows

---- file  foo.m ---------------
SetOptions[ $Output, FormatType -> OutputForm ];
Integrate[Sin[x],x]
--------end of file ---------------

Only when I add an explicit Print around each command will I see the output. Is there a setting one needs to add or some trick to have the command output go to stdout?

Windows 7, 64 bit, Mathematica 10.1


For illustration, this is the same thing I do with Maple. In Maple it actually displays all the input and all the output, which is nice, so I hope the same thing can be done in Mathematica.

Mathematica graphics

The file used above contains the same command as with Mathematica:

-----foo.mpl------
int(sin(x),x);
---- end of file -----
$\endgroup$
7
$\begingroup$

The old way, i.e.,

"C:\Program Files\Wolfram Research\Mathematica\10.1\math.exe" < foo.m > foo.out 

seems to just work still: enter image description here

This was the way to go back in the early 90's, no so-called FrontEnd on Windows DOS . This also worked quite fine for calculations, even (Postscript) graphics. Of course in Unix you could write nice scripts and run them on computers around the world. Quite fun actually.

Sometimes it is also useful to add AppendTo[$Echo, "stdout"] : enter image description here

$\endgroup$
  • 1
    $\begingroup$ And to make it print as the "script" is running, leave out the 2nd redirect. The key here is not to use -script, which as mentioned in the docs, does some funky stuff to redirecting In[] and Out[]. Using -noprompt gets rid of the In/Out if that is desired. $\endgroup$ – bobthechemist Apr 8 '15 at 0:27
  • $\begingroup$ @bobthechemist ok, I see now what the problem was. Using -script "foo.m" was confusing things. I thought one should use -script. It looks from above that just simple "< foo.m" with the AppendTo[$Echo, "stdout"] will solve all the problems. Strange the docs do not show such simple examples for users. Very hard to find such info in documentation. $\endgroup$ – Nasser Apr 8 '15 at 0:48
  • $\begingroup$ @Nasser Yes, the documentation of Mathematica could really be improved. Partially that has happened for 10.1, but the old useful functions and tools (like this one) do not get enough attention from WRI, unfortunately. Too bad. $\endgroup$ – Rolf Mertig Apr 8 '15 at 7:57
  • $\begingroup$ @bobthechemist Thanks for the -noprompt hint. $\endgroup$ – masterxilo Dec 16 '17 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.