[SERVER-8020] Shell exits with successful return code upon failure Created: 23/Dec/12  Updated: 11/Jul/16  Resolved: 24/Jun/13

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

Type: Bug Priority: Major - P3
Reporter: Ben McCann Assignee: Tad Marshall
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Participants:

 Description   

If I run a command with a bad username and password as shown below, the shill prints an error message, but returns with a successful return code. This makes it difficult to detect an error when executing the shell from another script. E.g. I have a script which uses the shell to lock the database, take a backup, and unlock the database and I'd like to detect an error with the bad username or password.

$ mongo -u myuser -p badpassword mydatastore --eval "printjson(db.fsyncLock())"
MongoDB shell version: 2.2.2
connecting to: mydatastore

{ "errmsg" : "need to login", "ok" : 0 }

 Comments   
Comment by Tad Marshall [ 24/Jun/13 ]

I tested the current master (2.5.1-pre-) and it behaves as requested here: the return code is set to 1 when authentication from the mongo shell command line fails.

With correct password

$ ./mongo -u user -p pwd admin --eval "printjson('hello there')"
MongoDB shell version: 2.5.1-pre-
connecting to: admin
"hello there"
$ echo $?
0

With incorrect password

$ ./mongo -u user -p wrong admin --eval "printjson('hello there')"
MongoDB shell version: 2.5.1-pre-
connecting to: admin
Mon Jun 24 15:30:07.759 Error: 18 { code: 18, ok: 0.0, errmsg: "auth fails" } at src/mongo/shell/db.js:234
exception: login failed
$ echo $?
1

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