首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重定向EOF的标准输出

重定向EOF的标准输出
EN

Stack Overflow用户
提问于 2016-06-30 07:25:17
回答 3查看 2.6K关注 0票数 0

我想将错误和输出流重定向到/dev/null,我在脚本文件中使用了2>&1 /dev/null (如下所示),但我得到的错误是

代码语言:javascript
复制
"UPDATE 1
UPDATE 1
UPDATE 1
UPDATE 1
ERROR:  syntax error at or near "EOF"
LINE 1: EOF 2>&1 /dev/null"        ^

如何重定向标准输出?

文件:

代码语言:javascript
复制
psql -h xx.xx.xx.xx -U postgres -d dbname 

<<

update rhq_alert_condition set threshold = $critical_threshold  where alert_definition_id = $critical_id;
update rhq_alert_condition set threshold = $critical_threshold  where alert_definition_id = $critical_recovery_id;
update rhq_alert_condition set threshold = $warning_threshold  where alert_definition_id = $warning_id;
update rhq_alert_condition set threshold = $warning_threshold  where alert_definition_id = $warning_recovery_id;

EOF

2>&1 /dev/null
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-06-30 08:16:46

获得的错误消息是由于脚本中的语法错误造成的。

您所使用的此处文档需要使用<<EOF,而不仅仅是<<

代码语言:javascript
复制
psql -h xx.xx.xx.xx -U postgres -d dbname <<EOF
update rhq_alert_condition set threshold = $critical_threshold  where alert_definition_id = $critical_id;
update rhq_alert_condition set threshold = $critical_threshold  where alert_definition_id = $critical_recovery_id;
update rhq_alert_condition set threshold = $warning_threshold  where alert_definition_id = $warning_id;
update rhq_alert_condition set threshold = $warning_threshold  where alert_definition_id = $warning_recovery_id;
EOF

若要将输出从此重定向到文件:

代码语言:javascript
复制
psql -h xx.xx.xx.xx -U postgres -d dbname >outfile <<EOF
update rhq_alert_condition set threshold = $critical_threshold  where alert_definition_id = $critical_id;
update rhq_alert_condition set threshold = $critical_threshold  where alert_definition_id = $critical_recovery_id;
update rhq_alert_condition set threshold = $warning_threshold  where alert_definition_id = $warning_id;
update rhq_alert_condition set threshold = $warning_threshold  where alert_definition_id = $warning_recovery_id;
EOF

也可以将错误流重定向到同一个文件:

代码语言:javascript
复制
psql -h xx.xx.xx.xx -U postgres -d dbname >outfile 2>&1 <<EOF
update rhq_alert_condition set threshold = $critical_threshold  where alert_definition_id = $critical_id;
update rhq_alert_condition set threshold = $critical_threshold  where alert_definition_id = $critical_recovery_id;
update rhq_alert_condition set threshold = $warning_threshold  where alert_definition_id = $warning_id;
update rhq_alert_condition set threshold = $warning_threshold  where alert_definition_id = $warning_recovery_id;
EOF

若要将错误流转到/dev/null,请改用2>/dev/null

要让这两个流都转到位桶,请使用>/dev/null 2>&1

票数 1
EN

Stack Overflow用户

发布于 2016-06-30 07:57:01

你可以这样做:

命令> /dev/null 2>&1

票数 0
EN

Stack Overflow用户

发布于 2020-07-20 23:28:14

我猜这个bash示例完成了作者询问的内容:

代码语言:javascript
复制
gnuplot -p << EOF > /dev/null 2>/dev/null
plot 'file.dat'
EOF

我调用了gnuplot而不是psql,但是关于EOF和重定向的想法是一样的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38116476

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档