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.