Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

257
Views
Bash script reads from .csv file to insert objects into mongo using --eval, keep getting SyntaxError. Can't figure out why

Im working on a homework assignment. We have to insert 1000 different users based on male and female. I started with the males. The idea is to use the script to insert all 442 records from a .csv file. Here is the script:

#!/bin/bash
while IFS=, read -r userName firstName lastName gender dob language bloodType zodiac constellation planet genre dino;
do
        mongo USERNAME --eval  'db.USERNAME.insert({username:"'$userName'",first:"'$firstName'",last:"'$lastName'",gender:"'$gender'",dob:"'$dob'"})'
done < males1.csv

I am not inserting all the fields because I am trying to determine the error. When I deleted the dob part of the code, it suddenly worked. It looked like this:

mongo X --eval 'db.USERNAME_male_a5.insert({username:"'$userName'",first:"'$firstName'",last:"'$lastName'",gender:"'$gender'"})'

I was able to enter all 442 records. But I was only able to insert the username, firstName lastName and Gender. I need the other fields in there too. But when I added the dob, it was given the following error:

uncaught exception: SyntaxError: "" literal not terminated before end of script : @(shell eval):1:96

I tried changing the quotes, but then I would get user X is not defined, which further confused me.

My professor gave us this syntax:

mongo USERNAME_a5 --eval 'db.USERNAME_{GENDER}_a5.insert({username:"", first:"", last:"", gender:"", dob:"", language:"", blood_type:"", zodiac:"", constellations:"", planets:"", tv_genre:"", dino:""})'

But this proved to be difficult to work with as I realized I need to change the quotes so that it doesn't literally insert the word "$userName"

Thank you for your time.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It can easily be overlooked with all those quoting characters that the parameter expansion $dob is not quoted, so that when dob is something like 'january 1 1990' the supposed --eval parameter is split at the spaces into the three pieces db.USERNAME.insert({username:"…",first:"…",last:"…",gender:"…",dob:"january, 1 and 1990"}), which causes the SyntaxError: "" literal not terminated. You can correct this by putting double quotes around $dob (i. e. …,dob:"'"$dob"'"})') and at least all other variables which can contain spaces.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!