The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
bash script to check the first character in string ole111 Shell Programming and Scripting 2 03-19-2008 12:34 AM
Bash: Can I run in background with pipes jjinno Shell Programming and Scripting 3 01-03-2008 08:51 PM
split files by specifying a string (bash shell) vikas027 Shell Programming and Scripting 12 11-01-2007 01:57 PM
bash: testing if string is a number eur0dad Shell Programming and Scripting 3 07-20-2006 12:50 AM
PIPEs and Named PIPEs (FIFO) Buffer size Jus Filesystems, Disks and Memory 1 08-20-2004 11:14 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-02-2008
neked neked is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 56
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 (permalink)  
Old 04-02-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,813
try eval "command 1 2 3 | command2"
example

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

  #3 (permalink)  
Old 04-02-2008
neked neked is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 56
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:11 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0