0

I'm trying to run mongo commands through my bash script file.

I have sample.sh -- run with ./sample.sh

I want to run below command inside my sample.sh file. It is bash script. I want to mongo commands run inside the file, not inside the shell.

kubectl exec $mongoPodName -c mongo-mongodb -- mongo db.createUser({user:"admin",pwd:"123456",roles:[{role:"root",db:"admin"}]})

I get the error below :

./generate-service-dbs.sh: line 37: syntax error near unexpected token `('
./generate-service-dbs.sh: line 37: `kubectl exec $mongoPodName -c mongo-mongodb -- mongo db.createUser({user:"admin",pwd:"123456",roles:[{role:"root",db:"admin"}]})'


1 Answer 1

2

Don't you have to run command with --eval?

kubectl exec $mongoPodName -c mongo-mongodb -- mongo --eval 'db.createUser({user:"admin",pwd:"123456",roles:[{role:"root",db:"admin"}]})'

Sign up to request clarification or add additional context in comments.

4 Comments

I try --eval for this command ``` kubectl exec $mongoPodName -c mongo-mongodb -- mongo --eval 'use myTest' ``` . It has error : ``` 2020-08-18T22:31:53.745+0000 E QUERY [js] uncaught exception: SyntaxError: unexpected token: identifier : @(shell eval):1:4 ```
I need to create mongodbs with user and roles from *.ini files, so i should run multi-line mongo commands within my sample.sh file, but i have many errors.
I am afraid I am not too familiar with mongodb but you should able to run a few command in a .js file. Please see this reference : docs.mongodb.com/manual/reference/program/mongo/…
We need to use double quotes (") instead of single quotes here. I tested with both cases, and the one which works is with single quotes. kubectl exec $mongoPodName -c mongo-mongodb -- mongo --eval "db.createUser({user:\"admin\",pwd:\"123456\",roles:[{role:\"root\",db:\"admin\"}]});"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.