Command line Mathematica在Mac OS X上的命令行中处于批处理模式
我想开始为我的Mathematica程序编写一些单元测试,并用一些makefile从命令行控制一切 这看起来像Mathematica,但我看不到任何关于在Mac OS X上开始这样做的基本说明-以前有人这样做过吗Command line Mathematica在Mac OS X上的命令行中处于批处理模式,command-line,macos,wolfram-mathematica,Command Line,Macos,Wolfram Mathematica,我想开始为我的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[]; 打印[“你好”]; x:=1; y=x+1; z=y+1;
更新: 创建如下所示的测试文件: Print["hello"]; x := 1; y = x+1; z = y+1; Print["y="ToString@y]; Print["z="ToString@z]; Quit[]; 打印[“你好”]; x:=1; y=x+1; z=y+1; 打印[“y=”ToString@y]; 打印[“z=”ToString@z]; 退出[]; 并用
/Applications/Mathematica.app/Contents/MacOS/MathKernel -noprompt < test.m
/Applications/Mathematica.app/Contents/MacOS/MathKernel-noprompt
是我能得到的最接近批处理的方法。但是,输出看起来很糟糕;为脚本的每一行添加新行
"hello"
"y=2"
"z=3"
“你好”
“y=2”
“z=3”
这是我能得到的最接近于仍然可以将信息输出到控制台输出的脚本的东西吗?我只使用Mathematica 6,但我希望不会有什么不同。通过一些实验,我发现可以从命令行启动
/Applications/Mathematica.app/Contents/MacOS/mathekernel
。但是,它似乎不接受通常的-h
或-help
命令行标志。最后,它给出了我期望的输出:
/Applications/Mathematica.app/Contents/MacOS/MathKernel -noprompt -run "<<test.m"
另请参阅Perl脚本,它可能比这种方法更具优势。多亏了Pillsy和Will Robertson提供的MASH插件!以下是相关的StackOverflow问题: 如果不使用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文件,然后使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];
你看过马什吗?
#!/usr/bin/env /path/to/mash.pl
prn["hello"];
x := 1;
y = x+1;
z = y+1;
prn["y=", y];
prn["z=", z];