LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-28-2009, 01:44 AM   #1
delevel
LQ Newbie
 
Registered: May 2009
Location: Sweden
Distribution: Debian/RHEL6
Posts: 17

Rep: Reputation: 0
Expect and ls -l *.sh


I get this error
Quote:
ls: cannot access *.sh: No such file or directory
while executing
"exec ls -l *.sh"
invoked from within
"set MYLIST [exec ls -l *.sh]"
(file "./myls.sh" line 2)
my code for myls.sh
Code:
#!/usr/bin/expect -f
set MYLIST [exec ls -l *.sh]
puts "$MYLIST"
exit
 
Old 05-28-2009, 07:20 AM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Tcl (and therefor Expect) doesn't do file name expansion, so everything passed to ls with a wildcard will fail.

This works:

Code:
#!/usr/bin/expect -f
set MYLIST [exec ls -l [glob *.sh] ]
puts "$MYLIST"
exit
Note that your program was terminated because the call to ls was considered failed. This is not usually wanted behaviour, not being able to find certain files is a valid result of ls, and that most of the times should not result in termination of your expect program.

You could consider putting the exec call in a catch {} construct so the program does not crash when ls fails.
http://www.hume.com/html84/mann/catch.html

jlinkels
 
Old 05-28-2009, 09:09 AM   #3
delevel
LQ Newbie
 
Registered: May 2009
Location: Sweden
Distribution: Debian/RHEL6
Posts: 17

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jlinkels View Post
Tcl (and therefor Expect) doesn't do file name expansion, so everything passed to ls with a wildcard will fail.

This works:

Code:
#!/usr/bin/expect -f
set MYLIST [exec ls -l [glob *.sh] ]
puts "$MYLIST"
exit
Note that your program was terminated because the call to ls was considered failed. This is not usually wanted behaviour, not being able to find certain files is a valid result of ls, and that most of the times should not result in termination of your expect program.

You could consider putting the exec call in a catch {} construct so the program does not crash when ls fails.
http://www.hume.com/html84/mann/catch.html

jlinkels
Thanks it works but got a other problem if there is more then one .sh file in that dir I get this insted

Quote:
ls: cannot access myls2.sh myls.sh: No such file or directory
while executing
"exec ls -l [glob *.sh] "
invoked from within
"set MYLIST [exec ls -l [glob *.sh] ]"
(file "./myls.sh" line 2)
is there a better way in expect to handel wildcards? so I can do a "ls -l *.sh" so it works like in shell?
 
Old 05-28-2009, 10:03 AM   #4
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Beats me...

When I tested it, I happened to have only one *.sh file in that directory. Now I have two files and I get the same error as you do.

[glob *.sh] as such returns a list with two file names.

When I write these two file names in the exec command verbatim I don't get an error:

Code:
set MYLIST [exec ls -l ppc_timing.sh another.sh]
works fine.

Now even when I do
Code:
set MYLIST [exec ls -l [join[glob *.sh]]]
and the outcome of [join[glob *.sh]] is exactly ppc_timing.sh another.sh, ls returns an error.

This works:
Code:
set MYLIST [exec ls -l [lindex[glob *.sh] 0]]
but again returns only one file name.

You could use a foreach loop as workaround. For the moment I have to stop looking as my boss starts looking at me.

jlinkels
 
Old 05-28-2009, 07:05 PM   #5
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
This works:
Code:
set MYLIST [eval exec ls -l [glob *.sh]]
The eval expresion is needed to flatten the list produced by glob (so it is explained). If the list is not flattened, the argument passed to ls is ONE file name, with spaces in it, e.g. "file1.sh file2.sh" instead of "file1.sh" "file2.sh". It doesn't ring a bell why eval makes that work right, but it does. As a matter of fact it is a textbook example to use eval here: http://wiki.tcl.tk/1017

Also, study this page thoroughly:
http://www.tcl.tk/man/tcl8.5/tutorial/Tcl26.html

jlinkels
 
Old 05-29-2009, 01:37 AM   #6
delevel
LQ Newbie
 
Registered: May 2009
Location: Sweden
Distribution: Debian/RHEL6
Posts: 17

Original Poster
Rep: Reputation: 0
Smile

Thanks man you saved my day I usually just program in PHP. Oh and thanks for taking the time and good links too.
 
  


Reply

Tags
expect, tcl, wildcard


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use expect TomCruise2002 Linux - General 2 04-09-2017 08:00 AM
Expect script: how do i send function key F12 in an expect script alix123 Programming 4 09-01-2013 09:06 PM
Calling expect scripts from other expect scripts sevapopov Linux - Software 3 04-03-2008 09:33 PM
expect - any other way? rooksin Programming 1 03-09-2006 10:26 PM
expect sk8guitar Programming 3 07-29-2003 09:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration