![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|