从命令行调用Mathematica程序,使用命令行参数,stdin,stdout和stderr [英] Call a Mathematica program from the command line, with command-line args, stdin, stdout, and stderr
问题描述
如果您在foo.m中有Mathematica代码,那么可以使用 -noprompt
和 -initfile foo.m
(或 -run<< foo.m
)
,并且命令行参数可用 $ CommandLine
(在其中有额外的垃圾),但有一种方法,只是有一些数学代码,如
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.
和chmod可执行并运行它?换句话说,如何像任何其他脚本语言(Perl,Python,Ruby等)一样使用Mathematica?
and chmod it executable and run it? In other words, how does one use Mathematica like any other scripting language (Perl, Python, Ruby, etc)?
推荐答案
MASH
自Mathematica版本6开始,以下perl脚本就足够了:
Since Mathematica version 6, the following perl script suffices:
http://ai.eecs.umich.edu/people/ dreeves / mash / mash.pl
对于以前的Mathematica版本,需要一个C程序:
For previous Mathematica versions, a C program is needed:
http://ai.eecs.umich.edu/people/dreeves/ mash / pre6
更新:最后,Mathematica 8原生地支持-script命令行选项:
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/
这篇关于从命令行调用Mathematica程序,使用命令行参数,stdin,stdout和stderr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!