Unix/Linux

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • bash redirection stdout and stderr - pipeline command causes subshell execution

    2 answers - 1434 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    If you know in advance what pipes you need, you can have the entire
    script run inside a pair of braces (or anything equivalent), and apply
    a bunch of redirections to the construct, like this:
    exec 4>&1 5>&1 # save the original stdout and stderr
    {
    {
    exec 6>&1 7>&2 # Save the pipes 6->file1&2 and 7->file1
    {
    {
    echo "This goes to file3"
    echo "This goes to file3 and file4" >&2
    # swap 1&2 and 4&5
    exec 3>&1 1>&4 4>&3 3>&2 2>&5 5>&3
    echo "This goes to orig-stdout"
    echo "This goes to orig-stderr" >&2
    command1
    command2 args >&4 2>&5 # stdout to file3 and file4,
    # stderr to file4.
    command3 >&6 2>&7 # stdout to file1 and file2,
    # stderr to file2.
    command4
    } 3>&2 2>&1 1>&3 | tee file4
    } &file3
    } 3>&2 2>&1 1>&3 | tee file2
    } &file1
    However, if the need to open files arises dynamically, I do not know.
    , yes, you can probably do something like this.
    let n++
    mknod /tmp/mypipe$n-totee p
    exec 9>proc$n.log
    tee proc$n.err </tmp/mypipe$n-totee >&9
    func >&9 2>/tmp/tmp/mypipe$n-totee
    Provided your platform supports named pipes.
    I guess you will have to experiment a little (or read op a lot :) ) to
    determine if the tee process terminates when the func closes its files.
    I hope this helps
    Enrique
  • No.1 | | 352 bytes | |

    enrio@online.no wrote:

    If you know in advance what pipes you need, you can have the entire
    script run inside a pair of braces (or anything equivalent), and apply
    a bunch of redirections to the construct, like this:

    Enrique,

    Many thanks for your answer, allow me to read, digest and test, I will reply
    then.

    Regards.
  • No.2 | | 1661 bytes | |

    enrio@online.no wrote:

    If you know in advance what pipes you need, you can have the entire
    script run inside a pair of braces (or anything equivalent), and apply
    a bunch of redirections to the construct, like this:

    Hi again,

    I have played with your script, but I cannot find a way to adapt it to my
    situation. I'm probably not thinking straight. I'm still musing over the
    named pipes though, maybe there is some way to adapt them.

    I'll try and make things clearer.

    I have a generic script, which assists me in building software packages.

    It goes like this (roughly, there is more to it):

    Fprepare() {
    command 1 &&
    command 2 &&
    cd whereever &&
    VAR='required all thru the script'
    }

    Fconfigure() {
    configure &&
    VAR2='required all thru the script'
    }

    Fmake() {
    make
    }

    Finstall() {
    make install
    }

    { Fprepare 3>&1 1>&2 2>&3 | tee Fprepare.err } &Fprepare.log
    # error checking

    { Fconfigure 3>&1 1>&2 2>&3 | tee Fconfigure.err } &Fconfigure.log
    # error checking

    { Fmake 3>&1 1>&2 2>&3 | tee Fmake.err } &Fmake.log
    # error checking

    { Finstall 3>&1 1>&2 2>&3 | tee Finstall.err } &Finstall.log
    # error checking

    What I require is, any environmental alteration within the functions, should
    be available to the other functions, whilst keeping the separate log and
    error files.

    I'm not sure it is achievable without placing the cds and the variable
    setting commands outside the functions, in which case logging is lost.

Re: bash redirection stdout and stderr - pipeline command causes subshell execution


max 4000 letters.
Your nickname that display:
In order to stop the spam: 8 + 7 =
QUESTION ON "Unix/Linux"

EMSDN.COM