Yes.
To help you be successful as quickly as possible, please
provide more context--a sketch of what you *really* want,
for example. Are you trying to find the first *.gz file
on a remote machine? How does it happen that you're
focused on Expect rather than Tcl? Why
ls -1tr | grep *.gz | head -1
rather than
ls -1tr *.gz | head -1
I'm not claiming there's anything wrong with what you're
doing; I just need to understand it better.
Are you truly writing
set result [exec ls -1tr | grep *.gz | head -1]
? How does
set result [lindex [glob -nocomplain *.gz] 0]
not meet your need?
>
> ? How does
>
> set result [lindex [glob -nocomplain *.gz] 0]
>
> not meet your need?
Thanks for your quick response. My application is generating a
performance file in .gz format everyday and need to be uploaded (sftp)
to a another server. with ls -1tr |grep *.gz | tail -1, I try to find
the latest .gz file and use expect to automate sftp.
With set result [exec ls -1tr | grep *.gz | tail -1],
expect *.gz: No such file or directory
while executing
"exec ls -1tr *.gz | tail -1"
invoked from within
"set result [exec ls -1tr *.gz | tail -1]"
(file "/home/eric/tools/myexpect02" line 2)
On this group, I found this will work:
set result [exec sh -c "ls -1tr *.gz" | tail -1]
I tried set result [lindex [glob -nocomplain *.gz] end], it works.
Thanks for your help. I find I have lot of to learn.
/Ron
It is an accident. The order of the list returned by [glob] is just
the underlying filesystem's raw order ("FAT order" would be a familiar
term for DOS aficionados).
If you want the most recently modified file, compute it by combining
[glob] and [file mtime]:
set mt 0
foreach fn [glob -nocomplain *.gz] {
set t [file mtime $fn]
if {$t>$mt} {
set mt $t
set recent $fn
}
}
# use $recent
For a different trade-off between performance on large lists and
readability, you can also:
set l {}
foreach fn [glob -nocomplain *.gz] {
lappend l [list $fn [file mtime $fn]]
}
set recent [lindex [lindex [lsort -integer -decreasing -index 1 $l]
0] 0]
-Alex
-Alex
I'd like to tie up a few loose ends, though, if you have the
patience to pursue this more:
A. Thanks for the higher-level explanation of what
you're doing (latest .gz to another server ...).
Perhaps more than you realize, it helps us answer
you.
B. My apology for leading you astray. When I first
responded to you, I ignored the -t or "the
most recent ..." part of what you were doing.
C. Please be aware that Expect is NOT necessary in
general when automating sftp. Again, if you're
happy with what you have, that's fine; <URL:
http://phaseit.net/claird/comp.unix.programmer/ftp_automation.html >
hints at alternatives, though. Eventually, I'll
elaborate <URL: http://wiki.tcl.tk/19641 > to
make more of this clear ...
Thanks for your and Alex's help. I'm new to script. Your help really
encourage me to keep learning. Hope someday I can help other people. /
Ron