Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-18643

Output redirection from mongo shell

    • Service Arch

      I wish there is a way on the mongo shell for redirecting the output to STDERR or files.

      The current workaround is just adding some tags as boundaries and parsing the output from a bash script or another application.

      ...
      print("<STDOUT>")
      print(multiline_json)
      print("</STDOUT>")
      print("<STDERR>")
      print(multiline_json)
      print("</STDERR>")
      ...
      

      And at the bash script side:

      #!/bin/bash
      COMMAND="mongo <args>"
      OUTPUT=$(${COMMAND})
      
      function STDERR {
        cat - 1>&2
      }
      
      function STDFILE {
        if [ -z "$1" ]; then
          return
        fi
        cat - >> $1
      }
      
      WRITE_ERR=0;
      for line in $OUTPUT; do
        if [[ "$line" == "<STDERR>"* ]]; then
          WRITE_ERR=1
          continue
        elif [[ "$line" == "</STDERR>"* ]]; then
          WRITE_ERR=0
          continue
        fi
        if [ "$WRITE_ERR" -eq "1" ]; then
          printf "%s\n" "$line" | STDERR
        else
          printf "%s\n" "$line"
        fi
      done
      

            Assignee:
            backlog-server-servicearch [DO NOT USE] Backlog - Service Architecture
            Reporter:
            ricardo.lorenzo Ricardo Lorenzo
            Votes:
            0 Vote for this issue
            Watchers:
            12 Start watching this issue

              Created:
              Updated:
              Resolved: