# Is there a way to get user input when running a script on a Linux terminal?

I have access to a node of a Linux cluster, on which Mathematica is installed. However, I do not have access to a Linux GUI.

Typically I write and test Mathematica scripts on my Windows personal computer (with the typical GUI/front end). Then, I run the scripts on the Linux terminal because the terminal is significantly faster than my desktop computer (my scripts read and write a lot of data). I use the command:

math < test.txt


where test.txt is a text file containing Mathematica code. I have been using this command successfully to run my scripts. I just always need to make sure that I do not try to print graphics to the screen, since I don't have a Linux GUI.

However, now I would like to write a script that prompts the user for input. For example, this code works fine on the Windows GUI:

Print["start"]; str = InputString[]; Print["string: " <> str]; Print["finish"];

But, it fails on the Linux terminal, generating this console output:

Mathematica 7.0 for Linux x86 (64-bit) Copyright 1988-2008 Wolfram Research, Inc.

In[1]:= start

In[2]:= ?

In[3]:= finish

In[4]:=

Is there any way I can prompt the user for input on a Linux terminal? The input does not necessarily need to be interactive. For example, I can imagine there being a way to pass an input string to math by a command like:

math -input "This is my input string." < test.txt


but I am not sure what is the syntax to do this.

InputString[] works on the terminal if I simply enter a terminal using the command:

math


and then type InputString[] in the input prompt. However, this is not how I want to run my scripts, since this (I think) would require me to retype every command, line by line.

-
@b.gatessucks math -initfile test.txt works, but it seems that math -script test.txt does not. For me (Mathematica 7), math -script test.txt just gives me a Mathematica input prompt, but does not execute the commands in test.txt. I wonder what is the difference between -initfile and -script. –  Andrew Sep 14 '12 at 19:42
@b.gatessucks man math says that -initfile "Execute[s] the commands in the initialization file file during startup." I don't see -script described in man math. –  Andrew Sep 14 '12 at 19:46
-script may be new (?) -- it is not documented in the linux man page for v8 but it is in the GUI help. The significant difference is -script exits when done, while -initfile leaves you at a prompt (unless you Quit[] in the script of course ) –  george2079 Sep 14 '12 at 19:49
@george2079 Thanks. OK, I will just use -initfile, and put Quit[] at the bottom of my scripts. –  Andrew Sep 14 '12 at 19:50

try: math -initfile test.txt

30

-

If you need more interactivity, take a look at mash. From the README:

This is how "math -script" should work. Namely, the same way that the perl/python/ruby/etc interpreters do:

• take a mathematica source file as its first argument (or from stdin if no arguments),
• make all the arguments available to the mathematica code as an array (list) called ARGV,
• evaluate the code,
• prints to stdout and stderr only what it is explicitly told to.

The advantages of this include having a self-contained mathematica program that can be executed (with arguments) from the command line, and be used with other programs in a pipeline, etc.

I haven't used it recently but tried it under an older Mathematica version (7 maybe?). Disadvantage is that you need perl (which should be no problem on the standard Linux machine).

-