Expect question

Hi all,

I have got a small expect script like this one.

#!/usr/bin/expect -f

set timeout 2
spawn ftp $env(IP)
match_max 100000
expect -exact "Name"
send -- "$env(USER)\n"
expect -exact "Password:"
send -- "$env(PASSWORD)\n"
expect "%"
send "bin\r"
expect "%"
send "prompt\r"
expect "%"
send "cd /etc\r"
expect "%"
send "do some commands blah blah blah\r"
expect "%"
send "cd /etc/init.d\r"
expect "%"
send "put file1\r"
expect "%"

if /etc/init.d does not exist file1 will be uploaded to /etc instead of /etc/init.d. How can I get expect not to upload file1 if /etc/init.d doesn't exist???????

Thank you very much

Instead of expect'ing simply a prompt (%), expect the response from a successful change-directory. You could also expect a failed cd and then create the directory if it doesn't exist.

Could you please give an example?

I have tried this before but it just exists the script.

expect {
#change file to transfer [file]
"2ftp>" [send "put check1\r"]
"550
ftp>" ftp_error
}

This is what I've used for starting a telnet session. Note the syntax is a bit different than what you have in the use of curly braces.

expect {
  timeout { send_user "Telnet timed out waiting for some box\n" ; exit }
  "onnection refused" { send_user "Connection was refused to some box]n" ; exit }
  "nknown host" {send_user "some box is unknown\n" ; exit }
  "Escape character is '^}'."
}

Hi and thanks for the reply.
I still got an issue here.

expect {
"No such file or directory" {send hmmm\r" }
"250 CWD command successful" {send put check1\r" }
}

I would like the script to send hmmm or do nothing if the answere is "No such file or directory"
and to send put check1 if the answere is "250 CWD command successful" which means the directory exists.
Sorry to be a pain

I think you'll want to use send_user instead of send for the "hmmm" message. ftp won't understand what to do with that command.

expect {
  "No such file or directory" 
    {send_user "hmmm\r" 
      send mkdir whatever_dir 
      send cd whatever_dir }
  "250 CWD command successful"
}
send put whatever_file

Not an expect expert, but I don't think you need anything after the final "250..." line as it will get what it expects, and moves on. Not sure if this is exact or the most efficient, but it should be close.