Hi, I got stuck here.
I run the script (see below) and it does what it is supposed to do , checking if major_id and course_id exist in each table and inserting the values in the tables looping over a test file.
but the message I get is “your script should have the suggested command added correctly”, I checked if missing parenthesis, indentation, single and double quotes but I do not get it. Any idea why is not meeting the requirements?
#!/bin/bash
# Script to insert data from courses.csv and students.csv into students database
PSQL="psql -X --username=freecodecamp --dbname=students --no-align --tuples-only -c"
cat courses_test.csv | while IFS="," read MAJOR COURSE
do
if [[ $MAJOR != "major" ]]
then
# get major_id
MAJOR_ID=$($PSQL "SELECT major_id FROM majors WHERE major='$MAJOR'")
# if not found
if [[ -z $MAJOR_ID ]]
then
# insert major
INSERT_MAJOR_RESULT=$($PSQL "INSERT INTO majors(major) VALUES('$MAJOR')")
if [[ $INSERT_MAJOR_RESULT == "INSERT 0 1" ]]
then
echo Inserted into majors, $MAJOR
fi
# get new major_id
MAJOR_ID=$($PSQL "SELECT major_id FROM majors WHERE major='$MAJOR'")
fi
# get course_id
COURSE_ID=$($PSQL "SELECT course_id FROM courses WHERE course='$COURSE'")
# if not found
if [[ -z $COURSE_ID ]]
then
# insert course
INSERT_COURSE_RESULT=$($PSQL "INSERT INTO courses(course) VALUES('$COURSE')")
if [[ $INSERT_COURSE_RESULT == "INSERT 0 1" ]]
then
echo Inserted into courses, $COURSE
fi
# get new course_id
COURSE_ID=$($PSQL "SELECT course_id FROM courses WHERE course='$COURSE'")
fi
# insert into majors_courses
fi
done