LinuxQuestions.org
Register a domain and help support LQ
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 03-27-2009, 03:47 PM   #1
edomingox
Member
 
Registered: Mar 2009
Posts: 49

Rep: Reputation: 15
Expect script fails to show file exist failure


I have this script to check if a file exists. The first "if" statement just makes sure that the file was entered in the command line. That works fine. Where it fails is the next step. I call the "ls" command just to find the file. That works if the file was there. It would show this:

# expect pass_manager.exp /etc/hosts
/etc/hosts

Now when I put a file that doesn't exist into the command line, I'm expecting to see the "does not exist" line to show. But I only get this:

# expect pass_manager.exp hosts
ls: cannot access hosts: No such file or directory

Is there something that I'm missing? What I'm really looking for is a way to expect script a way to check if a file exists. And there are not many good tutorials out there that I can find.

Code:
set hostFile [lindex $argv 0]
if {$hostFile == ""} {
	send_user "Usage: expect pass_manager \[FILE\]\n"
	send_user "    \[FILE\]       file containing list of hosts\n"
	exit
}
set timeout 3
spawn -noecho ls $hostFile
expect {
	"No such file or directory" {
		send "does not exist\n"
	}	
}
 
Old 03-27-2009, 10:41 PM   #2
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 41
I don't really know Expect but I believe it has full Tcl behind it right? How about just:
Code:
if {![file exists $hostFile]} {
  # It's not there.
}
http://www.tcl.tk/man/tcl8.5/TclCmd/contents.htm
 
Old 03-28-2009, 09:37 AM   #3
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,236

Rep: Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545
What you see is the return string of ls. That is correct.

If you receive "No such file", Expect will send "does not exist" the the connection it just opened. Since that connection is just closed as ls terminated.

A solution like taylor gave should work if you just want to check for file existence in tcl. If your goal is to use Expect anyway and communciate thru shell commands, make sure you set set exp_interal 1 to see what is happening back and forth the command you sent.

Without exp_internal you are clueless about what expect receives and how it is interpreted.

jlinkels
 
Old 03-29-2009, 08:11 PM   #4
edomingox
Member
 
Registered: Mar 2009
Posts: 49

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jlinkels View Post
If you receive "No such file", Expect will send "does not exist" the the connection it just opened. Since that connection is just closed as ls terminated.
jlinkels
That makes sense. I will try Taylor's method.

edit: It worked perfectly. I'll have to try to figure out the way expect works a bit better. Can't really find any good tutorials on it. I may have to look up some tcl tutorials.

Last edited by edomingox; 03-30-2009 at 08:20 AM.
 
  


Reply


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
to find if a file exist using shell script nanxy Programming 8 09-16-2013 11:30 PM
Expect script: how do i send function key F12 in an expect script alix123 Programming 4 09-01-2013 10:06 PM
xfce-setting-show gives no such file or directory plus HAL failure marymcd Linux - Newbie 0 03-11-2009 11:26 AM
Why does this Expect script fall through on failure? MattFunke Linux - Software 1 03-27-2008 04:02 AM
Expect script - No output to file pandersson61 Programming 4 05-23-2007 06:18 AM


All times are GMT -5. The time now is 03:17 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration