在Unix Shell脚本中检查psql命令的返回状态 [英] Check return status of psql command in unix shell scripting
问题描述
我正在使用psql命令连接并在postgreSQL数据库上发出查询。有人可以让我知道如何在Shell脚本中检查已执行查询的返回状态。
I am using psql command to connect and issue a query on postgreSQL database. Can anybody let me know how to check the return status of the executed query in shell script.
我使用了 echo $?命令检查状态,但始终返回零。
I have used echo $?
command to check the status but it always returning zero.
感谢帮助。
推荐答案
psql
返回代码记录为:
退出状态
psql如果正常完成则返回0到外壳程序,如果自身发生致命错误
(例如,内存不足,未找到文件)则返回1,如果
连接到外壳程序则返回2。服务器出现故障,会话无法交互,
和3(如果脚本中发生错误并且设置了变量ON_ERROR_STOP
)。
EXIT STATUS
psql returns 0 to the shell if it finished normally, 1 if a fatal error of its own occurs (e.g. out of memory, file not found), 2 if the connection to the server went bad and the session was not interactive, and 3 if an error occurred in a script and the variable ON_ERROR_STOP was set.
您可能只想使用ON_ERROR_STOP。
You probably just want to use ON_ERROR_STOP.
失败进行测试并向她报告ll:
$ psql -d test -v "ON_ERROR_STOP=1" <<EOF
select error;
select 'OK';
EOF
ERROR: column "error" does not exist
LINE 1: select error;
$ echo $?
3
失败被忽略并且没有报告给Shell:
$ psql -d test <<EOF
select error;
select 'OK';
EOF
ERROR: column "error" does not exist
LINE 1: select error;
^
?column?
----------
OK
(1 row)
$ echo $?
0
这篇关于在Unix Shell脚本中检查psql命令的返回状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!