Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multiple pipe symbols inside "|open 1

Status
Not open for further replies.

jdprasad

Programmer
Mar 9, 2006
3
SE
Hi all,

I trying to write the ouput of a program to a tk text widget. The program involved here is the top utility. I am creating a pipe and fileevent to see the information live. My code works if the open command is as follows:

if {[catch {open "|top -u $username -b" r+} input] } {

But the problem is I want to see the outout of only a particular process (a program called 'paraprof' in my case). So I do a grep on the top output as follows:

if {[catch {open "|top -u $username -b |grep paraprof" r+} input] } {

I get no output in the widget in this case. I guess it is due to the other pipe symbol. Is it considered to be a special character by the open command. I have no clue. I have tried many things by to no avail. Hope I will get a solution here. The complete functions inside which I am using this is shown below.
Thank you..
Johnny.
************************************************************
proc redirectnew {} {
global infile input
if {[catch {open "|top -u $username -b|grep paraprof" r+} input] } {
.txt insert end $input \n
} else {
#flush $input
fconfigure $input -blocking 0
fileevent $input readable updateText_new
.txt insert end $input \n
}
}
proc updateText_new {} {
global input
if [eof $input] {
catch {close $input}
} else {
gets $input line
.txt insert end $line\n
.txt see end
}
************************************************************
 
I don't know if "grep" is problematic in this case but I don't think multiple pipes should cause the error. Perhaps a solution is to not use the grep but rather operate on the list returned to input. Something like:
Code:
foreach line $input {
   if {[string first paraprof $line]> -1} {
      .txt insert end "$line \n"
   }
}

_________________
Bob Rashkin
 
Hi,
I have tried your suggestion. But the problem is that $input is not being recognized as a list of items by the foreach command. I guess it is processing the whole output as a single element. Hence it returns the whole output to the screen. Following is the code:
Code:
proc redirectnew {} {
       global infile input
       if {[catch {open "|top -u $username -b" r+} input] } {
          .txt insert end $input \n
       } else {[COLOR=red]
          fconfigure $input -blocking 0
          fileevent $input readable updateText_new
          foreach entry $pipe 
          {
             gets $entry new
             if {[string match ?*paraprof*? [list $new] ] >-1} 
             {
                .txt insert end $new \n
             }
          }  [/color]
        }
}
proc updateText_new {} {
       global input
       if [eof $input] {
          catch {close $input}
       } else {
          gets $input line
          .txt insert end $line\n
          .txt see end
       }
}

Thanks..
Johnny.
 
Hi Bob,
Thanks a lot for your help. Got it to work. I realized that I was performing the string match operations in the wrong place. And I am not using the foreach command since the pipe is already being processed line by line.

Code:
proc redirect {} {
        global infile pipe
        set pipe [open "|top -u johnny -b"  r+]
        fconfigure $pipe -blocking 0
        fileevent $pipe readable [list updateText $pipe ]
}

proc updateText {pipe} {
        if [eof $pipe] {
           catch {close $pipe}
        } else {[COLOR=red]
           gets $pipe line
           if { [string first paraprof [list $line]] >-1} {
             .txt insert end $line\n
           }[/color]
           .txt see end
        }

}

Thanks again,
Johnny.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top