You can use heredoc syntax.
#! /bin/sh
mongo <<EOF
use admin
db.shutdownServer()
quit()
exit
Turns out heredoc syntax throws a warning when EOF is missing at the end for bash script. This is the bash script version.
#! /bin/bash
mongo <<EOF
use admin
db.shutdownServer()
quit()
EOF
Here is the output, I guess this is what you expected.
MongoDB shell version: 2.4.14
connecting to: test
switched to db admin
Wed Jun 24 17:07:23.808 DBClientCursor::init call() failed
server should be down...
Wed Jun 24 17:07:23.810 trying reconnect to 127.0.0.1:27017
Wed Jun 24 17:07:23.810 reconnect 127.0.0.1:27017 ok
Wed Jun 24 17:07:23.812 Socket recv() errno:104 Connection reset by peer 127.0.0.1:27017
Wed Jun 24 17:07:23.812 SocketException: remote: 127.0.0.1:27017 error: 9001 socket exception [RECV_ERROR] server [127.0.0.1:27017]
Wed Jun 24 17:07:23.812 DBClientCursor::init call() failed