Thanks to Pillsy and Will Robertson for the MASH plug! Here's the relevant StackOverflow question: Call a Mathematica program from the command line, with command-line args, stdin, stdout, and stderr
If you don't use MASH, you may want to use the following utility functions that MASH defines.
For example, the standard Print will print strings with quotation marks -- not usually what you want in scripts.
ARGV = args = Drop[$CommandLine, 4]; (* Command line args. *)
pr = WriteString["stdout", ##]&; (* More *)
prn = pr[##, "\n"]&; (* convenient *)
perr = WriteString["stderr", ##]&; (* print *)
perrn = perr[##, "\n"]&; (* statements. *)
EOF = EndOfFile; (* I wish mathematica *)
eval = ToExpression; (* weren't so damn *)
re = RegularExpression; (* verbose! *)
read[] := InputString[""]; (* Grab a line from stdin. *)
doList[f_, test_] := (* Accumulate list of what f[] *)
Most@NestWhileList[f[]&, f[], test]; (* returns while test is true. *)
readList[] := doList[read, #=!=EOF&]; (* Slurp list'o'lines from stdin *)
To use MASH, just grab that perl file, mash.pl, and then make your test.m like the following:
#!/usr/bin/env /path/to/mash.pl
prn["hello"];
x := 1;
y = x+1;
z = y+1;
prn["y=", y];
prn["z=", z];