0

In a bash script, this won't work with Unexpected token ILLEGAL

#!/bin/bash
value="White Spaced String"
mongo --verbose localhost:27017/myDB --eval 'db.ips.update({"id":"id"}, { $push: { "key": {"key":"'$value'"}}})'

where as this works:

#!/bin/bash
value="NonWhiteSpacedString"
mongo --verbose localhost:27017/myDB --eval 'db.ips.update({"id":"id"}, { $push: { "key": {"key":"'$value'"}}})'

Any ideas ow to work around this? I tried:

value="${value// /\\ }" which will substitute whitespaces with \ escape signs and a whitespace. Yet, no success.

This does work, yet I want to preserve the whitespaces in my JSON in Mongo:

value="${value// /_}"

3
  • 3
    Your escapig is no working, you have " which appear to not be escaping right Commented Jan 21, 2015 at 14:21
  • ok changed script, updateing now Commented Jan 21, 2015 at 14:28
  • Changed Question entirely. Commented Jan 21, 2015 at 15:09

1 Answer 1

1

The argument to the --eval option needs to be a single bash word, so you need to avoid bash word-expanding $value. That means you need to put the expansion inside double-quotes:

mongo --verbose localhost:27017/myDB --eval \
  'db.ips.update({"id":"id"}, { $push: { "key": {"key":"'"$value"'"}}})'
                                                         ^      ^

That argument consists of a single-quoted segment, a double-quoted segment, and then a final single-quoted segment. The double quotes inside the single-quoted segments are, of course, ordinary characters.

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

Comments

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.