From: Gary on
My tcl script is failing to open/read/process all of a file past a
certain
point. After examining the output and comparing in a text editor I see
that
the script is displaying about 213 lines, 1222 words, and 8875 bytes
(as per
the gnu wc utility) before the operation stops and I'm back at the
shell
prompt. What I'm expecting to happen is the file gets read and output
is
displayed a line at a time.

This is defintely a FAQ and I've had lots of examples to try. A few
of the
methods I've tried using are listed at the bottom. They were
attempted one at
a time, not all at once in the same script.

I've tried Ubuntu Linux and FC9 both using TCL 8.4 with similar
results. The
data is plain text and a shell script can open it as can a Perl
script.

The file size is about 247798 bytes. It doesn't seem like it should
be too
big?

I suppose I could try to use some form of exec and cat but now this
has become
a quest to understand why it's failing.

FWIW - I'm working on the exercises in Exploring Expect(Chapter 3)
where I
need to look for text in rfc-index.txt and perform further actions
based on
data in the file. My first step is to get the data one line at a time
after
which I'll move on to the next step. The file can be retrieved from
here:
ftp://metalab.unc.edu/pub/docs/rfc/rfc-index.txt


# Attempt 1
set fp [open "rfc-index.txt" r]
fconfigure $fp -buffering line
gets $fp data
while {$data != ""} {
puts $data
gets $fp data
}
close $fp

# Attempt 2
set fp [open "rfc-index.txt" r]
while { [gets $fp data] >= 0 } {
puts $data
}
close $fp

# Attempt 3
set fp [open "rfc-index.txt" r]
while {-1 != [gets $fp line]} {
puts "$line"
}
close $fp

# Attempt 4
set fp [open rfc-index.txt r]
while 1 {
gets $fp line
if [eof $fp] break
puts "$line"
}

# Attempt 5
if [catch {open rfc-index.txt r} fid] {
puts "error opening rfc-index.txt ($fid)"
exit
}
set buffer [read $fid]
close $fid
foreach line [split $buffer \n] {
puts "$line"
}

Thank-you for your time.
Gary
From: jr4412 on
hi Gary,

> here:ftp://metalab.unc.edu/pub/docs/rfc/rfc-index.txt
> #  Attempt 2
> set fp [open "rfc-index.txt" r]
> while { [gets $fp data] >= 0 } {
>      puts $data}
>
> close $fp

downloaded the text file and entered code for attempt 2 exactly as
shown (into a simple text file called tst); using tcl8.4 on a
slackware 12.0 machine, this works for me (ie. run tclsh, then source
'tst'). sorry, no idea why this won't work for you.
From: Gary on
On Feb 6, 8:04 pm, jr4412 <jr4...(a)googlemail.com> wrote:
Hi jr4
>
> downloaded the text file and entered code for attempt 2 exactly as
> shown (into a simple text file called tst); using tcl8.4 on a
> slackware 12.0 machine, this works for me (ie. run tclsh, then source
> 'tst').  sorry, no idea why this won't work for you.

Thanks for looking. At the tclsh this is working for me as well.
Seems odd to me.
From: jr4412 on
hi Gary,

> Thanks for looking.  At the tclsh this is working for me as well.

so you only have problems when making the script an execuable (adding
hashbang & setting perms)?
again, works here.

> Seems odd to me.

yes, very.

care to post the script which works when source'd?
From: Gary on
On Feb 6, 10:27 pm, jr4412 <jr4...(a)googlemail.com> wrote:
jr4,
>
> care to post the script which works when source'd?

I didn't/don't understand this question because for this specific
problem I don't have a working TCL/Expect script.

Anyway it gave me an idea. In the header of my script I was using
#!/usr/bin/expect

So I changed it to
#!/usr/bin/tclsh

and now my file is fully parsed. But.. the Expect part is failing.
invalid command name "spawn"
while executing
"spawn ftp metalab.unc.edu"

Here's the full script:
#!/usr/bin/tclsh

set anyvar [llength $argv]
for {set i 0} {$i<$anyvar} {incr i} {
puts "arg $i: [lindex $argv $i]"
}

puts "Number of arguments on command line: $argc"

if {[llength $argv] == 0 } {
puts "usage: script_name rfcx.txt (where x is the number)"
puts "example: script_name rfc28.txt"
exit 1
}

proc retrieve {rfcs} {
foreach item $rfcs {
send "get $item\r"
expect "ftp>"
}
}

spawn ftp metalab.unc.edu
expect "Name"
send "anonymous\r"
expect "Password"
send "anybody(a)yahoo.com\r"
expect "ftp>"
send "cd pub\r"
expect "ftp>"
send "cd docs\r"
expect "ftp>"
send "cd rfc\r"
expect "ftp>"
send "binary\r"
expect "ftp>"
set timeout 120
retrieve $argv
send "bye\r"
expect "Goodbye."

# Attempt 2
set fp [open "rfc-index.txt" r]
while { [gets $fp data] >= 0 } {
puts $data
}
close $fp

It may be obvious that I'm new to TCL and Expect. I was thinking that
since Expect is an extension of TCL that the two interpreters might be
interchangeable? I must be doing something silly.