重定向EOF标准

时间:2016-06-30 07:25:18

标签: shell sh

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

"UPDATE 1
UPDATE 1
UPDATE 1
UPDATE 1
ERROR:  syntax error at or near "EOF"
LINE 1: EOF 2>&1 /dev/null"        ^

如何重定向stdout?

文件:

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

3 个答案:

答案 0 :(得分:1)

您收到的错误消息是由于脚本中的语法错误造成的。

您似乎正在使用的here-document需要使用<<EOF而非仅<<启动:

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

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

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

还要将错误流重定向到同一文件:

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 :(得分:0)

您可以尝试这样:

  

命令&gt; / dev / null 2&gt;&amp; 1

答案 2 :(得分:0)

我想这个bash的例子符合作者的要求:

gnuplot -p << EOF > /dev/null 2>/dev/null
plot 'file.dat'
EOF

我叫gnuplot而不是psql,但是关于EOF和重定向的想法是相同的。