# Passing arguments into WolframScript in batch mode

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[1]:= In[2]:= num {} In[3]:= Out[3]= True Out[4]= 1.2216923087229409686348818698669268506575506911553  And the sh script : #!/bin/sh #$ -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?

• 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. – ktm Jul 31 '19 at 18:27
• (although showing what you've tried so far is always welcome and I don't want to discourage that) – ktm Jul 31 '19 at 18:27
• 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. – Kiril Danilchenko Jul 31 '19 at 19:07
• 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? – ktm Jul 31 '19 at 19:09
• @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. – 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]]]; Export[ "~/Desktop/wstest/test"<>DateString[{"Hour", "Minute", "Second", "MillisecondShort"}]<>".txt" , RandomVariate[ MixtureDistribution[{1, 2}, {NormalDistribution[1, Rationalize[.2]], NormalDistribution[3, Rationalize[.1]]} ] , num , WorkingPrecision -> 50 ] , "CSV" ]  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 2.945267726074665622104002146319038844277342932419 3.0059934477355789443483766777766852723094126818552 2.9593531823056876773610286479120461237403695981006  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.

• 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 – Kiril Danilchenko Aug 6 '19 at 4:11
• I succeed to pass the arguments by using '\$CommandLine' – Kiril Danilchenko Aug 6 '19 at 4:44
• @KirilDanilchenko So it all worked? – ktm Aug 6 '19 at 18:29
• yes its work, thank you a lot – Kiril Danilchenko Aug 7 '19 at 3:02
• @KirilDanilchenko You can accept a correct answer ! – ktm Aug 7 '19 at 16:53