LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 02:47 PM   #1
edomingox
Member
 
Registered: Mar 2009
Posts: 64

Rep: Reputation: 16
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, 09:41 PM   #2
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
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, 08:37 AM   #3
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
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, 07:11 PM   #4
edomingox
Member
 
Registered: Mar 2009
Posts: 64

Original Poster
Rep: Reputation: 16
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 07:20 AM.
 
  


Reply



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

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

All times are GMT -5. The time now is 09:31 PM.

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