Home > Mobile >  Syntax error at beginning of command when running script
Syntax error at beginning of command when running script

Time:09-23

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;
        ^

CodePudding user response:

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

CodePudding user response:

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

  • Related