2009-10-22 76 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];