16
$\begingroup$

Suppose I have a Mathematica script called test.m. There at least 4 ways to run this from the terminal:

math -script test.m

math -run < test.m

math < test.m

MathematicaScript test.m

math -noprompt -run "<<test.m"

What are the differences between these? What is the recommended way to run a Mathematica script (that is, I would like to get a behavior similar to the one you get with a python script by running python file.py)

$\endgroup$
12
$\begingroup$

I'm not sure on which operating system you are and whether this makes a difference, but your 4 choice don't do the same.

math -run file.m

edit:

When you change this command to math -run < file.m then it does the same as the next (wrong) alternative.


Doesn't do anything with the content of file.m at all, because the -run option expects a command like math -run Print[2]. Additionally, the command is not ran in batch mode like you would expect it from a script. Instead, an interactive command-line Mathematica session is opened. This is the same (although the session is quit instantly) for

math < file.m

where the content of file.m is executed, but file.m is not considered to be a script. You can easily see this when you create a script file with she-bang like

#!/usr/local/bin/MathematicaScript -script

Print["Hello"]

the the output of math < file.m looks pretty awesome:

Mathematica 10.0 for Linux x86 (64-bit)
Copyright 1988-2014 Wolfram Research, Inc.

In[1]:= 
                                #1!
Out[1]= -script + -------------------------------
                  bin local MathematicaScript usr

In[2]:= In[2]:= Hello

In[3]:= 

Your last alternative seems to miss the -script option which is (as stupid as this may sound) required, even for the MathematicaScript command. Therefore, I need to run

WolframScript -script file.m

or I get an error

error: Need to provide the -script option. Usage: -script

which is pretty lame.

If I had to give a suggestion, I would tell you to always include the she-bang in your file and make the file executable with chmod +x. Then you can simply call

patrick@lenerd:~/tmp$ ./file.m 
    Hello
    patrick@lenerd:~/tmp$
$\endgroup$
  • $\begingroup$ I had forgotten a < in my second option (the one with -run). Taking into account your answer, I added a 5th option also using -run. Please see the edits in my question. $\endgroup$ – becko Oct 9 '14 at 23:38
  • $\begingroup$ With the additional redirect (<) you get the same output as in math < file.m, because the she-bang is not valid Mathematica syntax (but usually required). $\endgroup$ – halirutan Oct 9 '14 at 23:40
2
$\begingroup$

From my experience and under linux:

For MMA v10, I use:

MathematicaScript -script script_file

For MMA v11, I use:

wolframscript -f script_file
$\endgroup$
1
$\begingroup$

For the sake of a more condensed and current reply (Mathematica 11) I write my answer.

I think you should make use of the script option which is built to be used in the command line. For that, you have to have a file (named file) that would look like this:

#!/usr/local/Wolfram/Mathematica/11.3/Executables/wolframscript -script (*Default installation place for Mathematica 11*)
(* Your Code *)
Print["Hello World"];

file doesnt have to be a .m. Assuming you are in Linux it can just be named file and given executable privileges by running the command chmod +x file.

You run your mathematica script in the command line with

./file 

It can take commandline arguments as well as shown here. Note: if your command arguments are not all numbers don't use the ToExpression part in vars. By default the rest of the var definition will return your arguments into a list of strings, and you can convert the desired argument to an expression as needed in your code.

Hope it helped. When I was trying to create my own script I would have liked a direct short answer like this.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.