我想开始为我的Mathematica程序编写一些单元测试,并从命令行控制一些Makefile。

似乎Mathematica 可以从命令行运行但我可以在Mac OS X上看到关于开始这样做的任何基本说明—有没有人这样做过?


更新:

像这样创建一个测试文件:

Print["hello"];
x := 1;
y = x+1;
z = y+1;
Print["y="ToString@y];
Print["z="ToString@z];
Quit[];

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

是我能够进行某种批处理的最接近的。但输出看起来很丑陋;为脚本的每一行添加换行符!

"hello"




"y=2"

"z=3"

这是我最接近的脚本,仍然可以输出信息到控制台输出?我只使用Mathematica 6,但我希望这没有什么区别。

有帮助吗?

解决方案

最后,这给出了我希望的输出:

/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的 mash Perl脚本,可能比这种方法更具优势。

其他提示

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

感谢Pillsy和Will Robertson的MASH插头!以下是相关的StackOverflow问题:从命令行调用Mathematica程序,使用命令行参数,stdin ,stdout和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[]   *)
  Most@NestWhileList[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];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top