当 bash 脚本提示输入“ ”时,我想使用“ expect
”自动输入“ ”键。<Enter>
Country Name (2 letter code) [US]:
预期如下:
/usr/bin/expect << EOF
set timeout -1
spawn /bin/bash /path/to/generate_openssl_certificate.sh
expect {
"Country Name (2 letter code) [US]:" {send "\r" ; exp_continue}
}
EOF
错误如下:
spawn /bin/bash /path/to/generate_openssl_certificate.sh
invalid command name "US"
while executing
"US"
invoked from within
"expect {
"Country Name (2 letter code) [US]:" {send "\r" ; exp_continue}
}"
当我尝试使用“ -re
”匹配字符串时,它与字符串“ Country Name (2 letter code) [US]:
”不匹配:
expect {
-re "Country Name \(2 letter code\) \[US\]:" {send "\r" ; exp_continue}
}
这是输出,屏幕正在等待“ <Enter>
”键继续:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [US]:
Country Name (2 letter code) [US]:
不使用“ ”,如何匹配“ -re
”?如何使用“ Country Name (2 letter code) [US]:
”匹配“ -re
”?
Expect 是 Tcl 语言的扩展。Tcl 使用方括号进行命令替换(与 POSIX 类型的 shell 使用的方式相同
$(...)
)。命令替换将在双引号字符串内扩展。您可以做两件事:
转义开括号
使用 Tcl 的非插值引用机制、花括号
请注意,我使用的是精确匹配——默认情况下,expect 使用全局匹配,其中方括号代表字符集。在这种情况下,您需要匹配文字括号。