开发者

Variable within an expect file

开发者 https://www.devze.com 2023-03-16 21:15 出处:网络
I have开发者_如何学JAVA an expect file that looks like this.I invoke the script using expect myfile.exp

I have开发者_如何学JAVA an expect file that looks like this. I invoke the script using

expect myfile.exp

I want to be able to send a value of 1,2,3 or 4. Is there a notion of variable within the expect file?

expect -exact "> "
sleep .1
send -s -- "1\r"
expect -exact "> "
sleep .1
# This prompt can take values 1, 2, 3 or 4
send -s -- "1\r"
# I want to replace "1\r" above with one of the 4 possible values at runtime
expect -exact "> "
sleep .1
send -s -- "1\r"


Do you want this to be interactive? So that the script requests input from the user or does it need to be dependant on some value it receives?

Request user input: http://my.safaribooksonline.com/book/operating-systems-and-server-administration/unix/9781565920903/handling-a-process-and-a-user/the_expect_user_command


Solution is to use command line arguments

expect -f myfile.exp 2 3 1

Here 2 is the argument being passed. The arguments are processed as follows in the exp file

set var1 [lrange $argv 0 0]
set var2 [lrange $argv 1 1]
set var3 [lrange $argv 2 2]
expect -exact "> "
sleep .1
send -s -- "1\r"
expect -exact "> "
sleep .1
# This prompt can take values 1, 2, 3 or 4
# Original code 
# send -s -- "1\r"

# Modified code
send -s -- "${var3}\r"

expect -exact "> "
sleep .1
send -s -- "1\r"
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号