1

I'm trying to write a bash function in .bash_profile with a parameter that will run a script on my sql and test it, but I get the following error when trying to invoke it:

$ sqltest() ~/Downloads/hw5-2.sql
-bash: syntax error near unexpected token `~/Downloads/hw5-2.sql` 

The parameter is the file path. What I have right now (also have tried "$1" w/o quotes):

sqltest() { java -cp h2-1.4.200.jar org.h2.tools.RunScript  -checkResults -url jdbc:h2:mem:test -script "$1" -continueOnError ; }
  • If that guess is wrong, I'd suggest running `set -x` to enable trace logging, reproducing the error, and *showing us how you did it* with a transcript including both the command you ran and the exact text that resulted (up to and including the error message itself; any trace logs that relate to printing your prompt or other such local hooks don't need to be included). – Charles Duffy Feb 24 '20 at 16:00
  • (BTW, `"$1"` inside quotes is correct). – Charles Duffy Feb 24 '20 at 16:01
  • I am running as `sqltest() ~/Downloads/hw5-2.sql` – Emmanuel Castillo Feb 24 '20 at 16:01
  • Just run `sqltest ~/Downloads/hw5-2.sql`. The `()` is not part of the function name. – Charles Duffy Feb 24 '20 at 16:01
  • or maybe better to use `"$HOME"` since `~` does not expand in quotes, single or double. – Jetchisel Feb 24 '20 at 16:01
  • ...and next time, make sure the question you're asking includes *everything needed to produce the error*. – Charles Duffy Feb 24 '20 at 16:01
  • 1
    @Jetchisel, the expansion is done before the function is started, so `~` works fine. It would be a problem if they were running `sqltest "~/Downloads/hw5-2.sql"`, but they aren't. – Charles Duffy Feb 24 '20 at 16:02
  • @CharlesDuffy ok. – Jetchisel Feb 24 '20 at 16:02
  • @CharlesDuffy Thank you! `sqltest ~/Downloads/hw5-2.sql` without the `()` worked. – Emmanuel Castillo Feb 24 '20 at 16:07
  • Glad to hear! To mark your question solved, click the checkbox by an answer (because this one was duplicative, I answered it "community wiki", so I don't get any points from answer acceptance or upvotes; it just has the effect of marking the question as successfully addressed). – Charles Duffy Feb 24 '20 at 16:11

1 Answers1

1

Leave the parenthesis out of the function call; they're only part of function definition syntax:

sqltest ~/Downloads/hw5-2.sql
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441