|
|
 |
 |
 |
 |
TCL(Tool Command Language) Scripting
|
 |
 |
 |
 |
 |
 |
 |
 |
Expect expect beginner question
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
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 }
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
Pancho.Sanchez.por.que @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
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.
In article <1181181615.248613.319@p77g2000hsh.googlegroups.com>,
<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?
. . . Expect does work properly. It *is* working properly, almost certainly. You probably want expect {assword: } and send "echo \$?\r" rather than expect password; and send -- "echo $?" #make the return value visible and so on. You'll want to read <URL: http:/wiki.tcl.tk/3173 >. More, later.
On Jun 7, 4:19 am, David Gravereaux <davyg@pobox.com> wrote:
> Pancho.Sanchez.por.que @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 > 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
In article <1181183950.445931.63@n4g2000hsb.googlegroups.com>, Why Tea <ytl@gmail.com> wrote:
>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.
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.
Hi Cameron, your script works perfectly. Thank you.
|
 |
 |
 |
 |
|