title: How to execute multiple commands in interactive shell
body: <p>My application works with all kind of shell commands, provided from the console (<code>curl</code>, <code>date</code>, <code>ping</code>, whatever). Now I'd like to cover the case with interactive shell commands (like mongo shell), using <code>os/exec</code>.</p>
  
  <ul>
  <li><p>e.g. as a first step, connect to mongodb:
  <code>mongo --quiet --host=localhost blog</code></p></li>
  <li><p>then perform <strong>arbitrary number</strong> of commands, getting the result on every step
  <code>db.getCollection('posts').find({status:'INACTIVE'})</code></p></li>
  <li><p>and then
  <code>exit</code></p></li>
  </ul>
  
  <p>I tried the following, but it allows me to perform only one command per mongo connection:</p>
  
  <pre><code>func main() {
  
      cmd := exec.Command("sh", "-c", "mongo --quiet --host=localhost blog")
  
      cmd.Stdout = os.Stdout
      cmd.Stderr = os.Stderr
  
      stdin, _ := cmd.StdinPipe()
  
      go func() {
          defer stdin.Close()
          io.WriteString(stdin, "db.getCollection('posts').find({status:'INACTIVE'}).itcount()")
          // fails, if I'll do one more here
      }()
  
      cmd.Run()
      cmd.Wait()
  }
  
  </code></pre>
  
  <p>Is there a way to run multiple commands, getting stdout result per executed command?</p>