2009-10-22 17 views
6

Mathematicaプログラムの単体テストを書き始め、コマンドラインからいくつかのMakefileを使ってすべてを制御したいと思う。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

答えて

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

MASHプラグのPillsyとWill Robertsonに感謝します!関連するStackOverflowの質問は次のとおりです。Call a Mathematica program from the command line, with command-line args, stdin, stdout, and stderr

MASHを使用しない場合は、MASHで定義されている次のユーティリティ関数を使用できます。 たとえば、標準のPrintでは、文字列を引用符で印刷します。通常は、スクリプトでは必要ではありません。

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]; 
関連する問題