4
$\begingroup$

Here is my shell script so far (I am using MacOSX):

#!/bin/sh
math="/Applications/Mathematica.app/Contents/MacOS/MathKernel"
$math -run "<<input" -script script1.wl script2.wl > output

The two scripts I want to run in a row are script1.wl and script2.wl. The above seems to only run script1.wl

Anything obvious I am missing ?

$\endgroup$
5
  • $\begingroup$ I've never used the -script option. What happens if you simply use $math -run "<<input;<<script1.wl;<< script2.wl" > output? $\endgroup$
    – Jason B.
    Mar 15, 2017 at 16:32
  • $\begingroup$ If you want to run them with the same kernel, why don't you run them from within Mathematica (Get) instead of from within a shell script? $\endgroup$
    – Szabolcs
    Mar 15, 2017 at 16:43
  • $\begingroup$ @Szabolcs - sometimes you just don't want to be burdened with a front end :-) $\endgroup$
    – Jason B.
    Mar 15, 2017 at 16:54
  • $\begingroup$ @JasonB. I am not talking about the front end. I just don't think that it is a good idea to mix Mathematica will shell scripts. I meant: write Mathematica code to read in the two "scripts", not shell code. $\endgroup$
    – Szabolcs
    Mar 15, 2017 at 16:56
  • 1
    $\begingroup$ @JasonB. That's essentially the same what you said. Sorry. I didn't red carefully. $\endgroup$
    – Szabolcs
    Mar 15, 2017 at 16:58

1 Answer 1

2
$\begingroup$

I can't see any syntax on this page for using 2 files with the -script option (and as MB1965 points out, the help does say that only one script file can be used with the -script option). I do have some experience running WL files from the command line in Mathematica in batch mode, all of which I learned from Jens's page here.

If I create a file called "script1.wl" with the contents

x1 = 3;
x2 = 4;

and another file in the same directory, "script2.wl", with

Print[x1 + x2];
Exit[]

and then run this command from the same directory

math -noprompt -run "<<script1.wl;<<script2.wl" > output

where I've set math up as an alias in my shell to the MathKernel application, then the output file has a single line

7
$\endgroup$
1
  • 2
    $\begingroup$ Similarly if you run RunProcess[{$script, "-help"}, "StandardOutput"] it specifies that only one file can be passed. $\endgroup$
    – b3m2a1
    Mar 15, 2017 at 17:18

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.