For Programmers: Free Programming Magazines  


Home > Archive > Tcl > June 2007 > Expect expect beginner question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Expect expect beginner question
expectquestion@safe-mail.net

2007-06-07, 4:13 am

Hi everybody,

I am new to Expect and struggle a little bit with one of the main
commands. I wrote a small script, and it doesn't work. What is wrong?
script
start-------------------------------------------------------------------------------------------

#!/usr/bin/expect -f
spawn /bin/bash
....
send -- "test -d /usr\r" #does the directory /usr exist?
expect "test -d /usr\r"
send -- "echo $?" #make the return value visible

#and here is the non working code
expect {
1 { send_user "Directory doesn't exist" }
0 { send_user "Directory exist }
}

expect eof

script
end-------------------------------------------------------------------------------------------

The expect part doesn't choose the right answer. In fact, it looks
like expect has no influence at all.

The operation system is Slackware Linux and the version of Expect is
5.43.0

Thank you very much for your help.

Saludos
Pancho

Why Tea

2007-06-07, 4:13 am

On Jun 7, 11:09 am, expectquest...@safe-mail.net wrote:
> Hi everybody,
>
> I am new to Expect and struggle a little bit with one of the main
> commands. I wrote a small script, and it doesn't work. What is wrong?
> script
> start-------------------------------------------------------------------------------------------
>
> #!/usr/bin/expect -f
> spawn /bin/bash
> ...
> send -- "test -d /usr\r" #does the directory /usr exist?
> expect "test -d /usr\r"
> send -- "echo $?" #make the return value visible
>
> #and here is the non working code
> expect {
> 1 { send_user "Directory doesn't exist" }
> 0 { send_user "Directory exist }
>
> }
>
> expect eof
>
> script
> end-------------------------------------------------------------------------------------------
>
> The expect part doesn't choose the right answer. In fact, it looks
> like expect has no influence at all.
>
> The operation system is Slackware Linux and the version of Expect is
> 5.43.0
>
> Thank you very much for your help.
>
> Saludos
> Pancho


Why not use the built-in Tcl "file" command to test
for it? For example:

if [file isdirectory /usr] { do something }

Pancho.Sanchez.por.que.no@gmail.com

2007-06-07, 4:13 am

Thank you for answering Why Tea,

sorry, the example is bad. I want to check whether a directory on a
different computer is available. Okay, another example, I hope, that's
better *g*.

script
start---------------------------------------------------------------------------------------

#!/usr/bin/expect -f
spawn /bin/bash

send -- "ssh $username@$ip-address\r" #establish a connection to an
other computer
expect password;
send -- "$password\r"
expect "$user@$machine;"
send -- "test -d /usr\r" #does the directory /usr exist?
expect "test -d /usr\r"
send -- "echo $?" #make the return value visible

#and here is the non working code
expect {
1 { send_user "Directory doesn't exist" }
0 { send_user "Directory exist }

}

expect eof

script
end----------------------------------------------------------------------------------------

The question remains: why doesn't work expect properly?

Regards

Pancho

David Gravereaux

2007-06-07, 4:13 am

Pancho.Sanchez.por.que.no@gmail.com wrote:

> send -- "echo $?" #make the return value visible


I think you meant:
send -- "echo \$?"

Yours was asking to reference the variable named '?' Or instead of double quotes,
you could use curlies as what you're sending is a literal:

send -- {echo $?}


--
I'm killing time while I wait for life to shower me with meaning and
happiness. -- Calvin


Why Tea

2007-06-07, 4:13 am

On Jun 7, 12:00 pm, Pancho.Sanchez.por.que...@gmail.com wrote:
> Thank you for answering Why Tea,
>
> sorry, the example is bad. I want to check whether a directory on a
> different computer is available. Okay, another example, I hope, that's
> better *g*.
>
> script
> start---------------------------------------------------------------------------------------
>
> #!/usr/bin/expect -f
> spawn /bin/bash
>
> send -- "ssh $username@$ip-address\r" #establish a connection to an
> other computer
> expect password;
> send -- "$password\r"
> expect "$user@$machine;"
> send -- "test -d /usr\r" #does the directory /usr exist?
> expect "test -d /usr\r"
> send -- "echo $?" #make the return value visible
>
> #and here is the non working code
> expect {
> 1 { send_user "Directory doesn't exist" }
> 0 { send_user "Directory exist }
>
> }
>
> expect eof
>
> script
> end----------------------------------------------------------------------------------------
>
> The question remains: why doesn't work expect properly?
>
> Regards
>
> Pancho


I see. I'm not too sure about the problem, but I'd be
concerned about the setting of the shell variable $?
and whether Expect will see it as it is. I'm sure someone
can answer the question for you. But an alternative and
more reliable method could be using ls, e.g.
send "ls -d /usr\r"
expect {
/user\r { it_exists }
" No such " { it_is_not_there }
}

By the way, there are quite a lot of good Expect
automatic login scripts with telnet/ssh in this user
group. You can do a search to find them.

Cameron Laird

2007-06-07, 4:13 am

In article <1181181615.248613.319080@p77g2000hsh.googlegroups.com>,
<Pancho.Sanchez.por.que.no@gmail.com> wrote:
>Thank you for answering Why Tea,
>
>sorry, the example is bad. I want to check whether a directory on a
>different computer is available. Okay, another example, I hope, that's
>better *g*.
>
>script
>start---------------------------------------------------------------------------------------
>
>#!/usr/bin/expect -f
>spawn /bin/bash
>
>send -- "ssh $username@$ip-address\r" #establish a connection to an
>other computer
>expect password;
>send -- "$password\r"
>expect "$user@$machine;"
>send -- "test -d /usr\r" #does the directory /usr exist?
>expect "test -d /usr\r"
>send -- "echo $?" #make the return value visible
>
>#and here is the non working code
>expect {
> 1 { send_user "Directory doesn't exist" }
> 0 { send_user "Directory exist }
>
>}
>
>expect eof
>
>script
>end----------------------------------------------------------------------------------------
>
>The question remains: why doesn't work expect properly?

Mark Janssen

2007-06-07, 8:12 am

On Jun 7, 4:19 am, David Gravereaux <davyg...@pobox.com> wrote:
> Pancho.Sanchez.por.que...@gmail.com wrote:
>
> I think you meant:
> send -- "echo \$?"
>
> Yours was asking to reference the variable named '?' Or instead of double quotes,
> you could use curlies as what you're sending is a literal:
>
> send -- {echo $?}
>
> --
> I'm killing time while I wait for life to shower me with meaning and
> happiness. -- Calvin
>
> signature.asc
> 1KDownload


Actually in this case it doesn't matter as $? is not one of the forms
allowed for variable substitution. Thus
puts $?
and
puts \$?
will print the same. To get the contents of variable ? ${?} should be
used.

Mark

Cameron Laird

2007-06-07, 10:11 pm

In article <1181183950.445931.63230@n4g2000hsb.googlegroups.com>,
Why Tea <ytlim1@gmail.com> wrote:
>On Jun 7, 12:00 pm, Pancho.Sanchez.por.que...@gmail.com wrote:
>start---------------------------------------------------------------------------------------
>end----------------------------------------------------------------------------------------
>
>I see. I'm not too sure about the problem, but I'd be
>concerned about the setting of the shell variable $?
>and whether Expect will see it as it is. I'm sure someone
>can answer the question for you. But an alternative and
>more reliable method could be using ls, e.g.
> send "ls -d /usr\r"
> expect {
> /user\r { it_exists }
> " No such " { it_is_not_there }
> }
>
>By the way, there are quite a lot of good Expect
>automatic login scripts with telnet/ssh in this user
>group. You can do a search to find them.
>


It's OK to write "$?". As you write, it is indeed possible to
use several different approaches to search on a remote Unix
filesystem for a specific node. In particular, though, Mr.
Sanchez' technique *can* work, once it's cleaned up a bit:

spawn ssh $account@$host
expect "assword: "
send $password\r
send "test -d /usr\r"
expect {$ }
send "echo \"It is $?.\"\r"
expect {$ }
send exit\r
expect eof
puts "All done."

emits:

...
$account@$host:~$ test -d /usr
$account@$host:~$ echo "It is $?."
It is 0.
$account@$host:~$ exit
logout
Connection to $host closed.
All done.

Season to taste.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com