I have a physics simulation program which I run through a terminal window (I use a Mac) as shown below:

user$ programName inputfile.ele

I would like run a *.sh script that runs the simulation from Mathematica. I tried using Run but had no luck, though I was able to successfully follow through this example (How to run a shell script from inside Mathematica).

I am looking for advice on how to go about running the simulation from Mathematica. Ultimately, I would like to create a Do-loop and have Mathematica run the following script with different t2 values:

   t2 = i;
   t2scan = Table[t2, {71}];
   t3scan = Range[0.01, 0.08, 0.001];
   t2plus3 = t2scan + t3scan;
   ans = 
       NSolve[{SimpleEquation1 == 0, SimpleEquation2 == 0.03} /. t3 -> angle, 
       {f, g}], {angle, t3scan}];
   fdrift = Flatten[f /. ans];
   gdrift = Flatten[g /. ans];
   Export["/Users/Desktop/t2.txt", t2scan];
   Export["/Users/t3.txt", t3scan];
   Export["/Users/t2plus3.txt", t2plus3];
   Export["/Users/f.txt", fdrift];
   Export["/Users/g.txt", gdrift];
   Run["sh myScript.sh"]
   , {i, 0.01, 0.08, 0.001}]

Any help would be greatly appreciated.

  • $\begingroup$ What do you get as a return when you execute the Run command, 0 or some other number? $\endgroup$
    – Jason B.
    Jun 25, 2016 at 13:20
  • $\begingroup$ Yes! I get "32512" $\endgroup$ Jun 25, 2016 at 18:13
  • $\begingroup$ okay - so that means you get an error message of some kind, it returns 0 when it runs correctly. Perhaps its a directory issue? Is the script myScript.sh in the same directory that Mathematica is running from? If you type Directory[] you see what MMA's working directory is. If you go to a terminal and cd into that directory and type your sh command, does it work from there? $\endgroup$
    – Jason B.
    Jun 25, 2016 at 18:17
  • $\begingroup$ I dont think its a directory issue; I SetDirectory[] to all the same folder. I figured the simulation runs from any directory because its part of the "PATH" variables. Maybe Mathematica doesn't recognize the simulation as a variable because its variables/environment is set differently than my terminal? $\endgroup$ Jun 25, 2016 at 18:31
  • 1
    $\begingroup$ It's frustrating that you can't see the error message it gets from the terminal. If you add " > output.txt" to the end of the command does it pit any info in that file? $\endgroup$
    – Jason B.
    Jun 25, 2016 at 18:42

1 Answer 1


Essentially, two things needed to be done and amended in my script:

  1. The exact path location of the simulation needed to clarified. So, instead of writing:

     programName inputfile.ele

I had to put:

     /User/location/somewhere/programName inputfile.ele
  1. Secondly, by putting a generic output I was able to more clearly see exactly what the problem is:

     programName inputfile.ele>output.txt

My particularly issue is that my simulation requires a rpn definition file which is normally set as a path/variable in my terminal but apparently Mathematica does not recognize it. And wallah, after setting the rpn definition file in my script I could run the simulation program easily through Mathematica!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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