Call a Mathematica program from the command line, with command-line args, stdin, stdout, and stderr
Asked Answered
B

6

18

If you have Mathematica code in foo.m, Mathematica can be invoked with -noprompt and with -initfile foo.m (or -run "<<foo.m") and the command line arguments are available in $CommandLine (with extra junk in there) but is there a way to just have some mathematica code like

#!/usr/bin/env MathKernel
x = 2+2;
Print[x];
Print["There were ", Length[ARGV], " args passed in on the command line."];
linesFromStdin = readList[];
etc.

and chmod it executable and run it? In other words, how does one use Mathematica like any other scripting language (Perl, Python, Ruby, etc)?

Breaking answered 29/9, 2008 at 9:16 Comment(0)
B
11

MASH -- Mathematica Scripting Hack -- will do this.

Since Mathematica version 6, the following perl script suffices:

http://ai.eecs.umich.edu/people/dreeves/mash/mash.pl

For previous Mathematica versions, a C program is needed:

http://ai.eecs.umich.edu/people/dreeves/mash/pre6

UPDATE: At long last, Mathematica 8 supports this natively with the "-script" command-line option:

http://www.wolfram.com/mathematica/new-in-8/mathematica-shell-scripts/

Breaking answered 29/9, 2008 at 9:16 Comment(0)
S
6

Here is a solution that does not require an additional helper script. You can use the following shebang to directly invoke the Mathematica kernel:

#!/bin/sh
exec <"$0" || exit; read; read; exec /usr/local/bin/math -noprompt "$@" | sed '/^$/d'; exit
(* Mathematica code starts here *)
x = 2+2;
Print[x];

The shebang code skips the first two lines of the script and feeds the rest to the Mathematica kernel as standard input. The sed command drops empty lines produced by the kernel.

This hack is not as versatile as MASH. Because the Mathematica code is read from stdin you cannot use stdin for user input, i.e., the functions Input and InputString do not work.

Scintilla answered 29/9, 2008 at 9:16 Comment(0)
B
5

Assuming you add the Mathematica binaries to the PATH environment variable in ~/.profile,

export PATH=$PATH:/Applications/Mathematica.app/Contents/MacOS

Then you just write this shebang line in your Mathematica scripts.

#!/usr/bin/env MathKernel -script

Now you can dot-slash your scripts.

$ cat hello.ma
#!/usr/bin/env MathKernel -script

Print["Hello World!"]

$ chmod a+x hello.ma
$ ./hello.ma
"Hello World!"

Tested with Mathematica 8.0.

Minor bug: Mathematica surrounds Print[s] with quotes in Windows and Mac OS X, but not Linux. WTF?

Bobbee answered 29/9, 2008 at 9:16 Comment(0)
Y
4

Try
-initfile filename
And put the exit command into your program

Yoakum answered 29/9, 2008 at 9:26 Comment(2)
Thanks, that's an improvement over -run. I updated the question. I have what I think is a more complete solution that I'll post shortly.Breaking
In my mash.pl solution it turns out you do have to use -run, not -initfile. I'm not sure why but the latter causes problems with mathematica scripts that read from files.Breaking
L
2

I found another solution that worked for me.

Save the code in a .m file, then run it like this: MathKernel -noprompt -run “<

This is the link: http://bergmanlab.smith.man.ac.uk/?p=38

Lozoya answered 29/9, 2008 at 9:16 Comment(0)
A
1

For mathematica 7

$ cat test.m
#!/bin/bash
MathKernel -noprompt -run < <( cat $0| sed -e '1,4d' )  | sed '1d'
exit 0
### code start Here ... ###
Print["Hello World!"]
X=7
X*5

Usage:

$ chmod +x test.m

$ ./test.m
"Hello World!"

7
35
Allomorphism answered 29/9, 2008 at 9:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.