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.
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.