6

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

似乎 Mathematica可以从命令行运行,但我看不到任何关于在 Mac OS X 上开始执行此操作的基本说明——以前有人做过吗?


更新:

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

打印[“你好”];
x := 1;
y = x+1;
z = y+1;
打印["y="ToString@y];
打印["z="ToString@z];
退出[];

并运行它

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

是最接近某种批处理的方法。不过,输出看起来很难看;脚本的每一行都添加了换行符!

“你好”




“y=2”

“z=3”

这是我能得到的最接近仍然可以将信息输出到控制台输出的脚本吗?我只使用 Mathematica 6,但我希望这不会产生影响。

4

3 回答 3

3

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

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

有道理,我想。将此添加到 my.bash_profile允许轻松执行(如 中mma test.m):

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

另请参阅dreeves 的mashPerl 脚本,它可能比这种方法更有优势。

于 2009-10-22T05:54:41.713 回答
2

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

于 2009-10-22T02:25:37.533 回答
0

Thanks to Pillsy and Will Robertson for the MASH plug! Here's the relevant StackOverflow question: Call a Mathematica program from the command line, with command-line args, stdin, stdout, and stderr

If you don't use MASH, you may want to use the following utility functions that MASH defines. For example, the standard Print will print strings with quotation marks -- not usually what you want in scripts.

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 *)

To use MASH, just grab that perl file, mash.pl, and then make your test.m like the following:

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

prn["hello"];
x := 1;
y = x+1;
z = y+1;
prn["y=", y];
prn["z=", z];
于 2010-04-17T16:04:46.133 回答