I am looking for a way to passing arguments into a WL script when WL run in batch modes. The test script is:

#!/usr/bin/env wolframscript

(*generate "num" samples of a mixed distribution*)
num = ToExpression[$ScriptCommandLine];
Print["num ", num];
Print /@ RandomVariate[
  MixtureDistribution[{1, 2}, {NormalDistribution[1, 0.2], 
    NormalDistribution[3, 0.1]}], num, WorkingPrecision -> 50]

The output file:

Mathematica 11.3.0 Kernel for Linux x86 (64-bit)
Copyright 1988-2018 Wolfram Research, Inc.
In[2]:= num {}

Out[3]= True

Out[4]= 1.2216923087229409686348818698669268506575506911553

And the sh script :

#$ -cwd
/gpfs0/system/Mathematica/11.3/Executables/math < test.wls 3 > test_out.txt

It is looks that I do not succeed to pass the arguments to the script. Any suggestion on how to do it correctly?

  • $\begingroup$ Hmm, I think there are some fundamental misunderstandings going on here which make it difficult for me to understand what you even want to do. Can you give information on JUST 1) the code you want to run in the script, 2) what general arguments will look like/what form they're actually in? Then I'd be able to attempt a ground up solution. $\endgroup$ – ktm Jul 31 '19 at 18:27
  • $\begingroup$ (although showing what you've tried so far is always welcome and I don't want to discourage that) $\endgroup$ – ktm Jul 31 '19 at 18:27
  • $\begingroup$ thank you @user6014 I can't post my code, but I will try to explain my problem. I try to run simulations, and each instance of the simulation will process different instance of the dataset, each dataset has a different id (integer). I want an implement 'for' in "sh" script and run all instances of the dataset, using one "sh" script. So it is the reason why I have to pass an integer from into WL script. $\endgroup$ – Kiril Danilchenko Jul 31 '19 at 19:07
  • $\begingroup$ What does the data that is actually being passed look like? Is it a line in a file? Is each piece of data its own file? $\endgroup$ – ktm Jul 31 '19 at 19:09
  • $\begingroup$ @user6014 The data that will be passed is an integer. Yes, it is a line in code as such this "simulation=y" (I want to pass y into the file), each piece of data its unique file. I am using the parameter "y" as a unique identifier, and each simulation has a unique code, dataset. $\endgroup$ – Kiril Danilchenko Jul 31 '19 at 19:42

Okay, so this is what your script might want to look like:

#!/usr/bin/env wolframscript

num = ToExpression[$ScriptCommandLine[[2]]];

    "~/Desktop/wstest/test"<>DateString[{"Hour", "Minute", "Second", "MillisecondShort"}]<>".txt"
        MixtureDistribution[{1, 2},
            {NormalDistribution[1, Rationalize[.2]], NormalDistribution[3, Rationalize[.1]]}
        WorkingPrecision -> 50

And you can call it with

test.wls 3

from your shell (a chmod +x test.wls might be necessary). The result is a .txt file in the specified folder (with a timestamp added to make it unique) of the form


Your major mistakes:

1) ToExpression[$ScriptCommandLine] needs a position specifier to actually grab the specific argument

2) you were trying to run this script in the math executable. That's not needed, it can just run as its own executable against wolframscript

3) I think you were overcomplicating things with the < and > redirection. With the information you've provided so far, that shouldn't be necessary.

Let me know if you have got further questions.

  • $\begingroup$ Thank you for the answer, and sorry for the delay in response. Please take a look at the output file to line 2,num =ToExpression[$ScriptCommandLine]; Print["num ", num]; The ToExpression[$ScriptCommandLine] is empty, and I don't have any idea why I can't pass any argument to the script $\endgroup$ – Kiril Danilchenko Aug 6 '19 at 4:11
  • 1
    $\begingroup$ I succeed to pass the arguments by using '$CommandLine' $\endgroup$ – Kiril Danilchenko Aug 6 '19 at 4:44
  • $\begingroup$ @KirilDanilchenko So it all worked? $\endgroup$ – ktm Aug 6 '19 at 18:29
  • $\begingroup$ yes its work, thank you a lot $\endgroup$ – Kiril Danilchenko Aug 7 '19 at 3:02
  • 1
    $\begingroup$ @KirilDanilchenko You can accept a correct answer ! $\endgroup$ – ktm Aug 7 '19 at 16:53

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.