Shebang for psql

↘锁芯ラ 提交于 2019-12-04 12:17:31

问题


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?


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/3995326/shebang-for-psql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!