Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 03-27-2009, 02:47 PM   #1
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

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.

set hostFile [lindex $argv 0]
if {$hostFile == ""} {
	send_user "Usage: expect pass_manager \[FILE\]\n"
	send_user "    \[FILE\]       file containing list of hosts\n"
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
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:
if {![file exists $hostFile]} {
  # It's not there.
Old 03-28-2009, 08:37 AM   #3
Senior Member
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Stretch/Sid, Linux Mint DE
Posts: 4,612

Rep: Reputation: 690Reputation: 690Reputation: 690Reputation: 690Reputation: 690Reputation: 690
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.

Old 03-29-2009, 07:11 PM   #4
Registered: Mar 2009
Posts: 49

Original Poster
Rep: Reputation: 15
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.
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.


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 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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:50 PM.

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