psql 的 Shebang

发布于 2024-09-28 06:54:02 字数 195 浏览 3 评论 0原文

我正在尝试编写 PostgreSQL 脚本,但 shebang 行出现问题,

#! /usr/bin/psql [ psql_args_here ] -f

select now();

这给了我错误,就好像我刚刚在命令行中没有任何参数的情况下输入了 psql 一样。我怎样做才正确?

I'm trying to write PostgreSQL script(s) but having a problem with shebang line

#! /usr/bin/psql [ psql_args_here ] -f

select now();

This gives me error as if I just entered psql without any arguments in command line. How do I do it right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

‘画卷フ 2024-10-05 06:54:02

问题是 psql 不会跳过文件的第一行。

你可以尝试

#! /bin/sh
exec sh -c "tail -n +3 $0 | psql -f -"

select now();

或者简单地

#! /bin/sh
psql << E_O_SQL

select now();

E_O_SQL

The problem is that psql don't skip the first line of the file.

You could try

#! /bin/sh
exec sh -c "tail -n +3 $0 | psql -f -"

select now();

or simply

#! /bin/sh
psql << E_O_SQL

select now();

E_O_SQL
云仙小弟 2024-10-05 06:54:02

还有一个更好的解决方案。第一行应该是:

--() { :; }; exec psql -f "$0"

它作为常规的 shebang #!

http://rosettacode.org/wiki/Multiline_shebang#PostgreSQL

There is a even better solution. The first line should be:

--() { :; }; exec psql -f "$0"

It works as a regular shebang #!

http://rosettacode.org/wiki/Multiline_shebang#PostgreSQL

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文