从 psql 生成空输出 [英] Making an empty output from psql

查看:55
本文介绍了从 psql 生成空输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个执行复杂查询的 psql 命令.查询不会产生任何结果,因此 psql 在输出中返回(无行)".

有没有办法让 psql 返回一个空字符串?

我已经尝试在所有变体中使用 --pset=tuples-only=on 和 --pset=footer=off 和 -q,但它似乎不起作用.

页脚选项在 psql shell 提示符下有效,但在脚本中无效.

在 9.1.7 上试过,8.4、9.1 和 9.2 需要这个.

解决方案

可能已经够好了:

$ psql -Axt -c 'select 1 where 1=0'

产生一个空字符串

编辑以下评论:上面的命令产生一个空行,因此包含和行尾.

要什么都不产生,请删除 -x 选项.

无论如何它不会对外壳产生任何影响,如下所示:

使用 -x:

<预>r=`psql -Atx -d test -c "select 1 where 1=0"`回声 $r |od -c0000000 \n0000001

没有 -x:

<预>r=`psql -At -d test -c "select 1 where 1=0"`回声 $r |od -c0000000 \n0000001

I am running a psql command that executes a complex query. There's nothing that query produces, as such, psql returns "(No rows)" in the output.

Is there a way to make psql to return an empty string?

I've tried using --pset=tuples-only=on and --pset=footer=off and -q in all variations, and it doesn't seem to work.

Footer option works while in psql shell prompt, but doesn't work from script.

Tried on 9.1.7, need this for 8.4, 9.1 and 9.2.

解决方案

May be good enough:

$ psql -Axt -c 'select 1 where 1=0'

produces an empty string

EDIT following comments: The command above produces an empty line, so that includes and end-of-line.

To produce nothing at all, remove the -x option.

Not that it would make any difference to the shell anyway, as shown below:

with -x:

r=`psql -Atx -d test -c "select 1 where 1=0"`  
echo $r | od -c  
0000000  \n  
0000001  

without -x:

r=`psql -At -d test -c "select 1 where 1=0"`
echo $r | od -c
0000000  \n
0000001

这篇关于从 psql 生成空输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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