1

I'd like to run series of sqlite commands in bash script:

db="main.sqlite3"
db2="sub.sqlite3"

sqlite3 ${db} <<EOF
attach ${db2} as m;
select count(*) from m.details;
.exit
EOF

when attach to a string, it works, but when it is a variable, it won't, got

Error: near line 1: near "/": syntax error

how to use a variable(db2) inside EOF? thanks

Cyrus
  • 84,225
  • 14
  • 89
  • 153
AngeloC
  • 3,213
  • 10
  • 31
  • 51
  • 2
    This should work as written. There is no `/` in the snippet you posted, so maybe you simplified it too much? – Thomas Aug 15 '17 at 08:27
  • 1
    I suggest to check your file for special characters: `cat -A file` or `cat -v file` – Cyrus Aug 15 '17 at 08:27
  • 4
    Does the `attach` statement require the dbname to be quoted? Do you need `attach '$db2' as m;` ?? I can't tell from https://sqlite.org/syntax/attach-stmt.html and https://sqlite.org/syntax/expr.html – glenn jackman Aug 15 '17 at 11:51
  • finally, that's the one works: sqlite3 ${db} < – AngeloC Aug 15 '17 at 13:30
  • what you are really asking is this: https://stackoverflow.com/questions/4937792/using-variables-inside-a-bash-heredoc – jrwren Aug 16 '17 at 12:58
  • 1
    @jrwren No, because that question is about quoting "EOF"... – Murphy Aug 16 '17 at 12:59
  • Thanks @murphy you are right. The real issue is that attach doesn't accept parameter starting with / if it is unquoted. – jrwren Aug 16 '17 at 13:14

1 Answers1

2

Try putting the DB file to attach into double quotes:

attach "${db2}" as m;
Murphy
  • 3,827
  • 4
  • 21
  • 35