发送带有awk输出列的电子邮件

时间:2019-02-05 12:48:46

标签: linux awk

例如,如果第3栏的$ 100大于100,您能帮我发送电子邮件吗?

   host@root:> report_alias  | awk '{ if($3 >= 100) { mailx -s "FILES REPORT" < "FLOW" $1,$2,$3 " has problems" example@host.ro ;}}'
    awk: { if($3 >= 100) { mailx -s "FILES REPORT" < "FLOW" $1,$2,$3 " has problems" example@host.ro ;}}
    awk:                                                      ^ syntax error
    awk: { if($3 >= 100) { mailx -s "FILES REPORT" < "FLOW" $1,$2,$3 " has problems" example@host.ro ;}}
    awk:                                                                                     ^ syntax error
    awk: { if($3 >= 100) { mailx -s "FILES REPORT" < "FLOW" $1,$2,$3 " has problems" example@host.ro ;}}
    awk:   

“报告别名”的输出

Flow REPORT 1 3,450 has problems
Flow REPORT 2 3,154 has problems
Flow REPORT 3 134 has problems
Flow REPORT 4 134 has problems
Flow REPORT 5 has problems
Flow REPORT 6 has problems

1 个答案:

答案 0 :(得分:1)

尝试一下。

report_alias |
awk '$3 >= 100 { print "FLOW" $1, $2, $3 " has problems"}' |
mailx -s "FILES REPORT" example@host.ro

如果Awk没有输出,它将发送一条空消息。常见的解决方法是将输出保存到临时文件中,检查其是否为空,然后(如果不是)发送一条消息。

#!/bin/sh

t=$(mktemp -t report_alias.XXXXXXXXX) || exit
trap 'rm -f $t' EXIT
trap 'exit 1' HUP INT TERM

report_alias |
awk '$3 >= 100 { print "FLOW" $1, $2, $3 " has problems"}' >"$t"

if [ -s "$t" ]; then
    mailx -s "FILES REPORT" example@host.ro <"$t"
fi