2

I have a folder with multiple .sql files, each creating their own database. I'm trying the below bash script to loop through each file and initialize the database with sqlite3.

for filepath in $(find ~/sql/ -name '*.sql'); do
        sqlite3 "${file}.db" -init "$filepath"
done

My issue is I have to manually type .quit in the terminal to exit sqlite3 before the bash loop continues.

I tried adding .quit but the terminal doesn't recognize the sqlite command. Do I need to add an exit command in the .sql files to exit sqlite from bash's terminal or something else?

EDIT: from the comments, I got the script to run automatically with sqlite3 "${file}.db" < "$filepath".

7
  • Try adding -batch Commented May 5, 2022 at 21:51
  • I just tried sqlite3 -batch "${file}.db" -init "$filepath" and it's still needing manual .quit. Is that right? I also tried adding .quit to each .sql file and got the same issue. Commented May 5, 2022 at 21:57
  • Why are you using -init? Commented May 5, 2022 at 22:27
  • 1
    Also, see sqlite.org/cli.html#using_sqlite3_in_a_shell_script Commented May 5, 2022 at 22:30
  • 1
    sqlite3 "${file}.db" < "$filepath" ? Commented May 5, 2022 at 22:41

1 Answer 1

0

Where you can't/don't want to pass the commands in via standard input, a workaround is to misspell .quit (e.g., .quoot) as the last statement in the -init file, and pass the -bail option on the command-line.

sqlite3 will execute commands in the -init file until reaching one that produces an error (hopefully the intentionally misspelled one), and then terminate with a nonzero status. To exit solely at the end, you can insert .bail on into the file just prior to the misspelled command. In either case, the bash script can then continue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.