0

I have a sql file like below

#!/usr/bin/env bash

cat <<'VERBATIM_SQL'
INSERT INTO t_random VALUES (generate_series(1,9999999999), md5(random()::text));
VERBATIM_SQL

The sql file is getting called like below from a shell script

#!/usr/bin/env bash

./insert.sql | psql -U postgres -p 5432 -h localhost -d vnera
./insert.sql | psql -U postgres -p 5432 -h localhost -d vnera
./insert.sql | psql -U postgres -p 5432 -h localhost -d vnera

On executing I am getting a warning like below

Loop 1./insert.sql: line 5: warning: here-document at line 3 delimited by end-of-file (wanted `VERBATIM_SQL')

Can someone let me know why is this warning coming?

EDIT

I did what was suggested in one of the comments

support@vrni-platform:~/postgres_junk$ dos2unix insert.sql
dos2unix: converting file insert.sql to Unix format ...

But still getting the same warning

./insert.sql: line 5: warning: here-document at line 3 delimited by end-of-file (wanted `VERBATIM_SQL')
tuk
  • 5,941
  • 14
  • 79
  • 162
  • This might help: https://stackoverflow.com/q/18660798/3776858 – Cyrus Nov 26 '17 at 15:36
  • 2
    In particular, make sure that your file does not have windows line endings. – rici Nov 26 '17 at 15:54
  • DOS line endings wouldn't cause this problem, unless they weren't consistently used. `VERBATIM_SQL\r` would be a valid delimiter and used in both places. – chepner Nov 26 '17 at 16:01
  • Oh, though if DOS line endings are being used, it's probable(?) that the last line doesn't have a CRLF terminating it like the previous lines do. – chepner Nov 26 '17 at 16:05
  • Probably a duplicate of https://stackoverflow.com/q/31886144/1126841, but I'm not sure enough to close unilaterally. – chepner Nov 26 '17 at 16:06
  • so, put another way, try `dos2unix insert.sql` and then retry your test. Good luck. – shellter Nov 26 '17 at 16:21
  • I did dos2unix still getting same waning. – tuk Nov 26 '17 at 17:06
  • @tuk: OK. Also make sure that there is no whitespace following `VERBATIM_SQL` in the last line. – rici Nov 26 '17 at 17:18
  • @rici - Thanks. Whitespace following `VERBATIM_SQL` was the issue. Can you post your comments as an answer so that I can accept it? – tuk Nov 26 '17 at 17:39

1 Answers1

2

Shells are very picky about what they will accept as a here-doc termination. It must precisely match the supplied token. In particular, you should check that:

  • The termination line does not have Windows line-ending

  • There is no trailing whitespace on the termination line. (It also must not be indented.)

rici
  • 234,347
  • 28
  • 237
  • 341