# Mathematica script - passing command line arguments

When using Mathematica script

#!/usr/local/bin/MathematicaScript -script
Print[$ScriptCommandLine]  and running it with some arguments: $./script.m a=6 z=3 d=423 c=43 x=1


I'm getting the following error:

Mathematica could not find its system-wide initialization file "sysinit.m". The following places were searched: . ~ ./Packages ./SystemFiles/KernelInit ./SystemFiles/GraphicsInit

{"./script.m", "a=6", "z=3", "d=423", "c=43", "x=1"}

With fewer number of command line arguments, this and any other script do not causes errors. How can one fix this issue? I'm running Linux 3.1.0-1-amd64 #1 SMP Tue Jan 10 05:01:58 UTC 2012 x86_64, together with Mathematica 8.0.1.0

-
It fails again. I can overtake this by calling math -script script.m {2,3,4,5,6} but this is ugly. The file "sysinit.m" about which Mathematica complains is present in a standard installation directory. –  mmal May 8 '12 at 19:22
I am sorry, you are right, I can reproduce the problem. Removing one command line argument makes the problem disappear. –  Szabolcs May 8 '12 at 19:23
Another strange thing is that when using more than 4 arguments, the first element in $CommandLine (not $ScriptCommandLine) will be shown as "" instead of the full path to MathKernel as it should be. I think this is a bug. –  Szabolcs May 8 '12 at 19:35

Solution (tested on Linux)

Use this as first line of your script:

#!/usr/local/bin/MathematicaScript -runfirst "$TopDirectory=\"/usr/local/Wolfram/Mathematica/8.0\"" -script  If you installed Mathematica in a different directory, you have to adjust the path of $TopDirectory.

How did I debug this?

The first error message is quite clear: the system cannot open the file /SystemFiles/CharacterEncodings/ISO8859-1.m and obviously the system is correct, because this file does not exist in this directory.

You could now use strace to track down what happens (maybe you better redirect the output into a file)

strace -s 128 ./script.m 1 2 3 4 5


Looking into the output you probably stumble over the line

execve(

"/usr/local/Wolfram/Mathematica/8.0/SystemFiles/Kernel/Binaries/Linux-x86-64/MathKernel",
["", "-runfirst", "$TopDirectory=\"/usr/local/Wolfram/Mathematica/8.0\"", "-script", "./script.m", "--", "./script.m", "1", "2", "3", "4", "5"] , [/* 54 vars */]) = 0  You see that basically you script-call is of course just a call to the MathKernel. If you execute this on the command-line, your script runs fine. This seems to suggest, that by providing 5 or more parameters, the setting of the $TopDirectory is somehow removed. Therefore, I tried to set it explicitly in the MathematicaScript-call which seems to work.

-
You can also use MathKernel or just math instead of MathematicaScript. I think the system extracts the installation path from the first item of $CommandLine. If this is missing, it can't find the installation directory and can't find its files. I came up with the same workaround you describe (+1!). Do you agree that this is a bug in MathematicaScript? – Szabolcs May 9 '12 at 8:41 @Szabolcs I agree with Your explanation about the paths. I've just run this (oryginal version - without any modifications) script on a different machine and I didn't get any errors. This suggests that this could be system dependent issue. – mmal May 9 '12 at 9:21 @Szabolcs, yes, this is a bug. Why should the number of arguments be restricted? And when it is, where is it documented? I assume no one tested this at Wolfram. – halirutan May 9 '12 at 11:41 @halirutan Just needed a confirmation from at least one person before adding the tag. – Szabolcs May 9 '12 at 11:57 I get two kinds of errors (different from yours) on Mma 8.0.4 Mac OSX, depending on the number of arguments: (with the appropriate path for Mac OSX) #!/Applications/Mathematica.app/Contents/MacOS/MathematicaScript -script Print[$ScriptCommandLine]


With 5 arguments, I get

and with 7 or more arguments, I get a segfault:

MathematicaScript(14538) malloc: * error for object 0x70000037: pointer being freed was not allocated * set a breakpoint in malloc_error_break to debug Segmentation fault: 11

A workaround is to change MathematicaScript to MathKernel, and use $CommandLine[[3;;]] instead of $ScriptCommandLine.

-
The workaround works for me too, on OS X (+1). –  Jens May 8 '12 at 20:22

I can't reproduce this exactly on win7 mma 8.0.1. $ScriptCommandLine is empty, while $CommandLine returns

{"d:\\Math\\Mathematica\\8.0\\math.exe", "-noprompt", "-script", "./script.m", "a=6", "z=3", "d=423", "c=43", "x=1"}

As a workaround have you thought about using environment variables to pass information to your script? You can retrieve the value of an environment variable with a simple Environment["NAME_OF_VARIABLE"]

-
Environment seems not to be a good solution eg. when passing lists. By the way, the same happens when running: $./script.m {2,3,4,5,5} even when this script is empty. – mmal May 8 '12 at 19:09 I get the same thing both on Windows and Linux when using math.exe or math. $ScriptCommandLine is empty. However, if I use MathematicaScript (available only on Linux), then I get back the actual command line, starting with {"./script.m", ...}. –  Szabolcs May 8 '12 at 19:18
@mmal Why can't list be passed using env. variables? –  Ajasja May 8 '12 at 20:00
@Ajasja I agree, it can be done this way. But I don't think its the best/proper one. –  mmal May 8 '12 at 20:12