Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to execute unix command in expect script

782 views
Skip to first unread message

growin...@gmail.com

unread,
Oct 4, 2008, 9:45:59 PM10/4/08
to
Hi,
I'm new to Expect script. It always gives error when I exec following
simple Unix command in Expect script:
ls -1tr | grep *.gz | head -1
Can someone teach me how to execute the command in Expect?
Thanks in advance! /Ron

Cameron Laird

unread,
Oct 5, 2008, 2:04:46 AM10/5/08
to
In article <15f1b3ea-e46d-4466...@a70g2000hsh.googlegroups.com>,

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?

growin...@gmail.com

unread,
Oct 5, 2008, 10:01:40 PM10/5/08
to
On Oct 5, 2:04 am, cla...@lairds.us (Cameron Laird) wrote:
> In article <15f1b3ea-e46d-4466-a842-bf9e92378...@a70g2000hsh.googlegroups.com>,

>
>  <growingbam...@gmail.com> wrote:
> >Hi,
> >I'm new to Expect script. It always gives error when I exec following
> >simple Unix command in Expect script:
> >ls -1tr | grep *.gz  |  head  -1
> >Can someone teach me how to execute the command in Expect?
> >Thanks in advance!     /Ron
>
> 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
>
>     *.gz: No such file or directory
while executing
"exec ls -1tr *.gz | head -1"
invoked from within
"set result [exec ls -1tr *.gz | head -1]"
(file "/home/eric/tools/myexpect02" line 2)

>
> ?  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

Alexandre Ferrieux

unread,
Oct 6, 2008, 4:16:30 AM10/6/08
to
On Oct 6, 4:01 am, growingbam...@gmail.com wrote:
>
> with ls -1tr |grep *.gz | tail -1, I try to find
> the latest .gz file
>
> I tried  set result [lindex [glob -nocomplain *.gz] end], it works.

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

Cameron Laird

unread,
Oct 6, 2008, 7:03:39 AM10/6/08
to
In article <191e4afe-21c4-40de...@h60g2000hsg.googlegroups.com>,
<growin...@gmail.com> wrote:
.
.
.

>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 Alexandre's follow-up, I believe you have a fully-working
solution. That's the main thing, and there's no need to go
beyond that.

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 ...

growin...@gmail.com

unread,
Oct 6, 2008, 9:55:48 PM10/6/08
to
On Oct 6, 7:03 am, cla...@lairds.us (Cameron Laird) wrote:


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

0 new messages