从命令行调用 Mathematica 程序,使用命令行参数、标准输入、标准输出和标准错误 [英] Call a Mathematica program from the command line, with command-line args, stdin, stdout, and stderr
问题描述
如果您在 foo.m 中有 Mathematica 代码,则可以使用 -noprompt
调用 Mathematica并使用 -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 Scripting Hack -- 会这样做.
MASH -- Mathematica Scripting Hack -- will do this.
从 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 程序,使用命令行参数、标准输入、标准输出和标准错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!