1

With the following litte awk script, I get an email notification. But I want to get this email only if $1 is greater or equal than 100.

At which position must I put the if statement? I tried many versions without success. Following my script:

awk '

BEGIN{print "Subject:SASL Spam\nFrom:Server.de <test@emailer.com>\nTo:test@emailer.com"}

END{printf("%s\015\n", $1" | "$2)}

' /home/web0/html/tools/uptime/sasl.txt | /usr/lib/sendmail -t test@emailer.com
  • please give us some example of what is in /home/web0/html/tools/uptime/sasl.txt so we can understand what you mean. – gogoud Dec 2 '15 at 14:05
  • 36 sasl_username=web39p2 66 sasl_username=web57p1 78 sasl_username=web117p1 – Michael Woldrich Dec 2 '15 at 14:07
1

You can do like this (I shortened it a bit for readabillity):

{ awk 'BEGIN{...} END{...} END{if($1<100){exit(1)}}' file || exit; } > >(sendmail -t ...)
  • awk is called in a compound command.
    • if($1<100) checks for the value
    • exit(1) aborts further execution of code inside awk and returns the exit code 1
  • || exit this is executed when the awk command returns false (exit code higher than 0). The shell is then exited and no more commands are executed. I don'0t use the pipe | here because that will open a subshell and this subshell is then exited and the next command is executed anyway.
  • >(sendmail -t ...) the output is sent to sendmail (if not yet exited).

The whole command would then look like:

{ awk 'BEGIN{print "Subject:SASL Spam\nFrom:Server.de <test@emailer.com>\nTo:test@emailer.com"}
  END{printf("%s\015\n", $1" | "$2)} END{if($1<100){exit(1)}}' \
/home/web0/html/tools/uptime/sasl.txt || exit; } > >(/usr/lib/sendmail -t test@emailer.com)
  • This will also send a mail when awk fails when one of the parameters is incorrect. For example is $1 is equal to "TEST". – Marco Dec 2 '15 at 14:16
  • @Marco No when awk fails the part after || is executed, hence no email. And when $1 equals TEST it's no greater or equal than 100... – chaos Dec 2 '15 at 14:20
  • still always sending notification. Email body is "78 - sasl_username=web117p1" It makes no differce: $1>=100 or $1>=9999 – Michael Woldrich Dec 2 '15 at 14:40
  • @MichaelWoldrich Sorry, the statement was wrong: I had $1>=100 instead of $1<100. Please try again, now it should work. – chaos Dec 2 '15 at 14:44
0

So this uses a single awk statement to prepare an email containing data only from the lines in the source file start with a number >=100, if none of them meet this condition then no email is sent, which is I think what you are trying to achieve. It also makes sure that the first field is numeric in case there is some unexpected text there which might confuse the numeric comparison:

awk '
{
  if ($1~/^[0-9]*$/ && $1>=100) {
    if (BEENHERE!=1) print "Subject:SASL Spam\nFrom:Server.de <test@emailer.com>\nTo:test@emailer.com\n"
    printf("%s\015\n", $1" | "$2); BEENHERE=1
  }
}' /home/web0/html/tools/uptime/sasl.txt  | \
/usr/lib/sendmail -t test@emailer.com

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.