5
$\begingroup$

With the 10.4 version of WolframScript, I could run something like

$ WolframScript -run <CODE> -script /path/to/script.wl [ARGS...]

...where <CODE> stands for some arbitrary WL expression.

The command above would evaluate <CODE>, and then run /path/to/script.wl.

This functionality was broken with version 11.

For example, if simple_script.wl consists of a single line:

Echo[First @ $Path];

...then, with version 10.4 I get results consistent with the description given earlier, namely:

$ WolframScript -run '$Path=DeleteDuplicates @ Prepend[$Path, "/my/mma/lib"];' \
    -script simple_script.wl
>> /my/mma/lib

...but with version 11.1, the same command-line above results in

Wolfram Language 11.1.0 Engine for Linux x86 (64-bit)
Copyright 1988-2017 Wolfram Research, Inc.

ToExpression::sntx: Invalid syntax in or before "= ".
                                                  ^

In[1]:= 
Quit
sh: 1: -script: not found

I.e., with version 11.1, the command-line above

  1. runs into a syntax error (who knows why);
  2. starts an interactive session (in text-mode);
  3. never executes simple_script.wl; in fact, there appears to be an attempt (by what?) to execute, as a subsequent shell command, the tail of the original command-line starting with -script.

This loss of functionality breaks a couple dozen analysis pipelines (many man-months worth of work), so I'd like to find a reasonable work-around that does not require retooling all this code.

Is there a way to recover the functionality described above with version 11.1 of WolframScript?


P.S. I'm aware of the new -code flag, but the latest version of WolframScript recognizes only the first of -code and -script it finds on the command-line, not both.

$\endgroup$
  • 2
    $\begingroup$ Apparently -run option is no longer supported in WolframScript, I will report it as suggestion to wolframscript team to consider it supporting in near future versions. $\endgroup$ – dsingh Jul 2 '17 at 4:36
5
$\begingroup$

This is an execution mode I don't think we encountered in internal testing for the new wolframscript, the thought being that such processing would occur early in the script, an any needed values would be passed in as arguments. I'm not 100% sure what's happening here--I should think that it would fall back to legacy mode and support this. @dsingh has already filed a bug, and I'll let the relevant developer investigate and see if we can fix it.

Here are some workarounds. Instead of calling WolframScript, just call wolfram. If you don't access $ScriptCommandLine, just replacing WolframScript with wolfram will do everything you need:

wolfram -run '$Path=DeleteDuplicates @ Prepend[$Path, "/my/mma/lib"];' \
    -script simple_script.wl

If you do access $ScriptCommandLine, you'll need to simulate it by inserting a double dash and repeating the script name after it, like this:

wolfram -run '$Path=DeleteDuplicates @ Prepend[$Path, "/my/mma/lib"];' \
    -script simple_script.wl -- simple_script.wl ARG1 ARG2 ...

If you still have Mathematica 10.4 installed you can use its version of WolframScript with 11.1. Copy 10.4's version of WolframScript, found in $InstallationDirectory/SystemFiles/Kernel/Binaries/Linux-x86-64, to the corresponding directory in your 11.1 install. If you now use this version of WolframScript (by using the full path or creating a symbolic link) it will behave exactly as in previous versions.

$\endgroup$
  • $\begingroup$ Thanks! Regarding "...any needed values would be passed in as arguments", keep in mind that, in the Unix world, environment variables are a time-honored second way to pass values to programs. In fact, as the example I gave may hint at, we've been using the -run flag to support a functionality that is supported by pretty much every other scripting platform out there, namely an environment variable that specifies directories to be added to the search path for modules. ... $\endgroup$ – kjo Jul 5 '17 at 12:51
  • $\begingroup$ ... E.g. for Python, it's PYTHONPATH; for Perl, it's PERL5LIB; for MATLAB, it's MATLABPATH; etc. If WolframScript supports such an environment variable, please let me know. $\endgroup$ – kjo Jul 5 '17 at 12:51
  • $\begingroup$ Off the top of my head, I don't think we have such a thing, but this doesn't have to be done inside of -run. You can use GetEnvironment[] inside the script to get it. Or, if you want all scripts to look for a particular variable, add the following to /usr/share/Mathematica/Kernel/init.m: If[$ScriptCommandLine =!= {}, AppendTo[$Path, GetEnivronment[var]] $\endgroup$ – Itai Seggev Jul 7 '17 at 23:59
  • $\begingroup$ Certainly, but it is easier on our users for us to give them a wrapper script (around WolframScript, e.g.) that implements the desired behavior (through -run) than to ask them to modify their init.m files. $\endgroup$ – kjo Jul 10 '17 at 11:45

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.