Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

262
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda