0

I have the following function;

function getdetails {
    if ! "${PSQL_PATH}" -d "$DB" -U "$DB_USER" -h localhost -p "$DB_PORT" -t -c  | while read -a Record ; do
        taskid="${Record[0]}"
        clientname="${Record[1]}"
        backup_pass="${Record[2]}"
        backup_dir="${Record[3]}"
      done; then
            echo "Could not fech next task metadata from database"
            exit 1
      fi <<EOF
        WITH firsttask AS (SELECT taskid from tasks 
                            WHERE status = 'PENDING'
                            ORDER BY date_started ASC
                            LIMIT 1)
        SELECT taskid, username, storage_password AS backup_password, location AS backup_dir 
        FROM firsttask 
        INNER JOIN users USING (userid)
        INNER JOIN storage USING (userid)
        WHERE (username = '$1');
EOF 
}

For some reason, bash does not detect the last EOF and reports: ./processor.sh: line 138: warning: here-document at line 41 delimited by end-of-file (wanted `EOF') ./processor.sh: line 139: syntax error: unexpected end of file

Any ideas why the EOF is not picked up? Thank you!

Niels
  • 45
  • 1
  • 6

1 Answers1

5

It fails because the closing EOF word has trailing whitespace. It must be on a line by itself, with no leading or trailing whitespace.

An exception is when using the <<- syntax, in which case the closing word may be preceded by one or more TABs (but never spaces).

geirha
  • 5,801
  • 1
  • 30
  • 35