Expect Script - While file exist | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Expect Script - While file exist

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-08-2008
skal5 skal5 is offline
Registered User
 
Join Date: Oct 2008
Last Activity: 9 October 2008, 4:58 PM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Expect Script - While file exist

Hi:

Can anyone help me with the while loop in expect script. Following is my scenario:
1. one of my process is running and looking for a file
2. as soon as the file is dropped the proces then processes the file and creates a control file ".ctl" to indicate end of process for that file
3. after the above process, I need to rename the ".ctl" file to ".done" as part of the second processing

How do I write an expect script to check if the file exists in the directory and then only rename the file from ".ctl" to ".done" extension. I can work with the if condition, but my expect script is the one that drops the file and will have to wait till the ".ctl" file is created by the process and then my expect script will rename the .ctl file to .done

set filename "abc"

expect ">$ "
while { [file exists $flname.ctl] }\
{
expect ">$ "
puts "$flname.ctl exists\r"
expect ">$ "
send "mv $flname.ctl $flname.done\r"
}

For some reason the above code doesnt do anything. I am not sure if it's the syntax or something that I am overlooking. Or is it plain that I cant use "file exists" command in a while loop.
Please advice
Sponsored Links
    #2  
Old 10-08-2008
ramen_noodle ramen_noodle is offline Forum Advisor  
Registered User
 
Join Date: Dec 2007
Last Activity: 23 December 2008, 1:00 PM EST
Location: Virginia, USA.
Posts: 251
Thanks: 0
Thanked 1 Time in 1 Post
Ouch. I could probably advise but your outlined procedure is very wrong.
If all in tcl this will require a moderate amount of work and very little expect as
explained.
Sponsored Links
    #3  
Old 10-08-2008
skal5 skal5 is offline
Registered User
 
Join Date: Oct 2008
Last Activity: 9 October 2008, 4:58 PM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
I could do that using shell script, but I am automating some tests where in the script auto logs-in a remote server, then navigate to the specific directory where the data is, then touch the data file with .ready extension, and then waits for the process to complete processing (creates a .ctl file), after which my using my expect script, I'll have to rename .ctl file to .done extension
    #4  
Old 10-09-2008
ramen_noodle ramen_noodle is offline Forum Advisor  
Registered User
 
Join Date: Dec 2007
Last Activity: 23 December 2008, 1:00 PM EST
Location: Virginia, USA.
Posts: 251
Thanks: 0
Thanked 1 Time in 1 Post
Since you are driving the interaction remotely and possibly dealing with multiple logins concurrently the way I'd do this is to use an interact for each spawn_id (after successful setup) with a procedure that periodically polls the remote filesystem, makes the necessary changes and informs you of completion or error.

Here is a basic example from some code I wrote several years ago. You may want to check the interact examples out at the expect site and at active state.

Code:
spawn -noecho ssh -X root@$host


                       expect {
		                 
		              -re ".*asswor.*" {
			                   send $credentials\r\n
					   if {[info exists initcmd] && [string length $initcmd]} {send "$initcmd\r\n"}
					   if {[info exists cmdfile] && [string length $cmdfile]} {after 1000; feedCmdfile $spawn_id}
					   userMenu; stty echo
					   interact {
					             "~QQ" {rmvConnection $spawn_id}
						     "~AA" {
						           send_user "\nHostname: "
							   expect_user -re "(.*)\r.*" {set hostname $expect_out(1,string)}
							   send_user "\nUser: "
							   expect_user -re "(.*)\r.*" {set username $expect_out(1,string)}
							    if {![string compare [set id [addConnection $username $hostname]] "NUL"] == 0} {
							         iactInner $id
							    } else {
							           send_user "Connection to $hostname failed\n"
							    }	    
						           }
					             "~AF" {doFileAppend $spawn_id}		   		    
						     "~DF" {}
						     "~UF" {}
						     "~XX" {enterInterp $spawn_id}
						     "~MM" {userMenu}
				          }		     
			      }
			      
			      -re ".*\(yes.*no\)" {
			                   send "yes\r\n"
					   exp_continue
			      }
			      
			      -re ".*(#|>).*"  {
			      			if {[info exists initcmd] && [string length $initcmd]} {send "$initcmd\r\n"}
					        if {[info exists cmdfile] && [string length $cmdfile]} {after 1000; feedCmdfile $spawn_id}
					             userMenu; stty echo
					             interact {
					             "~QQ" {rmvConnection $spawn_id}
						     "~AA" {
						           send_user "\nHostname: "
							   expect_user -re "(.*)\r.*" {set hostname $expect_out(1,string)}
							   send_user "\nUser: "
							   expect_user -re "(.*)\r.*" {set username $expect_out(1,string)}
							    if {![string compare [set id [addConnection $username $hostname]] "NUL"] == 0} {
							         iactInner $id
							    } else {
							           send_user "Connection to $hostname failed\n"
							    }	    
						           }		    
						     "~DF" {}
						     "~UF" {}
						     "~XX" {enterInterp $spawn_id}
						     "~MM" {userMenu}
				          }		     
			      }
			      
			      eof {send_user "remote connection for $host has failed\n"}
			      timeout {send_user "Could not connect to $host..connect() timed out\n"}
		     }

Sponsored Links
    #5  
Old 10-09-2008
skal5 skal5 is offline
Registered User
 
Join Date: Oct 2008
Last Activity: 9 October 2008, 4:58 PM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks ramen_noodle for the reply

Thanks for your help ramen_noodle. I tried something else last night and it worked. The while statement support "file exists" feature and I was able to work it out. I will use your input regarding multiple access/login which will boost my script. Thanks a bunch for your quick responses and help.
Sponsored Links
    #6  
Old 01-19-2009
cogitate228 cogitate228 is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 27 January 2009, 2:43 PM EST
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Hi there sorry to dig up an old thread but I couldn't find a thread that was similiar.

I'm trying to get an expect script to look for a specific shell prompt and then execute some simples commands when it detects that prompt.

So say I log in to a box, and the shell prompt is

abc#

it executes commands

and if my script logs in to another box, it would be a different shell prompt like:

def#

How do i get my expect script to recognize that? Reason being is because i need to login as root to execute these commands and the prompts are different from server to server.

Any help would be great.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
file does not exist or unreadable in an FTP script rechever Shell Programming and Scripting 1 07-21-2009 09:45 AM
Check file and if it doesnt exist , exit script tententen Shell Programming and Scripting 10 07-17-2009 01:36 AM
strange expect script behavior, or am i misunderstanding expect scripting? v1k0d3n Shell Programming and Scripting 2 12-10-2008 04:31 PM
Need Script to check file exist and compare rbknisely UNIX for Dummies Questions & Answers 1 01-16-2008 01:08 AM
Have a shell script check for a file to exist before processing another file heprox Shell Programming and Scripting 3 11-14-2006 03:26 AM



All times are GMT -4. The time now is 06:40 AM.