You are not logged in.

#1 2009-03-10 20:29:55

deltaecho
Member
From: Georgia (USA)
Registered: 2008-08-06
Posts: 185

Bash function/script to execute multiple commands on same output

Occasionally, I come across the need to pass the output of a single command to multiple others (without piping each to the next), so today, I wrote a [very] simple Bash script to accomplish the task.  Piping the output of a command to `tree' is much like piping it to `tee', except `tree' allows you to execute multiple commands on the same output, rather than only writing to log files:

#!/bin/bash
#
# tree -- pipes the output of one command to >= 1 other
#
# Usage: command | tree 'command 1' 'command 2'

doc=$( cat - )

for arg in "$@"; do
bash -c "${arg}" <<EOF
${doc}
EOF
done

An example of its usage:

$ echo -e "1 2 3\n4 5 6" | tree 'grep 1' 'grep 4' "grep 1 | tee grep.log | awk '{print \$2}'"
1 2 3
4 5 6
2

$ cat grep.log
1 2 3

Though not elaborate, the script gets the job done.

Last edited by deltaecho (2009-03-10 20:34:01)


Dylon

Offline

#2 2009-03-10 20:41:12

Heller_Barde
Member
Registered: 2008-04-01
Posts: 245

Re: Bash function/script to execute multiple commands on same output

the name is a little unfortunate. there is already a tree utility

cheers Barde

Offline

#3 2009-03-10 20:48:17

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,963
Website

Re: Bash function/script to execute multiple commands on same output

You could always call it "the Ukrainian Pipe" tongue

(if you can't laugh at this, you either need to loosen up or make a point to follow (annual) current events)


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#4 2009-03-10 20:48:19

deltaecho
Member
From: Georgia (USA)
Registered: 2008-08-06
Posts: 185

Re: Bash function/script to execute multiple commands on same output

Heller_Barde wrote:

the name is a little unfortunate. there is already a tree utility

cheers Barde

...so there is...

My conscience is clear, though, because I didn't know about it until after your post tongue  If the script creates a naming conflict, you may definitely rename it anything you please.


Dylon

Offline

#5 2009-03-10 20:49:39

deltaecho
Member
From: Georgia (USA)
Registered: 2008-08-06
Posts: 185

Re: Bash function/script to execute multiple commands on same output

Xyne wrote:

You could always call it "the Ukrainian Pipe" tongue

(if you can't laugh at this, you either need to loosen up or make a point to follow (annual) current events)

That was a good one big_smile


Dylon

Offline

Board footer

Powered by FluxBB