Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
525 views
in Technique[技术] by (71.8m points)

scripting - Call a Mathematica program from the command line, with command-line args, stdin, stdout, and stderr

If you have Mathematica code in foo.m, Mathematica can be invoked with -noprompt and with -initfile foo.m (or -run "<<foo.m") and the command line arguments are available in $CommandLine (with extra junk in there) but is there a way to just have some mathematica code like

#!/usr/bin/env MathKernel
x = 2+2;
Print[x];
Print["There were ", Length[ARGV], " args passed in on the command line."];
linesFromStdin = readList[];
etc.

and chmod it executable and run it? In other words, how does one use Mathematica like any other scripting language (Perl, Python, Ruby, etc)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

MASH -- Mathematica Scripting Hack -- will do this.

Since Mathematica version 6, the following perl script suffices:

http://ai.eecs.umich.edu/people/dreeves/mash/mash.pl

For previous Mathematica versions, a C program is needed:

http://ai.eecs.umich.edu/people/dreeves/mash/pre6

UPDATE: At long last, Mathematica 8 supports this natively with the "-script" command-line option:

http://www.wolfram.com/mathematica/new-in-8/mathematica-shell-scripts/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...