You are not logged in.

#1 2011-05-20 15:02:43

_Mike_
Member
Registered: 2005-03-09
Posts: 30

Bash: Use command output as argument for next command in pipe

I'm trying to do this:

tail -f /var/logfile | grep -C 0 text | mail -s "OUTPUT OF GREP" mail@mail.com

How can I do this?

Offline

#2 2011-05-20 15:14:01

demian
Member
From: Frankfurt, Germany
Registered: 2009-05-06
Posts: 709

Re: Bash: Use command output as argument for next command in pipe

maybe with a variable

tail -f /var/logfile | grepout=$(grep -C 0 test) | mail -s "$grepout" mail@mail.com

// nope, doesn't work tongue

Last edited by demian (2011-05-20 15:17:19)


no place like /home
github

Offline

#3 2011-05-20 15:14:21

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Bash: Use command output as argument for next command in pipe

Haven't tested it but try

mail -s $(tail -f /var/logfile | grep -C 0 text) mail@mail.com

Edit: Are you sure it will work with 'tail -f'?
Seems that the subject can't contain certain characters:

[karol@black ~]$ mail -s $(tail /var/log/kernel.log | grep -C 0 detected) foo@bar.com
18:05:11 contains invalid character ':'
kernel: contains invalid character ':'
[ contains invalid character '['
8.433910] contains invalid character ']'
4-0:1.0: contains invalid character ':'
^C

How will you escape multiple newlines?

Last edited by karol (2011-05-20 15:20:07)

Offline

#4 2011-05-20 15:19:18

demian
Member
From: Frankfurt, Germany
Registered: 2009-05-06
Posts: 709

Re: Bash: Use command output as argument for next command in pipe

I think tail -f will always intefere. You'd have to use normal tail and instead create a loop to check it every ~ secs or so.


no place like /home
github

Offline

#5 2011-05-20 15:23:23

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Bash: Use command output as argument for next command in pipe

How about putting the output inside the e-mail?

echo "$(tail /var/log/kernel.log | grep -C 0 swap)" | mail -s somesubject foo@bar.com

This works nicely.

Offline

#6 2011-05-20 15:29:17

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Bash: Use command output as argument for next command in pipe

tail -f /var/logfile | stdbuf -oL grep  text | while read text; do mail -s "$text" mail@mail.com; done

Offline

#7 2011-05-20 15:33:15

_Mike_
Member
Registered: 2005-03-09
Posts: 30

Re: Bash: Use command output as argument for next command in pipe

It's important that the output goes to the subject.

To simplify , How would you do this?
cat file | grep word | command -x grepoutput

Offline

#8 2011-05-20 15:35:11

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Bash: Use command output as argument for next command in pipe

Did you try what I posted? It does exactly that.

http://partmaps.org/era/unix/award.html

Offline

#9 2011-05-20 15:58:50

_Mike_
Member
Registered: 2005-03-09
Posts: 30

Re: Bash: Use command output as argument for next command in pipe

falconindy: you replied fast I had to reload the page to see your comment tongue

I'm testing your commnad, I still dont fully understand it. But for the little I tested it's not working.
I executed it, waited a bit for many new lines to be added to the log file. Only after I finish the command with CONTROL+C, one mail is send with only 1 of the log lines added to the subject.

Offline

#10 2011-05-20 16:03:39

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Bash: Use command output as argument for next command in pipe

Right... because there's no body to the message.... i suppose you could fix that, hackishly...

tail -f /var/logfile | stdbuf -oL grep  text | while read text; do mail -s "$text" mail@mail.com </dev/null; done

Offline

#11 2011-05-20 16:25:51

_Mike_
Member
Registered: 2005-03-09
Posts: 30

Re: Bash: Use command output as argument for next command in pipe

Thanks falconindy. It works now, you really are a Bash Ninja.

Offline

#12 2011-05-22 13:24:50

stqn
Member
Registered: 2010-03-19
Posts: 1,191
Website

Re: Bash: Use command output as argument for next command in pipe

Please mark as solved...

Offline

Board footer

Powered by FluxBB