20
$\begingroup$

In Mathematica 11, there is new wolframscript.exe

According to help wolframscript.exe -h

-f [ -file ] arg---------Run a Wolfram Language file from the commandline.

-script arg------------Run Wolfram script, backwards compatible.

I created a test.wl file with content below

#!wolframscript
SetDirectory[DirectoryName[$InputFileName]];
Print[Directory[]]

Running wolframscript -script test.wl is fine.

But running wolframscript -f test.wl will give errors

SetDirectory::fstr: File specification is not a string of one or more characters.

Why the error? What is the difference between "wolframscript -f" and "wolframscript -script"?

$\endgroup$

1 Answer 1

14
$\begingroup$

The -script mode is an interface to the wolfram.exe's -script mode, which is a command-line version of Get. It essentially rewrites the command line, launches a kernel using the equivalent command, and then exits. It can only ever be used with a local kernel, and $ScriptInputString will always be None.

In -file mode, wolframscript.exe launches a slave kernel (either local or cloud, depending on options) and then handles the buffering between the various streams to inputs and outputs end up in the correct place. It reads in the commands from test.wl and then feeds them to the kernel. This allows it to not only provide $ScriptInputString, but provide it differently in -linewise mode.

So as you can see, the two modes are quite different.

As for the particular difference you spotted here, the behavior is technically correct. Since kernel is not in the middle of Get when it evaluates the contents of the file, \$InputFileName is an empty string. Compare, for example, the behavior of 'wolfram.exe < test.wl' versus 'wolfram.exe -script test.wl'. Morever, if you're using a cloud kernel, does this \$InputFileName even make any sense, and should it be available?

OTOH, this is a very technical and somewhat confusing distinction (which I had to think through before writing this answer), so I will file a suggestion so we investigate whether the situation can be improved.

$\endgroup$

Your Answer

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

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