Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you access ARGV in Mathematica?

I'd like to write command line scripts in Mathematica, but I can't seem to find an Argv[i_Integer] like function. (The docs are FANTASTIC otherwise.)

like image 761
mcandre Avatar asked Nov 01 '11 20:11

mcandre


3 Answers

It seems that $ScriptCommandLine is the variable you would need for this, assuming you're using MathematicaScript for scripting. If you use math or MathKernel -script it would be $CommandLine.

Other relevant discussions: a rather old one and one in the MMA toolbag.

like image 73
Sjoerd C. de Vries Avatar answered Nov 16 '22 06:11

Sjoerd C. de Vries


$CommandLine works for Mathematica, MathematicaScript, MathKernel, and math.

args.ma:

#!/usr/bin/env MathKernel -script

Print[$CommandLine]

Example:

$ ./args.ma a b c
{"MathKernel", "-script", "./args.ma", "a", "b", "c"}
like image 25
mcandre Avatar answered Nov 16 '22 06:11

mcandre


I'm fond of passing things in using environment variables:

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

like image 1
Joshua Martell Avatar answered Nov 16 '22 06:11

Joshua Martell