Proper way to run a Mathematica script (-script vs -run vs MathematicaScript vs <)?
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$
From my experience and under linux:
For MMA v10, I use:
MathematicaScript -script script_file
For MMA v11, I use:
wolframscript -f script_file