On Windows, MathKernel.exe launches the kernel running within a window. The standard output is redirected to that window. To avoid this redirection, we must run the kernel as a console application using Math.exe instead:
"C:\Program Files\Wolfram Research\Mathematica\10.0\Math" ^
-noprompt ^
-script C:\Users\a\Desktop\test.m ^
"data goes here"
Version 10 also has wolfram.exe which does the same thing.
Update: Using DOSKEY
As requested in a comment, we can define a Windows command alias for our script like this:
doskey _="C:\Program Files\Wolfram Research\Mathematica\10.0\Math" -noprompt -script "C:\path\to\script.m" $*
If script.m contains this...
Print @ {"input" -> InputString[], "cmdline" -> $CommandLine}
... then we can have the following exchange on the command line...
C:\> _ arg1 arg2
abcde
... which produces this output:
{"input" -> "abcde", "cmdline" -> {"C:\\Program Files\\Wolfram Research\\Mathematica\\10.0\\Math",
"-noprompt", "-script", "C:\\path\\to\\script.m", "arg1", "arg2"}}
Update 2: Using a Command File
We can use a Windows command file to emulate the CYGWIN-based pattern established in the answer by @William. First, we create a command file named _.cmd that contains the following lines:
@echo off
"C:\Program Files\Wolfram Research\Mathematica\10.0\Math" ^
-noprompt -script "%~dp0mmascript.m" %*
Ensure that _.cmd is in a directory that appears on the Windows executable PATH. The current working directory is fine.
Next, in the same directory, create a Mathematica script named mmascript.m with the following commands:
in := ToExpression @ InputString[]
If[Length @ $CommandLine < 5
, Print["no args"]
, Print @ ToExpression @ # & /@ $CommandLine[[5;;]]
]
It only needs to be in the same directory because the command file uses %~dp0 to reference it -- feel free to change that to an absolute pathname if desired.
mmascript.m differs from @William's test.m in that it evaluates all user-supplied arguments instead of just the first.
Here is a session transcript of the command file in action:
C:\Users\wreach> _
"no args"
C:\Users\wreach> _ Sin[Pi/2]
1
C:\Users\wreach> _ in
4 * 5
20
C:\Users\wreach> _ First@FileNames[]
"AppData"
C:\Users\wreach> _ FileNames[] | _ "in // First"
"AppData"
C:\Users\wreach> echo "test" | _ ToUpperCase@in
"TEST"
Note how the last two examples use pipes to feed input to the script. Like the CYGWIN example, the variable in expands to the result of evaluating a string read from stdin.
InputString[]doesn't seem to work in your example program if you attempt to assign the function to adoskeyshortcut. – William Feb 15 '15 at 22:36DOSKEY /m. – WReach Feb 15 '15 at 23:01echo test | _ arg1 arg2it fails in W7 because of the nature of the waydoskeyworks.'_' is not a recognized as an internal or external command.– William Feb 16 '15 at 00:15_ arg1 arg2 \n tworks. http://stackoverflow.com/questions/17074672/redirecting-stderr-and-stdout-in-a-doskey-macro – William Feb 16 '15 at 00:22_.cmdsomewhere in thePATH). But we have now firmly left Mathematica and are well into superuser :) – WReach Feb 16 '15 at 02:33test.m. There is no essential obstacle to adjusting the Mathematica script to accept as many arguments as you like, and to evaluate them or not evaluate them as suits the application. – WReach Feb 16 '15 at 17:38_ arg1 arg2as arguments while you cmd script accepts only_ arg1of the 2 arguments. I'm going to hold off on the bounty for now because I don't believe that is the intended behavior. – William Feb 17 '15 at 21:38WriteString["stderr", "bad news!"]. As to re-using a session using only streams and strings, the only approach that comes to mind at the moment is to implement a Telnet-like protocol where Mathematica and the other process take turns sending commands and responses back and forth. MathLink would give complete fine-grained control but would take more effort to set up. – WReach Aug 05 '15 at 14:59