Here,
I have the following working sql script, which takes /dev/stdin as
input. Then I want to convert it to a here document. But it doesn't
work, as shown below.
I think that this may not be a sqlite3 problem. Rather, it may be
because I don't use here document and pipe correctly. Could any bash
expert take a look see if there is anything wrong with my usage of
pipe and here document?
################### a working sql script
$ cat main.sql
create table test (id integer primary key, value text);
.separator "\t"
.import /dev/stdin test
.headers on
.mode column
select * from test;
$ cat main.sh
#!/usr/bin/env bash
rm -rf main.db
cat file.txt | sqlite3 main.db '.read main.sql'
$ cat file.txt
1 eenie
2 meenie
3 miny
4 mo
$ ./main.sh
id value
---------- ----------
1 eenie
2 meenie
3 miny
4 mo
#######################
#######################Converted to bash here document, which doesn't work.
$ cat main.sql
#!/usr/bin/env bash
rm -rf main.db
cat file.txt | sqlite3 main.db <<EOF
create table test (id integer primary key, value text);
.separator "\t"
.import /dev/stdin test
.headers on
.mode column
select * from test;
EOF
$ ./main.sql
Error: /dev/stdin line 1: expected 2 columns of data but found 1
####################
--
Regards,
Peng