0

I am trying to run a script with ./insert_data.sh; and I get an error saying there is a syntax error at or near the "." Am working in PSQL, thanks

Edit #1: My script:

#!/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
  # 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')")
    echo $INSERT_MAJOR_RESULT

    # get new major_id

  fi

  # get course_id

  # if not found

  # insert course

  # get new course_id

  # insert into majors_courses

done

Edit #2: Command i'm using to run the script: ./insert_data.sh; Error I'm recieving:

   students=> ./insert_data.sh;
ERROR:  syntax error at or near "."
LINE 1: ./insert_data.sh;
        ^
1
  • Comments are not for extended discussion; this conversation has been moved to chat. Commented Sep 23, 2022 at 2:37

2 Answers 2

1

Solved the problem- I had to get out of the PSQL terminal and enter the command within the "project" directory

Sign up to request clarification or add additional context in comments.

Comments

0

We can't replicate your case 1:1 because lack of data. But you can always use bash debug with -x flag to see where it actually stuck:

test@test:/tmp$ bash -x ./test.sh
+ PSQL='psql -X --username=freecodecamp --dbname=students --no-align --tuples-only -c'
+ cat courses_test.csv
+ IFS=,
+ read MAJOR COURSE
++ psql -X --username=freecodecamp --dbname=students --no-align --tuples-only -c 'SELECT major_id FROM majors WHERE major='\''"Sex"'\'''
psql: error: could not connect to server: No such file or directory

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.