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!

Expect problem "grep -c"

Status
Not open for further replies.

NetworkGhost

IS-IT--Management
Apr 12, 2005
1,324
US
I have the following script


#!/bin/expect
set get_cat [open categories r]
set catlist [split [read $get_cat] \n];
close $get_cat
set incr 0
foreach line $catlist {
set val_i [expr {$incr + 1}]
set prod [lindex $line 0]
set sht_title [lindex $line 1]
set mncat [lindex $line 2]
set catarg [lindex $line 3]
set catarg1 [lindex $line 4]
set mn_url "set fname $prod$sht_title$mncat$val_i
set res_string "v:state=root%7Croot-"
exec wget -q -O $fname v%3Asources=CARE
puts $fname
set incr [expr {$incr + 1}]
set test1 [exec grep -c tot $fname]
puts $test1
set test1 0
}

Problem is when I come across a line count of 0 the script barfs. Here is the output:

$ ./test1.sh
testprodprodtime1
1
testprodprodday2
0
child process exited abnormally
while executing
"exec grep -c tot $fname"
("foreach" body line 14)
invoked from within
"foreach line $catlist {
set val_i [expr {$incr + 1}]
set prod [lindex $line 0]
set sht_title [lindex $line 1]
set mncat [lindex $line 2]
set catarg [l..."
(file "./test1.sh" line 6)


Any ideas? I take this out

set test1 [exec grep -c tot $fname]
puts $test1
set test1 0

and it works fine.



Free Firewall/Network/Systems Support-
 
You get this error whenever exec grep ... returns 0?
It seems you're expecting test1 to reflect the number of lines within $fname that contain tot, but are you sure that's what would happen? I think, rather, exec is going to return 1 or 0, depending on whether grep executed successfully or not. That would imply (to me) that grep isn't working for some reason and it isn't a matter of your Tcl (which seems fine to me).

_________________
Bob Rashkin
 
Well it works when I test for values other than tot. I am expect tot to appear only once anyways. So I was using grep -c to determine if it was worth grabbing the line or not later on the script.

Free Firewall/Network/Systems Support-
 
I would try executing the grep -c from the shell (outside of Tcl/Expect) and see if you can learn something about when it returns 0. As I said, as far as I can tell, your Tcl is constructed just fine.

_________________
Bob Rashkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top