Unix Shell - Prompting for values in expect

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > November 2006 > Prompting for values in expect





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 Prompting for values in expect
kenkahn

2006-11-30, 7:23 pm

When using expect, how do you prompt a user for input and store that
value into a variable? Similar to the print/read combo for ksh.

Also, does expect support conditional statements like if/else?

Stephane CHAZELAS

2006-11-30, 7:23 pm

2006-11-30, 13:26(-08), kenkahn:
> When using expect, how do you prompt a user for input and store that
> value into a variable? Similar to the print/read combo for ksh.
>
> Also, does expect support conditional statements like if/else?


expect_user, send_user

expect_user {
-re "(.*?)\n" {
set var $expect_out(1,string)
send_user "You entered: $var\n"
}
}
if {[string compare $var "yes"] == 0} {
send_user "Affirmative\n"
} {
send_user "Negative\n"
}

I'd suggest you read a book/tutorial about TCL. expect is a TCL
interpreter with some expecting functionalities.

--
Stéphane
Stephane CHAZELAS

2006-11-30, 7:23 pm

2006-11-30, 21:42(+00), Stephane CHAZELAS:
> 2006-11-30, 13:26(-08), kenkahn:
>
> expect_user, send_user
>
> expect_user {
> -re "(.*?)\n" {
> set var $expect_out(1,string)
> send_user "You entered: $var\n"
> }
> }

[...]

And if you've got an old TCL without the perl-like regular
expressions:

expect_user {
-re "\n" {
set var [string trimright $expect_out(buffer) "\n"]
send_user "You entered: $var\n"
}
}

Note that expect_user is subject to $timeout just as expect.

The gets(n) tcl command doesn't seem to work when stdin is a
terminal.

gets stdin var
puts "You entered $var"

Works in

echo foo | expect ./this-script.exp

--
Stéphane
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com