tags:

views:

20

answers:

2

I have a bash script that has a few functions which are all called within 1 function. How can I pipe all the output from all the functions up to the main one? I'll be using tee as well to display that output to term and to a log file.

func 1

func 2

func 3
    func 1

func 4 
    func 2
    func 3

call func 4 # i want to grab it here

+1  A: 

As DigitalRoss said, all stdout goes to the same place and piping and teeing works regardless of how deeply functions and scripts are nested (up to system limits). In the demo below, function f4 demonstrates one way of doing it and f5 demonstrates another.

$ f1 () { echo f1; }
$ f2 () { echo f2; }
$ f3 () { echo f3; f1; }
$ f4 () { echo f4; f2; f3; }
$ f4
f4
f2
f3
f1
$ f4 | tee tee.out
f4
f2
f3
f1
$ cat tee.out
f4
f2
f3
f1
$ f5 () { { echo f4; f2; f3; } | tee tee2.out; }
$ f4 | tee tee.out
f4
f2
f3
f1
$ cat tee.out
f4
f2
f3
f1
Dennis Williamson
A: 

Hmm, when in doubt, use ( ) which will run a subshell and redirect its entire output.

So, try something like:

( mytoplevelfunc ) | tee whatever
DigitalRoss
I think this is what I was looking for. Thanks!
zarzar