使 SQLite3 命令文件可执行

pjv*_*aar 2 sqlite macos executable shebang

我有一个 SQLite3 命令文件。例如,

.print "This is running in SQLite3!"
Run Code Online (Sandbox Code Playgroud)

我想要的行为

sqlite3 < commands.sql
Run Code Online (Sandbox Code Playgroud)

当我在 OSX 上运行以下命令时:

./commands.sql
Run Code Online (Sandbox Code Playgroud)

这是我目前的解决方案:

#!/usr/bin/env sqlite3 -init
.print "This is running in SQLite3!"
Run Code Online (Sandbox Code Playgroud)

这有效,但它也会打印一些不需要的行:

-- Loading resources from ./process_errors.sql
Error: near line 1: unrecognized token: "#"
This is running in SQLite3!    
Run Code Online (Sandbox Code Playgroud)

alp*_*per 5

应该管用

#!/usr/bin/env bash
tail -n +4 "$0" | sqlite3
exit $?

-- sql commands
select * from some_table
Run Code Online (Sandbox Code Playgroud)