title: Call a Mathematica program from the command line, with command-line args, stdin, stdout, and stderr
body: <p>If you have Mathematica code in foo.m, Mathematica can be invoked with <code>-noprompt</code>
  and with <code>-initfile foo.m</code>
  (or <code>-run "&lt;&lt;foo.m"</code>)
  and the command line arguments are available in <code>$CommandLine</code> (with extra junk in there) but is there a way to just have some mathematica code like</p>
  
  <pre><code>#!/usr/bin/env MathKernel
  x = 2+2;
  Print[x];
  Print["There were ", Length[ARGV], " args passed in on the command line."];
  linesFromStdin = readList[];
  etc.
  </code></pre>
  
  <p>and chmod it executable and run it?  In other words, how does one use Mathematica like any other scripting language (Perl, Python, Ruby, etc)?</p>