bash - 如何使用双管 || 与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
解决方案
推荐阅读
- symfony - html 格式的 symfony/var-dumper 实时或热重载
- node.js - 如何从 Google Cloud Functions NodeJS 连接到 Google Cloud Storage
- java - 如何摆脱 try/catch 块的 IOException 捕获上的 IntelliJ 警告/错误?
- gdb - macOS Mojave 上的 GDB 错误
- angular - 找不到 nebular 的 np-install
- html - 将一排 div 居中在容器中
- firebase - 如何在flutter firestore中获取用户ID
- java - 后台服务被暂停/终止
- azure-functions - 提高 Azure Functions v2 中的并行度
- php - 如何将具有相同值的行从 mySQL 分组到 php 中?