Troubleshooting: Issue with Evaluating Whitespaces in MongoDB using Bash

When working with a bash script, you may encounter the error message Unexpected token ILLEGAL

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

However, if you remove the whitespaces in the variable declaration like this:

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

If you still want to keep the whitespaces and encounter issues, one idea to work around it is by using:

value="${value// /\\ }" This command substitutes whitespaces with \ escape signs and a whitespace.

Another workaround that works but changes the whitespaces is:

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

Answer №1

When using the --eval option in bash, it's important to ensure that the argument is a single bash word to avoid any issues with bash word-expanding features like $value. To achieve this, you should enclose the expansion within double quotes:

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

This argument is made up of a combination of single-quoted and double-quoted segments, along with one final single-quoted segment. The double quotes found inside the single quotes are simply treated as regular characters.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is It Possible to Determine If a Checkbox Has Been Checked?

Here's what I have now: I have a checkbox that I need to verify if it's selected. <input type="checkbox" name="person_info_check" value="0" &nbps>Read and agree!</input> However, the method I found online to verify the checkbox ...

ChartJS Chart failing to adjust size

I am working on a software project that includes a chart with two x-axes and one y-axis. However, I am looking to reduce the size of the chart. Although I attempted to adjust the width and height using "width: 50px" and "height: 50px", the resizing did no ...

Verify if an element exists within an array in a Mongodb database

{ data: [9, 3, 56, 3, 54] } { data: [6, 3, 5, 3, 5] } { data: [1, 2, 3, 4, 5] } How can I retrieve the object that contains the value 1 in the data array using MongoDB? db.findOne({"data":1 in data}) Is this the correct approach? ...

What is the best way to extract all query parameters in React-Router-DOM v6 using `useSearchParams` without having to specify the key?

I'm currently developing a React project using react-router-dom v6 and my goal is to extract all query parameters. http://localhost:3000/users?page=5&pageSize=25 The specific query parameters I am looking for are page and pageSize. I am aware of ...

Guidelines for choosing a dropdown menu option using jQuery in a WordPress photo gallery

First off, take a look at the screenshot to understand what I'm trying to accomplish: Within WordPress, after clicking on "New Post" and then on the "Add Media" button, a pop-up image gallery appears with an image filtering drop-down menu. My goal is ...

What is the best way to apply the 'active' class to a selected navigation link using JavaScript?

How can I use JavaScript to change the active click of a Bootstrap navbar based on which nav link was clicked? The issue is that the changes made by JavaScript are lost when the browser navigates to the corresponding page. MY CODE: document.querySelect ...

Using Highmaps in a VueJs application involves passing a state to the mapOptions for customization

I'm currently struggling with passing a vuex state to mapOptions in vuejs components. Here is the code snippet: <template> <div> <highcharts :constructor-type="'mapChart'" :options="mapOptions" class="map">&l ...

JavaScript loaded only on first visit or after the page is refreshed

For my cross-platform app using Cordova and jQuery Mobile 1.4.5, I have a basic html page that needs to load a javascript file. <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Security-Policy" content="default-src ...

What is the best way to call a method of a JavaScript object after calling a different method?

Upon frequently encountering this jQuery code snippet, I decided to simplify it into one level action as illustrated in the following code. function $(id) { var $ = document.getElementById(id); $.action1 = function() { }; return $; } Now ...

Capture XHR requests and alter the response body

I'm in the process of developing a script that serves as a proxy/wrapper for the conventional XMLHttpRequest object. This will allow me to intercept it, alter the responseText, and then return it back to the original onreadystatechange event. The mai ...

What is the method for eliminating the white background that shows up when the drop-down menu is hidden in Bootstrap?

I am currently using Bootstrap 3.3.4 Here is the HTML code I am working with: <nav class="navbar navbar-inverse"> <div class="container-fluid"> <div class="navbar-header"> <ul class="nav navbar-nav"> <li cla ...

Enhance the way UV coordinates are generated for rotated triangles/faces within THREE.js

While researching how to map a square texture onto n-sided polyhedrons using a convex hull generator in THREE.js, I found that existing solutions did not fully address my issue. The challenge was to ensure that the texture was not distorted and appeared co ...

Encountered an issue during Karma unit testing of an Ionic App: the error message [ng:areq] indicates that the argument 'StockCtrl' is not recognized as a function and is

I've encountered an issue while trying to conduct unit testing on my ionic app. Despite checking previous queries, none of the given solutions seem to work for me. Any suggestions on what might be causing this error? The error message I'm receiv ...

How should callbacks be established for communication between a React app and an iframe using postMessage?

I'm encountering an issue with my website's communication with a third-party iframe. The site has three methods - login, sign, and get information - all functioning in a similar manner. I embed the third-party iframe and send a message to it usin ...

Retrieve one single result in a group - MongoDB

Currently, I have a function that calculates the total sum of prices for the current month. However, it is returning a list even though it should just return a single document. Here is the code snippet: const calculateMonthlyRevenue = asyncHandler(async (r ...

Updating dependencies of dependencies in npm: A step-by-step guide

I'm puzzled by the fact that I can't seem to find a solution to this seemingly easy question. It's surprising that even running npm update doesn't resolve the issue. While I can't post my entire dependency tree here, I'll do ...

Displaying characteristics depending on chosen checkboxes

In the CodeSandBox comments, I have outlined the issues. To elaborate, I have an array named field.names that is used to render checkboxes. The goal is for users to be able to enter individual values for lengthType and size when selecting a checkbox. Howev ...

Discovering the length of an array using JavaScript

I have a question that may seem silly: How can we accurately determine the length of an array in JavaScript? Specifically, I want to find the total number of positions occupied in the array. Most of you may already be familiar with this simple scenario. ...

Retrieve the URL from the onclick property of an anchor element using Python

Struggling to extract a URL from an onclick attribute in a tag using Selenium with Python. The URL is located within a JavaScript function, and despite trying multiple techniques, the solution remains elusive. Executing the onclick function through the exe ...

Combining Various Input Types for Seamless Integration: Getting Files and Text to Collaborate Efficiently

In my form, I have two types of image inputs - one from a file and the other from a URL. To ensure that the image from the last changed input is used, I created an additional invisible input field called "imgTempURL". This field is filled with the image&ap ...