0

I wish to run a bash script that asks for a variable to be then used in a sqlite query.

I have no real experience in scripting, anyway I've tried something like the following but it doesn't work. Doesn't even give an error, just it doesn't show anything.

#!/bin/bash
echo name   
read name   
sqlite3 /arch.db << 'EOF'
.headers on
select type, number, address from documents where name = '$name';
EOF

I'd appreciate any help.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
M.I.
  • 1

1 Answers1

-1

Before answering about your script: are you sure that the path is correct? This path: sqlite3 /arch.db means that arch.db is searched in the root /.

Perhaps you meant ./arch.db? So that the sqlite db file is looked in the same path of the script?

Please note that **the $name var is quoted in two ', that means that the variable is not treated as such, but as a string.

Your script could be rewritten in this way:

#!/bin/bash
echo name   
read name   
sqlite3 ./arch.db <<EOF
.headers on
select type, number, address from documents where name = "$name";
EOF
  • The <<'EOF' correctly introduces a quoted here-document. There is nothing wrong with the quoting of EOF at the start of the here-document, other than the fact that it stops $name from being expanded. The single quotes around $name inside the here-document is just text, not shell syntax, so they have no meaning to the shell. Note that there is a huge difference between using single and double quotes in an SQL statement and that "something" refers to a column in a table, whereas 'something' is a string. – Kusalananda Aug 16 '22 at 09:06