我有一个 JBOSS 安装程序,手动运行时会提示输入,下面是执行示例,如下所示
[sp@sp baseInstaller]$ ./advStart.sh config
Buildfile: /home/sp/jboss/sp/baseInstaller/build.xml
init:
config:
[groovy] coa.install.props properties
[groovy]
[groovy] ? indicates a response is required from you
[groovy] [value] is the current value
[groovy] to keep current value just press <enter>
[groovy] or type new value and press <enter>
[groovy] {value,value..} shows the allowed values
[groovy] installer version 6.0.3.4
[groovy]
[groovy] Jboss Server Config Options (changeable after initial config)
[groovy] -------------------------------------------------------------
[groovy] ? host [sp.resource.com]
resource.com
[groovy] ip=10.50.55.90
[groovy] host changed to resource.com
[groovy] ? bind to host only or all ports (all has security implications)
[groovy] [host]
[groovy] {host,all,custom}
all
[groovy] bind to host only or all ports (all has security implications) changed to all
[groovy] ? min memory [64]
64
我尝试构建一个期望脚本来自动执行此过程,下面是我的脚本
[sp@sp baseInstaller]$ cat saba.sh
#!/usr/bin/expect
# Accessing command line arguments
set arg1 [lindex $argv 0]
set arg2 [lindex $argv 1]
# Printing the arguments
puts "Argument 1: $arg1"
puts "Argument 2: $arg2"
cd /home/sp/jboss/sp/baseInstaller
spawn ./advStart.sh config
# Expect the prompt for the argument with a timeout of 10 seconds
expect {
"? host [sp.resource.com]" {
# Send the host value
send "$arg1\r"
exp_continue
}
"? bind to host only or all ports (all has security implications)" {
# Send the argument value
send "$arg2\r"
}
timeout {
puts "Error: Timed out while waiting for the prompt."
exit 1
}
eof
} -timeout 10
# Wait for the script to finish
wait
现在,在执行此脚本时,它在下面的位置被击中,并且不再继续进行。你能帮我解决这个问题吗?
[sp@sp baseInstaller]$ expect saba.sh resource.com all
Argument 1: resource.com
Argument 2: all
spawn ./advStart.sh config
Buildfile: /home/sp/jboss/sp/baseInstaller/build.xml
init:
config:
[groovy] coa.install.props properties
[groovy]
[groovy] ? indicates a response is required from you
[groovy] [value] is the current value
[groovy] to keep current value just press <enter>
[groovy] or type new value and press <enter>
[groovy] {value,value..} shows the allowed values
答案1
expect
对使用的形式非常严格{}
。通过-timeout 10
在后面添加多余的内容,{}
它不再识别多行{}
格式,并错误地解释要匹配的模式。如果您使用 debug (选项)运行脚本,您可以看到这一点-d
。
考虑这个更简单的例子:
expect {
"abc" exit 1
} -timeout 10
你会看到调试
expect: does "" (spawn_id exp4) match glob pattern "\n "abc" exit 1\n"? no
"10"? no
正如您所看到的,模式被视为“{”和“}”之间的所有内容,并且“-timeout”被视为匹配时执行的命令。 “10”是下一个要匹配的模式。
此外,该模式"? host [sp.resource.com]"
将不匹配,因为默认情况下该模式被假定为“glob”,并且[
以一组字符开头的特殊字符也是如此。您需要在模式前加上前缀以-ex
表示完全符合被通缉。确保空格与数据完全匹配。