Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

256
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda