首页 > 解决方案 > 如何使用双管 || 与heredoc?

问题描述

我正在尝试完成一个相当简单的目标:使用第二个命令对上一个命令的可能错误做出反应。辐条中的扳手是需要在第一个命令中使用heredoc语法。

这个(简单的例子)会产生我想要捕捉的结果:

psql -c "select * from table_that_doesnt_exist" || echo "error"

除了,我需要执行的 SQL 是多个命令,对于我的情况,我必须使用 heredocs 执行此操作:

psql << SQL
select * from good_table;
select * from table_that_doesnt_exist
SQL

当试图从这种类型的配置中成功读取标准错误时(我已经尝试了一百万种方法),我似乎无法弄清楚。这些方法不起作用:

( psql << SQL
select * from good_table;
select * from table_that_doesnt_exist
SQL
) || echo "error"

或者

psql << SQL || echo "error"
select * from good_table;
select * from table_that_doesnt_exist
SQL

标签: bashpsql

解决方案


推荐阅读