shafeeq shafeeq - 1 year ago 89
Bash Question

Escape echo output from redirecting to a file

I have a script like the following

function long_proc()
#script1 that generate output1
#script2 that generate output2

long_proc > /tmp/debug.log

My requirement is the complete log should divert to
where as the output of script2 should go to
as well as to the log file.

can any one help me?

123 123
Answer Source

So this is what i think you want to do.

To keep output on stdout but also go to file

    exec 4>&1
    exec 1>&3

    #script1 that generate output1
    echo Just log
    #script1 that generate output2
    echo Log and Stdout | tee >(cat - >&4)

    exec 1>&4
    exec 4>&-

long_proc 3> log

So redirect all output to fd3, which is then all piped into log. Tee into stdout anything that you want to also be in stdout.

Benefit of this is that you can then pipe like

long_proc 3> log | sed 's/^/Piped and /'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download