2009-10-22 66 views
6

我想开始为我的Mathematica程序编写一些单元测试,并使用一些Makefiles控制命令行中的所有内容。从Mac OS X上的命令行以批处理模式处理Mathematica

看起来像Mathematica can be run from the command line,但我看不到有关在Mac OS X上做这件事的任何基本说明 - 有没有人做过此事?


更新:

创建一个测试文件是这样的:

 
Print["hello"]; 
x := 1; 
y = x+1; 
z = y+1; 
Print["y="[email protected]]; 
Print["z="[email protected]]; 
Quit[]; 

而且随着

/Applications/Mathematica.app/Contents/MacOS/MathKernel -noprompt < test.m 

运行它是最接近我能得到某种批量处理。虽然输出看起来很丑陋;换行符被添加到脚本的每一行!

 

"hello" 




"y=2" 

"z=3" 

这是我能找到的还能输出信息到控制台输出的脚本的最接近的东西吗?我只使用Mathematica 6,但我希望这不会有所作为。

+0

你看着混搭:HTTP://ai.eecs。 umich.edu/people/dreeves/mash/? – Pillsy 2009-10-22 14:40:43

回答

3

这,最后,给出了输出像我期待它:

/Applications/Mathematica.app/Contents/MacOS/MathKernel -noprompt -run "<<test.m" 

有道理,我想。添加此我.bash_profile可以轻松地执行(如mma test.m):

mma() { /Applications/Mathematica.app/Contents/MacOS/MathKernel -noprompt -run "<<$1" ; } 

dreeves's mash Perl脚本见,这可能提供了这种方法的优点。

2

通过一些实验,我发现/Applications/Mathematica.app/Contents/MacOS/MathKernel可以从命令行启动。但它似乎并不接受通常的-h--help命令行标志。

0

感谢Pillsy和Will Robertson的MASH插件!下面是相关的StackOverflow问题:Call a Mathematica program from the command line, with command-line args, stdin, stdout, and stderr

如果您不使用MASH,则可能需要使用MASH定义的以下实用程序函数。 例如,标准打印将打印带有引号的字符串 - 通常不是您想要的脚本。

ARGV = args = Drop[$CommandLine, 4];   (* Command line args.   *) 
pr = WriteString["stdout", ##]&;    (* More       *) 
prn = pr[##, "\n"]&;       (* convenient     *) 
perr = WriteString["stderr", ##]&;   (* print      *) 
perrn = perr[##, "\n"]&;      (* statements.    *) 
EOF = EndOfFile;        (* I wish mathematica   *) 
eval = ToExpression;       (* weren't so damn    *) 
re = RegularExpression;      (* verbose!     *) 
read[] := InputString[""];     (* Grab a line from stdin.  *) 
doList[f_, test_] :=       (* Accumulate list of what f[] *) 
    [email protected][f[]&, f[], test];  (* returns while test is true. *) 
readList[] := doList[read, #=!=EOF&];  (* Slurp list'o'lines from stdin *) 

要使用MASH,只要抓住是Perl文件,mash.pl,然后让你的test.m类似如下:

#!/usr/bin/env /path/to/mash.pl 

prn["hello"]; 
x := 1; 
y = x+1; 
z = y+1; 
prn["y=", y]; 
prn["z=", z];