How to get BASH to interpret pipes in a string correctly?

In a BASH library I'm creating, I have two functions that look like:

function check_process {
    PIDFILE=$1
    if [ -f $PIDFILE ]; then
        PID=`cat $PIDFILE`
        if [ ! "$PID" == "" ] && [ -d "/proc/$PID" ]; then
            return 1
        fi;
    fi;
    return 0
}

function fork_process {
    CMD=$1
    PIDFILE=$2
    if check_process $PIDFILE; then
        $CMD &
        echo $! > $PIDFILE
        echo "new pid in $PIDFILE on `date`" >> $LOG
    fi;
}

Which I use to launch, keep track of the PID, and prevent duplicates of any process in a system independent manner (there exists utilities like start-stop-daemon and startproc on different linux flavors, but they are not universal).

So if I want to use it to track a process over an SSH connection, I could do something like:

fork_process "ssh some_user@some_server tail -f some_log_file >> local_log_file" my_process.pid

This would check the file my_process.pid and make sure that the process ID is not already running before going ahead to run the SSH connection and fork it in the background, while saving the new PID in the pid file.

The problem is, if I have a command like this:

tail -f local_log_file | gawk my_gawk.awk >> some_other_log

then doing:

fork_process "tail -f local_log_file | gawk my_gawk.awk >> some_other_log" my_process.pid

will not work, because BASH will interpret the '|' character literally and not as a pipe. Is there a way to force BASH to interpret '|' as a pipe even when it is in a string?

Thanks,
Neked

try eval "command 1 2 3 | command2"
example

csadev:/home/jmcnama> p="ls -l | grep '^d'"
csadev:/home/jmcnama> eval "$p"

Thanks Jim!

I put the "eval" operator in my fork_process function like this:

function fork_process {
    CMD=$1
    PIDFILE=$2
    if check_process $PIDFILE; then
        eval "$CMD &"
        echo $! > $PIDFILE
        echo "new pid in $PIDFILE on `date`" >> $TAILER_LOG
    fi;
}

And that solved the problem.