exec 2> >(Log)
And then I added my Log function to the script
Log() {
    if [ -z "$1" ]; then
        read Message
    else
        Message="$1"
    fi
    ....
}
And it was working great, until I tested it in Born Shell. Here I got an redirect error. So I needed to look around for another way to do this and I stumbled across an article from Chris Siebenmann. This is a great example, but I needed to append this to the whole script and not just one single command. This however was easy enough, I just needed to append a sub-process to all of my script content.
Log() {
    if [ -z "$1" ]; then
        read Message
    else
        Message="$1"
    fi
    if [ -n "$Message" ]; then
        ....
    fi
}
exec 3>&1
(
    .... Execute everything from here
) 2>&1 >&3 3>&- | Log
Now everything from stderr will be parsed to the function, and it will work with both Bash and Sh.
 
No comments:
Post a Comment