You are not logged in.

#1 2010-06-02 08:41:17

awagner
Member
From: Mainz, Germany
Registered: 2007-08-24
Posts: 191

Can I break a pipe if there's empty input?

Hi all,

trying to pimp my crontab, I encountered some difficulties. I want to process logfiles (msmtp and procmail for what it's worth, with grep and mailstat) and have the results in my mailbox. Of course only if there are any.

So, here's one of my scripts:

#!/bin/dash

LOGFILE=$HOME/Maildir/_msmtp.log
SUCCESS="exitcode=EX_OK"

egrep -v $SUCCESS $LOGFILE | /usr/bin/mailx -E -s "[SMTP Warning]" $USER

The only reason I am using mailx-heirloom is the "-E" flag which tells it to silently discard mails with an empty body. If only I could break the pipe in the grep step when this yields no lines, I could use any other mail program (sendmail/esmtp).
Does anyone know how I should go about this?

(The other one would go like this:

#!/bin/dash

LOGFILE=$HOME/Maildir/_procmail.log

mailstat -s $LOGFILE | /usr/bin/mailx -E -s "[Procmail Summary]" $USER

)

Thanks for any help,
Andreas

Offline

#2 2010-06-02 23:13:53

chpln
Member
From: Australia
Registered: 2009-09-17
Posts: 361

Re: Can I break a pipe if there's empty input?

I'm not sure of a clean way to do this.  You could write the output to a temporary file and check the contents before mailing.

Also, fcron is able to mail the output of a command when it returns a non-zero exit status.

Offline

#3 2010-06-03 00:13:46

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,799
Website

Re: Can I break a pipe if there's empty input?

the cleanest way i can come up with:

#!/bin/bash

mymail() {
  local output

  output="$(cat /dev/stdin)"

  [[ -n "$output" ]] && echo $output | mail
}

grep 'foo' ./file | mymail

at first i tried inserting a sorta check function into the pipeline that would call exit on empty input; that didn't work b/c bash executes each link in a pipeline in its own subshell.  this means any call to exit only affects that link.

this is why you need to wrap mail, that's the link you want to control, so put your logic there.

as always, untested.

Offline

#4 2010-06-06 19:36:36

awagner
Member
From: Mainz, Germany
Registered: 2007-08-24
Posts: 191

Re: Can I break a pipe if there's empty input?

Great. Starting from brisbin33's suggestion, I have gotten some results. If it weren't for the necessary formail tinkering to get the mail in proper format, I would already post my solution. But as far as the question of the thread is concerned, the solution is brinbin33's.

Thanks a lot.
Andreas

Offline

#5 2010-06-07 10:15:36

Tes--
Member
Registered: 2009-11-13
Posts: 44

Re: Can I break a pipe if there's empty input?

You could also install "moreutils" and use the "ifne" command.

grep 'foo' ./file | ifne mail

Offline

#6 2010-06-11 20:05:11

jadamson
Member
Registered: 2010-06-11
Posts: 1

Re: Can I break a pipe if there's empty input?

You could use a regular 'if statement'. It's not the most efficient, but it's simple.

if egrep -v $SUCCESS $LOGFILE ; then egrep -v $SUCCESS $LOGFILE | /usr/bin/mailx -E -s "[SMTP Warning]" $USER ; fi

Last edited by jadamson (2010-06-14 17:10:37)

Offline

Board footer

Powered by FluxBB