如何在bash中获取psql结果?
我试图在 bash 中获取 psql 结果
echo 'SELECT * FROM fictive_table LIMIT 1;' >> /tmp/x.sql
psql --single-transaction -d dbname -f /tmp/x.sql
echo $?
这会失败,但我在 bash 中得到的结果是 0。逻辑/代码有问题吗?
来自手册
如果 psql 正常完成,则向 shell 返回 0;如果发生致命错误,则返回 1 发生自身错误(内存不足、找不到文件)
更新: @Andrea Spadaccini 是对的。这并不致命。我怎样才能抓住它?
I'm trying to get the psql result in bash
echo 'SELECT * FROM fictive_table LIMIT 1;' >> /tmp/x.sql
psql --single-transaction -d dbname -f /tmp/x.sql
echo $?
This will fail but the result I get in bash is 0. Is there a problem with the logic/code?
From the manual
psql returns 0 to the shell if it finished normally, 1 if a fatal
error of its own (out of memory, file not found) occurs
Update:
@Andrea Spadaccini is right. This is not fatal. How can I catch it ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许查询触发的错误并不像手册中提到的那样致命,即您不会耗尽内存,也不会出现文件未找到错误。
进一步阅读手册,我读到:
要捕获错误,您可以尝试:
ON_ERROR_STOP
变量;或者,Maybe the kind of error triggered by the query is not a fatal one like the ones mentioned in the manual, i.e., you don't run out of memory and it does not get a file not found error.
Reading a bit further in the manual, I read that:
To catch your error you can try to:
ON_ERROR_STOP
variable; or,使用选项
-o
将查询结果从 psql(命令行终端)写入文件。每个文档:
Use the option
-o
to write results of a query from psql (the command line terminal) to a file.Per documentation:
[我知道...这是旧...]
我发现这适用于我从 bash 运行 psql 命令的用例:
我在我的 nextcloud 服务器上使用 psql - 这是一个示例命令:
[I know... this is old...]
I discovered that this works for my use case for running psql commands from bash:
I'm using psql on my nextcloud server - here's a sample command: