6
$\begingroup$

How do you pass negative values as input for a wolframscript?

This documentation page explains that the syntax for passing parameters is:

wolframscript -file filename.m 1 2 3 output.txt

However it seems it doesn't work with negative parameters, since, for example, the -2 in

wolframscript -file filename.m 1 -2 3 output.txt

is interpreted as an option, not as a number/parameter and the script returns an error saying that:

Export::infer: Cannot infer format of file 1.

As MWE take for example the script:

arg=ToExpression/@Most@Rest[$ScriptCommandLine]; (*Drop first argument (which is filename.m by default) and last argument and transforms strings to numbers*)
file=Last[$ScriptCommandLine];  (*The last argument is the output file name*)
Export[file,arg];  (*Exports*)

This is generated from a .nb by saving it as .m file with shift+ctrl+s after selecting all the cells and pressing ctrl+8.

EDIT After the answer by @Syed, I see I have to add that I use Ubuntu. With Syed's answer I get the error:

./run.sh: line 3: syntax error near unexpected token `('.

The full bash script run.sh is:

#! /bin/bash

wolframscript -file filename.m 1 (-2) 3 output.txt
$\endgroup$
4
  • $\begingroup$ Why do you have bash Shebang in the first line of a WolframScript file? Also, how do you call this file, what is the content of run.sh? $\endgroup$
    – Domen
    May 1 at 14:25
  • $\begingroup$ @Domen I am sorry. I messed up everything: I wanted to add the content of the bash script, but copied the .m file. I have edited the question. Sorry again. $\endgroup$
    – mattiav27
    May 1 at 14:33
  • $\begingroup$ Take a moment and edit the question so that it includes the actual content of your files. For example, you are still having a Mathematica syntax error in Rest@[$ScriptCommandLine] (you can't use both @ and []). Also, the error message makes no sense becase there is no line 3 in your run.sh. $\endgroup$
    – Domen
    May 1 at 14:44
  • $\begingroup$ @Domen ok done. There is a blank line in the actual run.sh between #! /bin/bash and wolframscript... I removed it in the post $\endgroup$
    – mattiav27
    May 1 at 14:51

2 Answers 2

3
$\begingroup$

I have found the solution myself. In fact it is similar to the other answer by Syed.

I have found that ( and ) have a special meaning in bash: as explained in this thread in the Unix-SE they are used for subshell constructs. The first comment to the question in the linked thread, however, gives the right answer: using \( and \) instead.

The following works in my case:

#! /bin/bash

wolframscript -file filename.m 1 \(-2\) 3 output.txt

I think it should work in any other Linux version (and also in MacOS maybe?)

$\endgroup$
2
$\begingroup$

With a minor change to the script:

arg=ToExpression/@Most@Rest@$ScriptCommandLine;
file=Last[$ScriptCommandLine]; 
Export[file,arg]; 

Use:

wolframscript -file sample.m 1 (-2) out.txt

C:\type out.txt

1

-2


EDIT

Echo[$ScriptCommandLine] argv =StringReplace[#, "\\"->""]&/@Most@Rest @ $ScriptCommandLine; file=Last@ $ScriptCommandLine; Echo[argv] argc = Length @ argv; Echo[argc] Export[file,argv];

along with the command line:

wolframscript -file sample.m -args 1 \-2 out.txt

i.e. escaping the minus sign with \ and then stripping it later from arguments.

$\endgroup$
7
  • 1
    $\begingroup$ I am sorry, I accepted your answer, but had to revert it, since after trying on Ubuntu I get the error ./run.sh: line 3: syntax error near unexpected token ('`. I edited the question in order to add the OS. Thanks anyway. $\endgroup$
    – mattiav27
    May 1 at 14:15
  • $\begingroup$ I am on Win7-x64. Thanks for the heads up. $\endgroup$
    – Syed
    May 1 at 14:30
  • $\begingroup$ Try: wolframscript -file -sample.m -- 1 -2 out.txt. I cannot experiment with it as I don't have Linux currently. The -- is to indicate that arguments follow. $\endgroup$
    – Syed
    May 1 at 14:37
  • $\begingroup$ @Syed, I was also trying with -- when wanting to answer the question but unfortunately this does not work. $\endgroup$
    – Domen
    May 1 at 14:40
  • $\begingroup$ @mattiav27 I have tried something different. Please see the edit. $\endgroup$
    – Syed
    May 1 at 15:45

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.