title: Capturing output and input of interactive program with bash
body: <p>I'm working on automating an interactive command line Java program in bash to validate that the program produces proper output for the input (basically poor-man's unit testing in bash).</p>
  
  <p>For example, if I have a java program that asks the user to enter their full name, and then outputs only their first name, it should look something like this:</p>
  
  <pre><code>Enter your name: John Doe
  John
  </code></pre>
  
  <p>Where "John Doe" was typed in by the user.</p>
  
  <p>A simple bash command to run this might look like this:</p>
  
  <pre><code>OUTPUT=`echo "John Doe" | java NameReader`
  </code></pre>
  
  <p>or</p>
  
  <pre><code>OUTPUT=`java NameReader &lt;&lt;&lt; "John Doe"`
  </code></pre>
  
  <p>The trouble with both of these is that $OUTPUT now contains the following:</p>
  
  <pre><code>Enter your name: John
  </code></pre>
  
  <p>Because the text that was sent to stdin (and its newline accompanying) isn't reproduced in the output of the program the way we would see it in the console.</p>
  
  <p>Ideally, $OUTPUT would contain this:</p>
  
  <pre><code>Enter your name: John Doe
  John
  </code></pre>
  
  <p>But I could live with this:</p>
  
  <pre><code>Enter your name: 
  John
  </code></pre>
  
  <p>(The input is omitted entirely, but the output is on a new line, as expected)</p>
  
  <p>Is there a way in bash (without altering the underlying java program) to get the text that is being piped to stdin to also pipe to stdout at the "time" it's read by the java program, so the full interactive session is captured?</p>
  
  <p>(One more note: Some searching indicated that the spawn/expect commands might be helpful, but the system this will run on does not appear to have them available)</p>