0

The problem is simple:

echo ".tables" | sqlite3 $myPathVariable

If I do: PATH=/my_path it works but i I do myPathVariable="my_path" it fails. The path is retrieved after getting the result of a grep+sed command like myPathVariable=`... grep ... sed`

I need to retrieve dynamically the myPathVariable at execution time, can you please help me to achieve that, I must be missing something basic!

By the way: echo ".tables" | sqlite3 my_path works but echo ".tables" | sqlite3 "my_path"

Edit: the variable PATH was just a bad name here, it doesn t refer to the PATH env variable, sorry :) . Then I noticed that the problem lies within sqlite3: The command .open ~/myDatabaseFilePath fails. The command .open "~/myDatabaseFilePath" fails. The command .open "/.../myDatabaseFilePath" fails. But the command .open /.../myDatabaseFilePath works. So sqlite3 can't deal with path containing '~' or surrounded by quotes!

The path generated at execution is correct, the file exists. It's just a workarround but it doesn't explain why it doesn't work!

I'm just curious to know why the behaviour is not the same ?

Thanks for your help!

sybil
  • 46
  • 3
  • 2
    `PATH` is a [very important shell variable](https://www.gnu.org/software/bash/manual/bashref.html#index-PATH). Don't overwrite it. I'm surprised your shell can find sqlite3. Get out of the habit of using ALL_CAPS_VARNAMES – glenn jackman Jul 13 '16 at 00:37
  • The answer to your question is most likely: given your current directory (`pwd`), is "my_path" the correct *relative* directory? – glenn jackman Jul 13 '16 at 00:41
  • Please provide a more realistic example; the literal strings `my_path` and `"my_path"` are *absolutely* identical to the shell. – chepner Jul 13 '16 at 01:30

1 Answers1

0

Once you obtain the db name (which by the way is not a path but a filename) by whatever means you are using, check if it exists and is readable.

dbname=$(... grep ... sed ... whatever)
if [[ ! -r "$dbname" ]]
then
    echo "$dbname does not exist or is not readable" >&2
    exit 1
fi
sqlite3 "$dbname" .tables
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134