Linux expect文件中的变量

Linux expect文件中的变量,linux,expect,Linux,Expect,我有一个expect文件,看起来像这样。我使用 expect myfile.exp 我希望能够发送1、2、3或4的值。expect文件中是否有变量的概念 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 w

我有一个expect文件,看起来像这样。我使用

expect myfile.exp
我希望能够发送1、2、3或4的值。expect文件中是否有变量的概念

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"

您希望这是交互式的吗?因此脚本请求用户输入,还是需要依赖于它接收到的某些值

请求用户输入:
解决方案是使用命令行参数

expect -f myfile.exp 2 3 1
这里2是正在通过的参数。参数在exp文件中的处理如下

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"

与其使用
[lrange$argv 0]
不如使用
[lindex$argv 0]
等。