代码之家  ›  专栏  ›  技术社区  ›  Will Robertson

Mathematica在Mac OS X上的命令行中处于批处理模式

  •  6
  • Will Robertson  · 技术社区  · 16 年前

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

    好像是Mathematica can be run from the command line 但是我看不到任何关于在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,但我希望这不会有什么不同。

    3 回复  |  直到 16 年前
        1
  •  3
  •   Community Mohan Dere    9 年前

    /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
  •  2
  •   las3rjock    16 年前

    通过一些实验,我发现 /Applications/Mathematica.app/Contents/MacOS/MathKernel -h 或 --help

        3
  •  0
  •   Community Mohan Dere    9 年前

    感谢Pillsy和Will Robertson提供的MASH plug!以下是相关的StackOverflow问题: Call a Mathematica program from the command line, with command-line args, stdin, stdout, and stderr

    例如,标准打印将打印带引号的字符串——通常不是脚本中需要的字符串。

    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 ,然后进行测试。I’我喜欢以下内容:

    #!/usr/bin/env /path/to/mash.pl
    
    prn["hello"];
    x := 1;
    y = x+1;
    z = y+1;
    prn["y=", y];
    prn["z=", z];