6

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

Domen
  • 23,608
  • 1
  • 27
  • 45
mattiav27
  • 6,677
  • 3
  • 28
  • 64
  • 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? – Domen May 01 '23 at 14:25
  • @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. – mattiav27 May 01 '23 at 14:33
  • 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. – Domen May 01 '23 at 14:44
  • @Domen ok done. There is a blank line in the actual run.sh between #! /bin/bash and wolframscript... I removed it in the post – mattiav27 May 01 '23 at 14:51

2 Answers2

3

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?)

mattiav27
  • 6,677
  • 3
  • 28
  • 64
2

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.

Syed
  • 52,495
  • 4
  • 30
  • 85
  • 1
    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. – mattiav27 May 01 '23 at 14:15
  • I am on Win7-x64. Thanks for the heads up. – Syed May 01 '23 at 14:30
  • 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. – Syed May 01 '23 at 14:37
  • @Syed, I was also trying with -- when wanting to answer the question but unfortunately this does not work. – Domen May 01 '23 at 14:40
  • @mattiav27 I have tried something different. Please see the edit. – Syed May 01 '23 at 15:45
  • @Syed It doesn't work. I get many errors like Set::write: Tag Times in argv {sample.m, 1} is Protected. and at the end I get Export::infer: Cannot infer format of file 1. Now I have found a solution myself, but many thanks for your help!! – mattiav27 May 01 '23 at 18:01
  • You have to write the script one command per line. I couldn't format it for this page. It works on windows, so I will leave it there. Thanks for your feedback and answer. – Syed May 01 '23 at 18:19