[SERVER-18643] Output redirection from mongo shell Created: 25/May/15  Updated: 06/Dec/22  Resolved: 29/Jun/20

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Ricardo Lorenzo Assignee: Backlog - Service Architecture
Resolution: Won't Fix Votes: 0
Labels: ml-retriage, platforms-re-triaged, sp-shell
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
is duplicated by SERVER-27311 3.2.10 js client prints trace message... Closed
Related
related to SERVER-14880 Ability to output to file from mongo ... Backlog
is related to SERVER-19791 Save mongo shell session into a file ... Closed
Assigned Teams:
Service Arch
Participants:

 Description   

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



 Comments   
Comment by Jessica Sigafoos [ 29/Jun/20 ]

Once the new shell supports running scripts, we assume it will behave in the expected manner.  If this is not the case, please file a new ticket in the MONGOSH project.

Comment by Chris Findlay [ 03/Jun/15 ]

Implementing the other would make it easy to implement this if we simply define some system filehandles for STDOUT, STDERR and STDIN.
The main point here is that in all CLIs I can think of, there are 2 output streams, but the mongo shell allows access to only one, and without SERVER-14880 our only choice is to redirect output to a file, and thus by definition, ALL output to the file. E.g. I can't be generating some custom CSV and also be printing out my progress.

As it turns out, SERVER-14880 will solve my particular use case entirely, but I do suggest the ability to have the system filehandles be accessed as an extension to it.
Input in particular would provide many useful possibilities.

Comment by Kevin Pulo [ 29/May/15 ]

This feels very similar to SERVER-14880, and possibly some of the tickets it also links to.

Generated at Thu Feb 08 03:48:20 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.