0

the moment I use sed to replace node and port the quotes are removed and hence syntax error. Is there a different solution to this syntax error.

NODE=abcd
PORT=1234
mongo_cmd="mongo admin -u $USER  -p $PASWD --host $NODE --port $PORT --quiet --eval 'rs.status().members.filter( x => x.name == "NODE:PORT" ).shift().stateStr)' "
mongo_cmd=$(echo $mongo_cmd | sed -e "s/NODE/$NODE/" -e "s/PORT/$PORT/")
echo $mongo_cmd

mongo admin -u <user>  -p <pass> --host abcd --port 1234 --quiet --eval 'rs.status().members.filter( x => x.name == abcd:1234 ).shift().stateStr)'

2022-03-09T23:45:45.300-0800 E QUERY    [main] SyntaxError: missing ) after argument list @(shell eval):1:73

1 Answer 1

1

Maybe like this:

NODE=abcd
PORT=1234
cmd="rs.status().members.filter( x => x.name == '$NODE:$PORT' ).shift().stateStr)"
mongo_cmd="mongo admin -u '$USER'  -p '$PASWD' --host $NODE --port $PORT --quiet --eval "$cmd"

or

mongo_cmd="mongo 'mongodb://$USER:$PASWD@$NODE:$PORT/?authSource=admin' --quiet --eval "$cmd"
3
  • getting -su: syntax error near unexpected token ('` error. I'm adding the outcome to question.
    – unknown
    Mar 11 at 2:52
  • Maybe but quotes around user and password, see my update. Mar 11 at 10:06
  • using escape character worked well echo "mongo admin -u $USER -p $PASWD --host $HOST --port $PORT --quiet --eval 'rs.status().members.filter( x => x.name == \"${HOST}:${PORT}\" ).shift().stateStr'"
    – unknown
    Mar 11 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.