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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get BASH to interpret pipes in a string correctly?
# 1  
Old 04-02-2008
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:

Code:
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:

Code:
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:
Code:
tail -f local_log_file | gawk my_gawk.awk >> some_other_log

then doing:

Code:
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
# 2  
Old 04-02-2008
try eval "command 1 2 3 | command2"
example
Code:
csadev:/home/jmcnama> p="ls -l | grep '^d'"
csadev:/home/jmcnama> eval "$p"

# 3  
Old 04-02-2008
Thanks Jim!

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

Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Escape bash-special character in a bash string

Hi, I am new in bash scripting. In my work, I provide support to several users and when I connect to their computers I use the same admin and password, so I am trying to create a script that will only ask me for the IP address and then connect to the computer without having me to type the user... (5 Replies)
Discussion started by: arcoa05
5 Replies

2. UNIX for Beginners Questions & Answers

Who -r interpret?

I booted into single user mode with /usr/sbin/reboot -- -s but after doing a control -d my who -r shows run-level 3 Nov 17 14:07 3 0 S I was expecting it to show run-level S why is this still in run level 3? thanks (1 Reply)
Discussion started by: goya
1 Replies

3. Programming

Fork and Execvp Not Executing Bash Commands From C correctly.

I had been looking at page 75 of this online book: http://richard.esplins.org/static/downloads/linux_book.pdf I've used the system function in C to call bash commands before, but wanted to learn this way too. The solution in the book worked perfectly. However, I tried changing the simple "ls -l... (3 Replies)
Discussion started by: Azrael
3 Replies

4. UNIX for Dummies Questions & Answers

Bash does not wrap long lines correctly

Ksh is my default shell, but I want use the bash shell since its convenient to me. When I type a long command line in a terminal, it does not wrap to the next line when I reach the end of the line and it wraps onto the same line, overwriting my prompt and the rest of what I typed. $... (5 Replies)
Discussion started by: senthil.ak
5 Replies

5. Shell Programming and Scripting

redirect an awk string output to a script input with pipes

Hi, I have a function in a bash script that returns a string after some operations using awk. The following code returns 555 $VARIABLE="EXAMPLE" get_number $VARIABLE this value I'd like to pass it as a second argument of another script with the following usage myscript.sh <param1>... (7 Replies)
Discussion started by: rid
7 Replies

6. Shell Programming and Scripting

Can someone interpret this -- not sure

Was wondering if someone could interpret this for me -- I'm not sure what everything means. It's a shell script from my bash book: cd () { builtin cd "$@" es=$? echo "$OLDPWD ->$PWD" return $es } what I don't quite understand is the "$@". I think, if I understand... (6 Replies)
Discussion started by: Straitsfan
6 Replies

7. Shell Programming and Scripting

Bash Tarring not un Tarring correctly

HI All, Im encountering behaviour that is not correct for my requirements when I untar a file. Im using the below command to tar up files from various folders to the ARCHIVE folder as below... tar -cvf "$ARCHIVE_PATH"/"$dte_tar_filename" "$LOG_PATH" "$PROCESSED_PATH2" "$ERROR_PATH" ... (5 Replies)
Discussion started by: satnamx
5 Replies

8. UNIX for Dummies Questions & Answers

Please interpret.

Hi guys, I have no idea on unix but suddenly, my cobol programs calls a unix script that i know nothing about. can you guys interpret these lines for me? i know its a print command but I want to actually know how many copies it prints. qprt -da -P $1 -t '6' -i '6' -l '70' $2 qprt -da... (1 Reply)
Discussion started by: supacow
1 Replies

9. Shell Programming and Scripting

How Do We Interpret This ?

ksh $ETL_XXX/bin/filename.ksh wf_workflowname . Which is used in post session command. (2 Replies)
Discussion started by: dummy_needhelp
2 Replies

10. Filesystems, Disks and Memory

PIPEs and Named PIPEs (FIFO) Buffer size

Hello! How I can increase or decrease predefined pipe buffer size? System FreeBSD 4.9 and RedHat Linux 9.0 Thanks! (1 Reply)
Discussion started by: Jus
1 Replies
Login or Register to Ask a Question