如果找到零行,则psql返回代码 [英] psql return code if zero rows found

查看:62
本文介绍了如果找到零行,则psql返回代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果发现零行,我希望我的psql命令失败:

I would like for my psql command to fail if zero rows are found:

psql -U postgres -d db -c "select * from user where id=1 and name='Joe';"

我希望能够检查返回值.如果至少存在一行,则从process(!)返回0;如果不存在这样的行,则从psql进程返回非零.如果找不到行,如何设置返回码?

I want to be able to check the return value. Return 0 from the process(!) if at least one row exists and return non-zero from the psql process if no such row exists. How can I set a return code if no rows are found?

推荐答案

我不认为psql可以单独完成,但是如果您只想查看退出状态是否有任何行,可以组合使用就像

I don't think psql can do it by itself, but if you just want to see if there are any rows or not with the exit status you could combine it like

psql -U postgres -d db -t -c "select * from user where id=1 and name='Joe'" | egrep .

如果egrep无法匹配任何内容,它将导致egrep以非零值退出. -t将使其不打印列标题和摘要信息,因此,如果需要这些内容,则可能需要调整此命令行.

That will cause egrep to exit with non-zero if it cannot match anything. The -t will make it not print the column headers and summary information, so you may need to tweak this command line if you need that stuff.

这篇关于如果找到零行,则psql返回代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆