I'm working on a script to handle some basic mySQL statements, but I need it to be aware of whether or not the statement that was just executed contained an error. Since mySQL prints it's error messages to stderr and exits with status code 0 a little bit of finagling is in order.

The below function takes a mySQL query as a single argument, printing the results as a comma-delimited list, and it worked just fine until I added '2> $err_pipe' and related code. $err_pipe being the named pipe which is created earlier.

Code:
function pf_query {
    if [ $# -ne 1 ]; then
        echo "-- mySQL function error: invalid arguments for query" >&2
        return 1
    else
        mysql -h $pf_h -u $pf_u -p$pf_p -D $pf_d -B -e "$1" --skip-column-names 2> $err_pipe | tr -d ',' | tr "\t" ","
        q_error=`cat $err_pipe`
        if [ -n "$q_error" ]; then
            echo "-- mySQL Errors: $q_error" >&2
            return 1
        else
            return 0
        fi
    fi
}
The truly odd thing is this code seemed to work until I added some code to check if the pipe is created properly prior to the execution of the function:
Code:
err_pipe=/home/user/bin/_include/err_pf
if [ -e $err_pipe ]; then
    echo "pipe exists" >&2
    if [ ! -p $err_pipe ]; then
        echo "Error pipe is not a pipe, re-creating" >&2
        rm $err_pipe
        mkfifo $err_pipe
    else
        echo "pipe is a-ok" >&2
    fi
else
    echo "no pipe, creating." >&2
    mkfifo $err_pipe
fi
With the code as above the script hangs, I believe due to an unconnected pipe. If I add a & to the line with the mysql statement the script completes, but with informational garbage in both stdout and stderr from the backgrounding of the command, ie:
Code:
[2]-  Done                    mysql -h $pf_h -u $pf_u -p$pf_p -D $pf_d -B -e "$1" --skip-column-names 2> $err_pipe | tr -d ',' | tr "\t" ","
Is put into my output, which is not acceptable to the functions that will be using this function.

Admittedly I am not as familiar with named pipes as I would like to be, could someone please point me in the direction of getting this issue resolved?