【问题标题】:How do you access ARGV in Mathematica?如何在 Mathematica 中访问 ARGV?
【发布时间】:2011-12-19 20:01:58
【问题描述】:

我想在 Mathematica 中编写命令行脚本,但我似乎找不到类似 Argv[i_Integer] 的函数。 (否则文档非常棒。)

【问题讨论】:

标签: wolfram-mathematica command-line-arguments argv


【解决方案1】:

似乎$ScriptCommandLine 是您需要的变量,假设您使用MathematicaScript 编写脚本。如果您使用mathMathKernel -script,它将是$CommandLine

其他相关讨论:rather old one 和 MMA 中的一个toolbag

【讨论】:

  • 谢谢。不幸的是,MathKernel$ScriptCommandLine 报告为空{},无论提供的实际参数如何。和MathematicaScript 报告Could not find Mathematica installation directory.
  • @mcandre 可执行文件在您的路径上吗?
  • @mcandre .... 你在文档中看到这一行吗:“脚本的第一行必须包含 -script 选项,它必须是 last选项就行了。”
  • @mcandre "如果没有提供 -script 选项,$ScriptCommandLine 会给出一个空列表。"
  • @mcandre 这一切仍然假设您使用的是 MathematicaScript。
【解决方案2】:

我喜欢使用环境变量传递东西:

http://reference.wolfram.com/mathematica/ref/Environment.html

【讨论】:

  • 有帮助,但它比指定命令行标志更手动。
【解决方案3】:

$CommandLine 适用于 Mathematica、MathematicaScript、MathKernel 和数学。

args.ma:

#!/usr/bin/env MathKernel -script

Print[$CommandLine]

例子:

$ ./args.ma a b c
{"MathKernel", "-script", "./args.ma", "a", "b", "c"}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    • 2012-03-05
    • 2011-12-16
    • 2011-02-16
    • 2011-05-02
    • 1970-01-01
    相关资源
    最近更新 更多