Lets say you need to find out which is the primary node in the mongo cluster in your shell script,
mongo –host 10.126.71.162 –username=userid –password=secret –eval “db.runCommand(‘ismaster’).primary”
This would quickly open the mongo shell and execute the commands, and give the output as the last line
MongoDB shell version v4.0.5
connecting to: mongodb://10.126.71.162:27017/?gssapiServiceName=mongodb
Implicit session: session { “id” : UUID(“65f2e5ab-aa97-4306-8a20-afd70a4ceeb0”) }
MongoDB server version: 4.0.5
10.126.71.162:27017